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 请问以下文字内容及对应编码是用了什么加密算法或压缩算法呢?
  • ¥50 关于#html5#的问题:H5页面用户手机返回的时候跳转到指定页面例如(语言-javascript)
  • ¥15 无法使用此凭据登录,因为你的域不可用,如何解决?(标签-Windows)
  • ¥15 yolov9的训练时间
  • ¥15 二叉树遍历没有报错但无法正常运行
  • ¥15 在linux系统下vscode运行robocup3d上场球员报错
  • ¥15 Python语言实验
  • ¥15 SAP HANA SQL 增加合计行
  • ¥20 用C#语言解决一个英文打字练习器,有偿
  • ¥15 srs-sip外部服务 webrtc支持H265格式