0

I was trying to just print the content of my MySQL Database with PHP.

This is how my database looks like:

mysql> show tables;
+-------------------+
| Tables_in_myTable |
+-------------------+
| users             |
+-------------------+
1 row in set (0,00 sec)

mysql> select * from users;
+------------+
| name       |
+------------+
| Nick       |
+------------+
1 row in set (0,00 sec)

My PHP code looks like this:

    <?php
echo "HI"; //to see, that the php gets runned at all.

$connection = mysql_connect('localhost', 'root', ''); //The Blank string is the password
mysql_select_db('myTable');

$query = "SELECT * FROM users"; 
$result = mysql_query($query);

echo "<table>"; 

while($row = mysql_fetch_array($result)){    
    echo "<tr><td>" . $row['name'] . "</td></tr>"; 
}

echo "</table>"; 

mysql_close(); 
?>

But on the website, the only output I get is

"HI"

I checked the username, passwd and DB-Names in my PHP.

I'm running an apache2 Server on Ubuntu (Ubuntu Gnome, if that matters) 16.10 and the community version of MySQL

OptimusCrime
  • 14,662
  • 13
  • 58
  • 96
  • 1
    ***Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php).*** [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Feb 13 '17 at 17:22
  • 1
    Add error reporting to the top of your file(s) right after your opening ` – Jay Blanchard Feb 13 '17 at 17:23
  • @JayBlanchard is there a way to do it without? I am a beginner in PHP/SQL and would like to have an easy fix (just for learning, I could learn PDO's after learning the bases). – ThefrenchSpeedruns Feb 13 '17 at 17:25
  • @JayBlanchard TY :) Appearently theres an error with connect... – ThefrenchSpeedruns Feb 13 '17 at 17:26
  • Uncaught Error: Call to undefined function mysql_connect() in /var/www/html/genmainpage.php:6 Stack trace: #0 /var/www/html/index.php(26): include() #1 {main} thrown in /var/www/html/genmainpage.php on line 6 – ThefrenchSpeedruns Feb 13 '17 at 17:26
  • I can not see any direct problems with your code. One possibility is that you are running PHP7, which does not include the mysql-lib. Unless you have proper error logging it may kill you script after the initial output. – OptimusCrime Feb 13 '17 at 17:26
  • 1
    No - you should learn properly right out of the gate. And the error you're getting is why. The The `mysql_*` API is not available on your system, so switching now is the way to go. – Jay Blanchard Feb 13 '17 at 17:29
  • Yeah, I used mysqli – ThefrenchSpeedruns Feb 13 '17 at 17:40

2 Answers2

2

mysql_select_db selects a database, yet you pass your table name as a parameter. Table name is different from database name. Check your database name using

show databases;

and use that instead of table name on mysql_select_db

Also, mysql_* functions are deprecated, you need to use mysqli_* functions or PDO.

It is also a bad idea to run queries like

SELECT * FROM users

it is better to select the columns you need explicitly, like

SELECT name FROM users

EDIT

I was confused by the database name, which seems to be myTable indeed. The problem was that the deprecated functions were not found.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
  • It looks like their database is called `myTable` – Andy Feb 13 '17 at 17:27
  • Ok, after using mysqli everything worked. The problem weren't the databases, as I got a little bit caught while fastly changing the names for the stackoverflow post (The names weren't adult enough for such a cool forum ;')) – ThefrenchSpeedruns Feb 13 '17 at 17:32
  • @Andy thanks for pointing that out, I have edited my answer accordingly. – Lajos Arpad Feb 13 '17 at 17:33
  • @ThefrenchSpeedruns you are right, the problem was that the deprecated functions were not found on your end. I have edited my answer accordingly. If the answer helped you solve your problem, then you might consider accepting it as the correct answer. – Lajos Arpad Feb 13 '17 at 17:34
  • 1
    @LajosArpad I had to wait two minutes ;( – ThefrenchSpeedruns Feb 13 '17 at 17:39
2

Given your error message Call to undefined function mysql_connect() it may look like you have a version of PHP that lacks the mysql_ library.

The mysql_ library was removed in 7.x, and there are many reasons for that. You either have to rewrite your code using mysqli or PDO, OR downgrade your PHP version to 5.x (although I would strongly advice against this).

Community
  • 1
  • 1
OptimusCrime
  • 14,662
  • 13
  • 58
  • 96