吼吼,剛學,記錄一下~
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);?>
以上就介紹了php串連資料庫實現簡單查詢,包括了方面的內容,希望對PHP教程有興趣的朋友有所協助。