I think, I might need help with my PHP code...
I'm trying to echo the info in a MySQL database and it gives me the Error: Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /usr/local/ampps/www/php/db.php on line 18
My code is:
<?php
$servername = "blabla"; //changed, connecting works
$username = "blabla";
$password = "blabla";
$database = "blabla";
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} else {
echo "Connection success \n";
}
$sql = mysql_query("SELECT * FROM Schueler");
while($data = mysql_fetch_array($sql)) //This is line 18...
{
echo "ID: " . $data['ID'] . " Vorname: " . $data['Vorname'] . " Nachname: " . $data['Nachname'] . " Klasse: " . $data['Klasse'] . "<br>";
}
$conn->close();
?>
Would be nice if somebody could help me :)
Edit:
I fixed it using MySqli only, this is my working code now:
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} else {
echo "Connection success \n";
}
$sql = "SELECT ID, Vorname, Nachname FROM Schueler";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["ID"]. " - Name: " . $row["Vorname"]. " " . $row["Nachname"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
Thanks for the quick advice.