- 相關推薦
php連接mysql數(shù)據(jù)庫代碼
書山有路勤為徑,學海無涯苦作舟。學習PHP就要勤奮。下面是php連接mysql數(shù)據(jù)庫代碼,歡迎閱讀參考!
<?php
$mysql_server_name="localhost"; //數(shù)據(jù)庫服務器名稱$mysql_username="root"; // 連接數(shù)據(jù)庫用戶名$mysql_password="111111"; // 連接數(shù)據(jù)庫密碼$mysql_database="phptest"; // 數(shù)據(jù)庫的名字// 連接到數(shù)據(jù)庫$conn=mysql_connect($mysql_server_name, $mysql_username,$mysql_password);// 從表中提取信息的sql語句$strsql="select * from test";
// 執(zhí)行sql查詢
$result=mysql_db_query($mysql_database, $strsql, $conn);// 獲取查詢結果$row=mysql_fetch_row($result);echo '<font face="verdana">';
echo '<table border="1" cellpadding="1" cellspacing="2">';// 顯示字段名稱echo "\n<tr>\n";for ($i=0; $i<mysql_num_fields($result); $i++){echo '<td bgcolor="#ff0F00"><b>'.
mysql_field_name($result, $i);
echo "</b></td>\n";
}
echo "</tr>\n";
// 定位到第一條記錄
mysql_data_seek($result, 0);
// 循環(huán)取出記錄
while ($row=mysql_fetch_row($result))
{
echo "<tr>\n";
for ($i=0; $i<mysql_num_fields($result); $i++ ){echo '<td bgcolor="#00FF00">';echo "$row[$i]";
echo '</td>';
}
echo "</tr>\n";
}
echo "</table>\n";
echo "</font>";
// 釋放資源
mysql_free_result($result);
// 關閉連接
mysql_close();
?>
-------------------------------------------------------------------------------------
<?php$link = mysql_connect("mysql_host", "mysql_user", "mysql_password") or die("Could not connect");print "Connected successfully";mysql_select_db("my_database") or die("Could not select database");$query = "SELECT * FROM my_table";$result = mysql_query($query) or die("Query failed");print "<table>\n";while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { print "\t<tr>\n"; foreach ($line as $col_value) { print "\t\t<td>$col_value</td>\n"; } print "\t</tr>\n"; } print "</table>\n";mysql_free_result($result);mysql_close($link);?>
-------------------------------------------------------------------------------------
代碼單獨存為一個文件conn.php,在以后要用到這些代碼時只要include這個頁面就可以了.如add.php這個頁面要要添加數(shù)據(jù)入數(shù)據(jù)庫,那么在add.php里include "conn.php";
【php連接mysql數(shù)據(jù)庫代碼】相關文章:
PHP數(shù)據(jù)庫:mysql重置密碼12-03
一段經(jīng)典php mysql分頁程序代碼03-22
php與php MySQL之間的關系03-30
關于php操作mysql執(zhí)行數(shù)據(jù)庫查詢11-18
php查詢mysql的實例03-29
PHP常用MySql操作的方法03-27
php語言字典代碼03-18
php+mysql注射語句構造12-04
PHP中MySql操作是什么11-17