Home >Backend Development >PHP Tutorial > 初学者提问.PHP读取MYSQL

初学者提问.PHP读取MYSQL

WBOY
WBOYOriginal
2016-06-13 13:41:441142browse

菜鸟提问.PHP读取MYSQL。
入门级菜鸟提问:
连接数据库没反应


echo "测试MYSQL";
@ $db=mysqli_connect('211.71.150.75','root','123','mysql');
echo "测试MYSQL1";
if(mysqli_connect_errno())
{
echo '连接错误!';
exit;
}
$query ="select * from user";
$result =$db->query($query);
$num_results=$result->num_rows;
echo '结果一共'.$num_results.'条';
for($i=0;$i{
$row=$result->fetch_assoc();
echo '

'.($i+1).'.Title:';
}
?>
echo "测试测试!";
?>

只能输出测试MYSQL,其他没有任何反应.
在命令行下测试MYSQL了,没有问题的.

请各位指教,先谢谢啦。

------解决方案--------------------
写得好象很不规范,有些地方没看懂- -#

如果数据库名字为mysql,其下有表user,其下有字段name的话.可以这样写:

PHP code


<p>测试MySQL</p>
<?php @mysql_connect("211.71.150.75","root","123") or die("连接失败!".mysql_error());
   mysql_select_db('mysql');

   $query = "SELECT * FROM `user`";
   $result = mysql_query($query);

   $num = mysql_num_rows($result);
   echo "<p>结果一共$num条

"; while($row = mysql_fetch_assoc()) { echo "

$row[name]

"; } ?>
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