-3

I connected successfully to my database and the I tried to print my data but I can only see the "Connected succesfully" line in the browser. Here is my code:

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

$db=mysqli_connect("host","root","pass","dbase");

if (!$db) {
    echo "Error: Unable to connect to MySQL." . PHP_EOL;
    echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
    echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
    exit;
}
echo "Connected successfully";
$sql = "SELECT username FROM students";
$result = $db->query($sql);

if ($result->num_rows > 0) {
   // output data of each row
   while($row = $result->fetch_assoc()) {
   echo "username: " . $row["username"].  "<br>";
   }
} else {
  echo "0 results";
}
$db->close();
?>

So the issue was that SELinux was blocking remote connections from PHP scripts executed by Apache web server. The setsebool -P httpd_can_network_connect=1 in the terminal did the trick.

Christakitos
  • 146
  • 1
  • 11
  • 1
    What is in the PHP error log? – Peter VARGA Mar 23 '16 at 22:37
  • Simple: You didn't select a database. You're also using the wrong variable `$conn` it's `$db`. – Funk Forty Niner Mar 23 '16 at 22:42
  • If you're having trouble with bugs, why would you suppress error messages with `@`? It seems like such messages would be useful. – Tim Mar 23 '16 at 22:43
  • I am new in php-mysql, let me try your suggestions and i will let you know the results – Christakitos Mar 23 '16 at 22:44
  • @Christakitos Al and I both posted answers for you below quite a long time ago and haven't had a response from you. Whether you saw them, is unknown, so go over them carefully. – Funk Forty Niner Mar 24 '16 at 00:08
  • I did check them and they were very helpful but i found the real issue. I edited the original post with the solution. Thank you for your help. – Christakitos Mar 24 '16 at 00:10
  • @Christakitos Great, but the way Stack works is that when someone provides a solution, an answer should be accepted in order to mark the question as solved. Right now, your question is still considered as open. I myself went through a lot of trouble typing out a rather well-explained answer http://stackoverflow.com/a/36190126/1415724 – Funk Forty Niner Mar 24 '16 at 00:13
  • @Christakitos you also can't go edit your original question and showing correct syntax. People may downvote both our answers because people will say *"There's nothing wrong with the guy's code"*. What you did was wrong. – Funk Forty Niner Mar 24 '16 at 00:14
  • @Fred -ii And this answer helped me a lot, I did find the solution a few minutes ago and then i edited the post for you to know i found it. Don't you see the updates? I'm really sorry i am learning how to use the stack. – Christakitos Mar 24 '16 at 00:17
  • @Fred -ii Ok, thank you for your time and your help. I also did learn a few things for stack from you. – Christakitos Mar 24 '16 at 00:23
  • @Christakitos You're welcome, always glad to help, *cheers* and thanks. – Funk Forty Niner Mar 24 '16 at 00:24

2 Answers2

1

Edit: Note to future readers. The OP edited their question with the corrected syntax after answers were posted and this answer was based on their original post:

and added:

"So the issue was that SELinux was blocking remote connections from PHP scripts executed by Apache web server. The setsebool -P httpd_can_network_connect=1 in the terminal did the trick."

Firstly, you didn't select a database.

and if (!db) needs to be changed to if (!conn) and if ($db->connect_error) to if ($conn->connect_error). The same variable needs to be used throughout your code.

You're also using the wrong variable $db it's $conn (or whatever you want to use, but as I said above, you need to use the same variable for your connection and for the query.

Error reporting http://php.net/manual/en/function.error-reporting.php would have told you about an undefined variable.

Therefore:

$conn = @mysqli_connect("host","root","pass", "your_database");

Sidenote: host I take it is only representative of the host you're using. If you're on your own PC, you would use localhost and accessing that file as http://localhost/file.php instead of file///file.php if that is what you're doing and will not work. The web browser will not parse PHP directives like that and you need to install a webserver/PHP in order for it to work, including MySQL.

Read the manual on connecting:

and remove the @ symbols while testing. They are error suppressors.

Check for errors with http://php.net/manual/en/mysqli.error.php

Example from the manual:

Sidenote: 127.0.0.1 or localhost or if hosted, use the setting they gave you to use.

<?php
$link = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db");

if (!$link) {
    echo "Error: Unable to connect to MySQL." . PHP_EOL;
    echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
    echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
    exit;
}

echo "Success: A proper connection to MySQL was made! The my_db database is great." . PHP_EOL;
echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;

mysqli_close($link);

Add error reporting to the top of your file(s) which will help find errors.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Then the rest of your code

Sidenote: Displaying errors should only be done in staging, and never production.

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
0

You use another variable than you connected to the database!

You connected to $db but perform the query with $conn. Also, in the check if the database connection could be established you do not have the $ before db.

It always helps a lot when you have in a terminal window the PHP error log open. 99% of errors can be fixed by your self.

Peter VARGA
  • 4,780
  • 3
  • 39
  • 75