Table does not exist, but it EXISTS

北慕城南 提交于 2021-02-08 10:40:33

问题


So I'm starting to learn how to use PHP to access MYSQL databases and after successfully connecting to the database. 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. 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. I just get the error "Table 'test.blog_table' doesn't exist" with the code below

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



回答1:


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.




回答2:


Probably you are missing the mysqli and the mysqlnd PHP extensions.

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 (e-g.: pdo_mysql) extensions.



来源:https://stackoverflow.com/questions/60977257/table-does-not-exist-but-it-exists

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!