连接到mysql数据库但无法打印数据[重复]

This question already has an answer here:

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.

</div>

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.

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.