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 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog