Home  >  Article  >  php教程  >  PHP 查询MySQL数据库的完整代码

PHP 查询MySQL数据库的完整代码

WBOY
WBOYOriginal
2016-06-13 11:37:222072browse

PHP 查询MySQL数据库的完整代码,使用时请注意修改连接语句中的用户名与密码。

$link = mysql_connect('localhost', 'root', '123456')  //烈火提示:请先修改用户名与密码
or die('Could not connect: ' . mysql_error());
mysql_select_db('ruida') or die('Could not select database');

// 执行 SQL 查询
$query = 'SELECT * FROM product';
$result = mysql_query($query) or die('Query failed: ' . mysql_error());

// 用 HTML 显示结果
echo "

\n";
while ($line = mysql_fetch_assoc($result)) {
echo "\t\n";
foreach ($line as $col_value) {
echo "\t\t\n";
}
echo "\t\n";
}
echo "
$col_value
\n";
// 释放结果集
mysql_free_result($result);
// 关闭连接 Liehuo.Org
mysql_close($link);
?>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn