dongluan1743 2017-02-13 17:19
浏览 34
已采纳

MySQL PHP:无法获得任何结果[重复]

This question already has an answer here:

I was trying to just print the content of my MySQL Database with PHP.

This is how my database looks like:

mysql> show tables;
+-------------------+
| Tables_in_myTable |
+-------------------+
| users             |
+-------------------+
1 row in set (0,00 sec)

mysql> select * from users;
+------------+
| name       |
+------------+
| Nick       |
+------------+
1 row in set (0,00 sec)

My PHP code looks like this:

    <?php
echo "HI"; //to see, that the php gets runned at all.

$connection = mysql_connect('localhost', 'root', ''); //The Blank string is the password
mysql_select_db('myTable');

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

echo "<table>"; 

while($row = mysql_fetch_array($result)){    
    echo "<tr><td>" . $row['name'] . "</td></tr>"; 
}

echo "</table>"; 

mysql_close(); 
?>

But on the website, the only output I get is

"HI"

I checked the username, passwd and DB-Names in my PHP.

I'm running an apache2 Server on Ubuntu (Ubuntu Gnome, if that matters) 16.10 and the community version of MySQL

</div>
  • 写回答

2条回答 默认 最新

  • dougezhua0017 2017-02-13 17:26
    关注

    mysql_select_db selects a database, yet you pass your table name as a parameter. Table name is different from database name. Check your database name using

    show databases;
    

    and use that instead of table name on mysql_select_db

    Also, mysql_* functions are deprecated, you need to use mysqli_* functions or PDO.

    It is also a bad idea to run queries like

    SELECT * FROM users
    

    it is better to select the columns you need explicitly, like

    SELECT name FROM users
    

    EDIT

    I was confused by the database name, which seems to be myTable indeed. The problem was that the deprecated functions were not found.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 workstation加载centos进入emergency模式,查看日志报警如图,怎样解决呢?
  • ¥50 如何用单纯形法寻优不能精准找不到给定的参数,并联机构误差识别,给定误差有7个?matlab
  • ¥15 workstation加载centos进入emergency模式,查看日志报警如图,没有XFS,怎样解决呢?
  • ¥20 求各位解释一道区间DP
  • ¥15 应用商店如何检测在架应用内容是否违规?
  • ¥15 Ubuntu系统配置PX4
  • ¥50 nw.js调用activex
  • ¥15 数据库获取信息反馈出错,直接查询了ref字段并且还使用了User文档的_id而不是自己的
  • ¥15 将安全信息用到以下对象时发生以下错误:c:dumpstack.log.tmp 另一个程序正在使用此文件,因此无法访问
  • ¥15 速度位置规划实现精确定位的问题