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.
2
Answers
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 themysqlnd
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 thePDO
and thepdo_whatever_db_driver
(e-g.:pdo_mysql
) extensions.