简体   繁体   English

表不存在,但它存在

[英]Table does not exist, but it EXISTS

So I'm starting to learn how to use PHP to access MYSQL databases and after successfully connecting to the database.所以我开始学习如何使用PHP访问MYSQL数据库并在成功连接到数据库之后。 I want to select data from it.我想从中选择数据。 However I get the issue that the table doesn't exist.但是我得到了表不存在的问题。 Yet, it exists when I check it in my phpmyadmin.然而,当我在我的 phpmyadmin 中检查它时它存在。 I've checked spelling, capitalization, etc.. and nothing works.我检查了拼写、大小写等,但没有任何效果。 I've tried it with multiple databases and I get the same error.我已经尝试使用多个数据库,但出现相同的错误。 So I'm really confused as to whats going on because from the looks of it, there is NOTHING wrong with the code.所以我真的很困惑发生了什么,因为从它的外观来看,代码没有任何问题。 I've tried running the SQL query in phpmyadmin just to verify that the query works.. and it does.我试过在 phpmyadmin 中运行 SQL 查询只是为了验证查询是否有效..确实如此。 I just get the error "Table 'test.blog_table' doesn't exist" with the code below我刚刚收到错误“表'test.blog_table'不存在”,下面的代码

<?php

$host = "localhost";
$user = "root";
$password = "";
$database_in_use = "test";



$conn = new mysqli($host, $user, $password, $database_in_use);
if ($conn->connect_errno) {
echo "Failed to connect to MySQL: (" . $conn->connect_errno . ") " . $conn->connect_error;
}

echo $conn->host_info . "<br>";




$sql = "SELECT * FROM blog_table";
$result = $conn->query($sql);

 $result = $conn->query($sql) or die($conn->error);

$conn->close();
?>

So I'm just completely lost and have no idea whats going on with it.所以我完全迷失了,不知道发生了什么。


Solved!解决了! I connected to the wrong database, which is why the tables were there, but weren't showing up.我连接到错误的数据库,这就是为什么表在那里,但没有出现的原因。 Since I was connecting to a different database and the one I created tables in didn't have the default port.由于我连接到另一个数据库,而我在其中创建表的数据库没有默认端口。

Probably you are missing the mysqli and the mysqlnd PHP extensions.您可能缺少mysqlimysqlnd PHP 扩展。

Also, I recommend you to use \\PDO object to fetch queries to your DB instead of the mysqli driver, if you do it, you will be free to change in the future to a PostgreSQL DB for example anytime just changing the DSN in the constructor (you need for that the PDO and the pdo_whatever_db_driver (eg.: pdo_mysql ) extensions.此外,我建议您使用 \\PDO 对象来获取对您的数据库的查询而不是mysqli驱动程序,如果您这样做,您将来可以自由地更改为 PostgreSQL 数据库,例如随时更改构造函数中的 DSN (为此,您需要PDOpdo_whatever_db_driver (例如: pdo_mysql )扩展。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM