I'm learning php and right now I'm figuring out dbs. Was following up with a tutorial on YT and got stuck in the following:
我正在学习php,而且现在我正在计算dbs。正在跟进YT的教程并陷入以下困境:
"retrieve all records from table"
“从表中检索所有记录”
Specifically, I can connect to my DB no prob, but when I perform the select query it prints nothing
具体来说,我可以连接到我的数据库没有概率,但当我执行选择查询时,它什么都不打印
Here's my code...
这是我的代码......
<?php
//error_reporting(0);
require 'connect.php';
$result = $db->query("SELECT * FROM sample-objects");
//$result = mysqli_query($db, "SELECT * FROM sample-objects");
print_r($result);
?>
and my connection script
和我的连接脚本
<?php
$db = new mysqli('localhost','root','','test');
// echo $db->connect_errno,"\n";
if ($db->connect_errno)
{
echo "error: ",$db->connect_error; /* Imprime descripción de error de DB */
//die("Site offline");
}
?>
I don't get any errors from "connect.php" and I do have 2 records in my db (did so from phpmyadmin)
我没有从“connect.php”得到任何错误,我的数据库中有2条记录(来自phpmyadmin)
Any help would be appreciated, thanks :)
任何帮助将不胜感激,谢谢:)
1 个解决方案
#1
1
Having checked for errors on your query would have signaled the error in your table name containing a hyphen:
检查查询中的错误后,会在包含连字符的表名中发出错误信号:
SELECT * FROM sample-objects
^ that is the problem character
where MySQL is interpreting that as sample MINUS objects
thinking you want to do math here.
MySQL正在将其解释为MINUS对象,它认为你想在这里做数学。
Either you wrap it with ticks (careful, those are not single quotes '
) copy/paste as shown:
你可以用蜱(小心,那些不是单引号')包装它,如图所示复制/粘贴:
SELECT * FROM `sample-objects`
or rename it with an underscore (rename your table first before using this example)
或者使用下划线重命名(在使用此示例之前先重命名表)
SELECT * FROM sample_objects
References:
参考文献:
- http://php.net/manual/en/mysqli.error.php
- http://php.net/manual/en/mysqli.error.php
- http://dev.mysql.com/doc/en/identifier-qualifiers.html
- http://dev.mysql.com/doc/en/identifier-qualifiers.html
Example from the manual on mysqli_query()
:
mysqli_query()手册中的示例:
if (!$mysqli->query("SET @a:='this will not work'")) {
printf("Error: %s\n", $mysqli->error);
}
I also noticed you commented out //error_reporting(0);
我也注意到你注释了// error_reporting(0);
That actually turns error reporting off. You need to turn it on and to display:
这实际上关闭了错误报告。您需要将其打开并显示:
Add error reporting to the top of your file(s) which will help find errors.
将错误报告添加到文件的顶部,这将有助于查找错误。
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of your code
Sidenote: Displaying errors should only be done in staging, and never production.
旁注:显示错误只能在分段中完成,而不能在生产中完成。
#1
1
Having checked for errors on your query would have signaled the error in your table name containing a hyphen:
检查查询中的错误后,会在包含连字符的表名中发出错误信号:
SELECT * FROM sample-objects
^ that is the problem character
where MySQL is interpreting that as sample MINUS objects
thinking you want to do math here.
MySQL正在将其解释为MINUS对象,它认为你想在这里做数学。
Either you wrap it with ticks (careful, those are not single quotes '
) copy/paste as shown:
你可以用蜱(小心,那些不是单引号')包装它,如图所示复制/粘贴:
SELECT * FROM `sample-objects`
or rename it with an underscore (rename your table first before using this example)
或者使用下划线重命名(在使用此示例之前先重命名表)
SELECT * FROM sample_objects
References:
参考文献:
- http://php.net/manual/en/mysqli.error.php
- http://php.net/manual/en/mysqli.error.php
- http://dev.mysql.com/doc/en/identifier-qualifiers.html
- http://dev.mysql.com/doc/en/identifier-qualifiers.html
Example from the manual on mysqli_query()
:
mysqli_query()手册中的示例:
if (!$mysqli->query("SET @a:='this will not work'")) {
printf("Error: %s\n", $mysqli->error);
}
I also noticed you commented out //error_reporting(0);
我也注意到你注释了// error_reporting(0);
That actually turns error reporting off. You need to turn it on and to display:
这实际上关闭了错误报告。您需要将其打开并显示:
Add error reporting to the top of your file(s) which will help find errors.
将错误报告添加到文件的顶部,这将有助于查找错误。
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of your code
Sidenote: Displaying errors should only be done in staging, and never production.
旁注:显示错误只能在分段中完成,而不能在生产中完成。