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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵