1

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.


Dharman
  • 30,962
  • 25
  • 85
  • 135
Lewman
  • 39
  • 7
  • try a SHOW tables; and see what it shows you, are you sure that the database selected in the connection string has this table – nbk Apr 01 '20 at 18:05
  • I've tried SHOW TABLES and the error no longer appears, however no tables show up either. And I'm extra sure that the database has the tables. Here is a screenshot of the database directory https://gyazo.com/b1967ec7574f37b31bf13d8e42406dbb – Lewman Apr 01 '20 at 18:17
  • I believe I've found the issue. MyPHPADMIN has 2 servers, MYSQL and MarinaDB. For some reason its not seeing the MYSQL server and is instead only letting me access the MarinaDB. I've tried adding a PW onto the mysql database and then adding that into the password variable but then I get a bunch of errors that access for root user was denied – Lewman Apr 01 '20 at 18:22
  • You are connecting to the default port on 3306 your mysql serv has another port see which he has and add it to your connection stirng – nbk Apr 01 '20 at 18:23
  • It is a very bad idea to use `die(mysqli_error($conn));` in your code, because it could potentially leak sensitive information. See this post for more explanation: [mysqli or die, does it have to die?](https://stackoverflow.com/a/15320411/1839439) – Dharman Apr 01 '20 at 20:42
  • Hi, I was using the die(mysqli_error($conn)); for testing purposes just to see if I could even get a database connection working and whatnot. Thanks anyway though :) – Lewman Apr 04 '20 at 19:48

2 Answers2

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.

Lewman
  • 39
  • 7
0

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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
JesusValera
  • 629
  • 6
  • 14
  • Missing extension would cause fatal error. Not a MySQL error. – Dharman Apr 01 '20 at 20:44
  • Hi, yeah I've been using PDO object now with prepared staments and whatnot. Still learning but slowly but surely on my way to making my own CMS. (It was just because I wasn't connecting to the right database, the WAMP server I was on had mysqli and mysqlnd with it, I think... Either way the setup works now and I'm able to do stuff with my database! – Lewman Apr 04 '20 at 19:48