PHP + MySQL查询 - >这个SQL有什么问题(短)

时间:2022-01-28 20:34:32

I have finally graduated from MS Access to MySQL. Unfortunately, I can't even get a query right. Can someone please tell me what's wrong? I keep getting the 'invalid query' message that I specified in the php.

我终于从MS Access到MySQL毕业了。不幸的是,我甚至无法获得正确的查询。有人可以告诉我有什么问题吗?我一直收到我在php中指定的“无效查询”消息。

<?php
@ $db = mysql_connect('localhost', 'root', 'root', 'newdatabase');
if (!$db) {
    die ('Failed to connect to the database.');
}


$query = "SELECT first FROM demographics";
$result = mysql_query($query);

if (!$result) {
    die('invalid query');
}

while ($row = mysql_fetch_assoc($result)) {
    echo $row['first'];
}

?>

Also, the book I am reading tells me to use:

另外,我正在阅读的书告诉我使用:

new mysqli('localhost', 'root', 'root', 'newdatabase')

to connect as opposed to the

连接而不是连接

mysql_connect

I used in the above code. I haven't been able to connect to the db with new mysqli. Does it matter which one I use?

我在上面的代码中使用过。我无法使用新的mysqli连接到数据库。我使用哪一个是否重要?

1 个解决方案

#1


3  

The fourth parameter to mysql_connect() is not the database name. It is a boolean specifying whether or not to establish an additional new connection or use an existing one. Most often, it is omitted. Use mysql_select_db() to choose the database. Since you have not selected a database in your code, your query is likely failing.

mysql_connect()的第四个参数不是数据库名称。它是一个布尔值,指定是否建立其他新连接或使用现有连接。大多数情况下,它被省略。使用mysql_select_db()选择数据库。由于您尚未在代码中选择数据库,因此查询可能会失败。

$db = mysql_connect('localhost', 'root', 'root');
if (!$db) echo mysql_error();
else mysql_select_db("database");

Note I have removed the @ from the mysql_connect() call. @ suppresses errors, and was probably preventing you from seeing what was going wrong with your connection.

注意我已从mysql_connect()调用中删除了@。 @抑制错误,可能会阻止您查看连接出现的问题。

When testing the success or failure of a query, don't die(). Instead echo out mysql_error() while developing your application. In production, you can replace the echo by writing to error_log() instead.

在测试查询的成功或失败时,请不要死()。而是在开发应用程序时回显mysql_error()。在生产中,您可以通过写入error_log()来替换echo。

if (!$result) {
  echo mysql_error();
}

As for using MySQLi instead of the basic mysql_* functions, MySQLi can be used in an object oriented fashion and also offers prepared statements, which can be both more efficient to use and more secure. As mentioned in comments above, PDO is often recommended as the most flexible API for interacting with an RDBMS from PHP.

至于使用MySQLi而不是基本的mysql_ *函数,MySQLi可以以面向对象的方式使用,并且还提供预处理语句,使用起来更高效,更安全。如上面的评论所述,PDO通常被推荐为最灵活的API,用于与PHP中的RDBMS进行交互。

#1


3  

The fourth parameter to mysql_connect() is not the database name. It is a boolean specifying whether or not to establish an additional new connection or use an existing one. Most often, it is omitted. Use mysql_select_db() to choose the database. Since you have not selected a database in your code, your query is likely failing.

mysql_connect()的第四个参数不是数据库名称。它是一个布尔值,指定是否建立其他新连接或使用现有连接。大多数情况下,它被省略。使用mysql_select_db()选择数据库。由于您尚未在代码中选择数据库,因此查询可能会失败。

$db = mysql_connect('localhost', 'root', 'root');
if (!$db) echo mysql_error();
else mysql_select_db("database");

Note I have removed the @ from the mysql_connect() call. @ suppresses errors, and was probably preventing you from seeing what was going wrong with your connection.

注意我已从mysql_connect()调用中删除了@。 @抑制错误,可能会阻止您查看连接出现的问题。

When testing the success or failure of a query, don't die(). Instead echo out mysql_error() while developing your application. In production, you can replace the echo by writing to error_log() instead.

在测试查询的成功或失败时,请不要死()。而是在开发应用程序时回显mysql_error()。在生产中,您可以通过写入error_log()来替换echo。

if (!$result) {
  echo mysql_error();
}

As for using MySQLi instead of the basic mysql_* functions, MySQLi can be used in an object oriented fashion and also offers prepared statements, which can be both more efficient to use and more secure. As mentioned in comments above, PDO is often recommended as the most flexible API for interacting with an RDBMS from PHP.

至于使用MySQLi而不是基本的mysql_ *函数,MySQLi可以以面向对象的方式使用,并且还提供预处理语句,使用起来更高效,更安全。如上面的评论所述,PDO通常被推荐为最灵活的API,用于与PHP中的RDBMS进行交互。