Home  >  Article  >  Backend Development  >  PHP学习笔记之二:在PHP中连接MySQL

PHP学习笔记之二:在PHP中连接MySQL

WBOY
WBOYOriginal
2016-06-23 14:30:03960browse

创建如下脚本文件:dbconnect.php

$dbhost = "localhost";
$dbuser = "root";
$dbpassword = "123456";
$dbdatabase = "productsdb";

$db = mysql_connect($dbhost, $dbuser, $dbpassword); //数据库连接操作,将连接结果保存到$db变量中
mysql_query("SET NAMES 'GBK'");  //设置查询结果为中文
mysql_select_db($dbdatabase, $db); //选择使用的数据库

$sql = "SELECT * FROM products;";
$result = mysql_query($sql); //将SQL语句发送到数据库,并将查询结果放到$result变量中

while($row = mysql_fetch_assoc($result)) //mysql_fetch_assoc从$result中取出每一行记录并将其赋予$row变量
{
 echo $row['product'].'
';
}

?>

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