Home > Article > Backend Development > What to do if php query fails
Solution to PHP query failure: 1. Use the function "mysql_select_db('demo');" to select the query database; 2. Add a database resource connector to mysql_query.
The operating environment of this article: Windows 7 system, PHP version 7.1, DELL G3 computer
What should I do if the php query fails?
The specific problem is described as follows:
php+mysql! 连接成功,但是查询失败! 详细代码如下: <?php $startTime=microtime (true); //打开数据库 $mysql=mysql_connect('127.0.0.1 : 3306','root','root123','demo'); if (!$mysql){ echo "连接失败,原因为:".mysql_error(); exit(); } //读取student表 $query = mysql_query("select * from student") or die("查询失败"); //关闭数据库 mysql_close($mysql ); ?> mysql本地有student表 表中也有数据,在mysql中可以查询 但是mysql不能查询,显示查询失败 我的平台是win7 +apache +mysql +php!谢谢哈 在线等! 各位大侠多谢帮忙哈 本人穷 没有分 还望海涵 纯技术问题。。。 lingquery语句换成show databases 是可以看到里面四个数据库的,分别是 infomation_schema demo mysql test 但是连接demo数据库后查询student表,显示查询失败,但是student表里面是有内容的
Solution:
There are 2 reasons for failure:
1. mysql_connect('127.0.0.1 : 3306','root','root123','demo');
The demo here is a database that cannot be connected to. The function mysql_select_db('demo') should be used;
2. mysql_query("select * from student") or die("query failed");
The mysql_query here requires a database resource Connector (i.e. $mysql)
If you correct the above 2 points, you should not go wrong!
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What to do if php query fails. For more information, please follow other related articles on the PHP Chinese website!