skip to Main Content

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


  1. Chosen as BEST ANSWER

    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search