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

时间:2022-09-22 15:12:26

This question already has an answer here:

这个问题已经有了答案:

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

我试着用PHP打印MySQL数据库的内容。

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代码是这样的:

    <?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.

我在PHP中检查了用户名、密码和dba名。

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

我在Ubuntu (Ubuntu Gnome,如果有关系的话)16.10和MySQL的社区版本上运行一个apache2服务器

2 个解决方案

#1


2  

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

mysql_select_db选择一个数据库,但是您将表名作为参数传递。表名与数据库名不同。使用

show databases;

and use that instead of table name on mysql_select_db

并在mysql_select_db上使用它而不是表名。

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

此外,不赞成使用mysql_*函数,您需要使用mysqli_*函数或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.

我被数据库的名字搞糊涂了,这个名字看起来确实是我的。问题是没有找到已废弃的函数。

#2


2  

Given your error message Call to undefined function mysql_connect() it may look like you have a version of PHP that lacks the mysql_ library.

如果您的错误消息调用未定义函数mysql_connect(),可能看起来您有一个缺少mysql_库的PHP版本。

The mysql_ library was removed in 7.x, and there are many reasons for that. You either have to rewrite your code using mysqli or PDO, OR downgrade your PHP version to 5.x (although I would strongly advice against this).

在7中删除了mysql_库。x,有很多原因。要么使用mysqli或PDO重写代码,要么将PHP版本降级为5。x(尽管我强烈反对)。

#1


2  

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

mysql_select_db选择一个数据库,但是您将表名作为参数传递。表名与数据库名不同。使用

show databases;

and use that instead of table name on mysql_select_db

并在mysql_select_db上使用它而不是表名。

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

此外,不赞成使用mysql_*函数,您需要使用mysqli_*函数或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.

我被数据库的名字搞糊涂了,这个名字看起来确实是我的。问题是没有找到已废弃的函数。

#2


2  

Given your error message Call to undefined function mysql_connect() it may look like you have a version of PHP that lacks the mysql_ library.

如果您的错误消息调用未定义函数mysql_connect(),可能看起来您有一个缺少mysql_库的PHP版本。

The mysql_ library was removed in 7.x, and there are many reasons for that. You either have to rewrite your code using mysqli or PDO, OR downgrade your PHP version to 5.x (although I would strongly advice against this).

在7中删除了mysql_库。x,有很多原因。要么使用mysqli或PDO重写代码,要么将PHP版本降级为5。x(尽管我强烈反对)。