php串連資料庫實現簡單查詢

來源:互聯網
上載者:User
吼吼,剛學,記錄一下~

1.首先建立資料庫,在裡面建立資料表test,任意插進去了兩條記錄

2.建立php檔案。

串連資料庫的代碼:

$conn=mysql_connect("localhost","root","");//串連資料庫伺服器if (!$conn){  die('Could not connect: ' . mysql_error());}mysql_select_db("mytest",$conn);//選擇資料庫mytetstmysql_query("set names utf8");//設定編碼格式

mysql_connect(servername,username,password);

servername 可選。規定要已連線的服務器。預設是 "localhost:3306"。
username 可選。規定登入所使用的使用者名稱。預設值是擁有伺服器處理序的使用者的名稱。
password 可選。規定登入所用的密碼。預設是 ""。

3.執行資料庫的語句,例如查詢語句

$arr=mysql_query("select * from test",$conn);

輸出查詢結果

       while($row = mysql_fetch_array($arr)){echo $row['id'] . " " . $row['num'];echo "
";}

————————————————————————————————————————

實現結果:

當點擊尋找全部時,顯示:

按id尋找時,輸入1顯示:

完整代碼:

";echo "idnumnamesexbithday";if(isset($_POST['select_all'])){$arr=mysql_query("select * from test",$conn);while($row = mysql_fetch_array($arr)){echo "{$row['id'] }{$row['num']} {$row['name']} {$row['sex']} {$row['birthday']}";// echo $row['id'] . " " . $row['num'];// echo "
";}}else if (isset($_POST['select_sure'])) {$id=$_POST['select_index'];$arr=mysql_query("select * from test where id=$id",$conn);if($row=mysql_fetch_assoc($arr)){//如果查詢的到echo "{$id}{$row['num']} {$row['name']} {$row['sex']} {$row['birthday']}";}}echo "";mysql_close($conn);?>

  • 大小: 3.3 KB
  • 大小: 8.5 KB
  • 大小: 7.2 KB
  • 查看圖片附件

以上就介紹了php串連資料庫實現簡單查詢,包括了方面的內容,希望對PHP教程有興趣的朋友有所協助。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.