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

WBOY
Release: 2016-06-23 14:30:03
Original
959 people have browsed it

创建如下脚本文件: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'].'
';
}

?>

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!