-1

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.

Bob
  • 23
  • 3
  • seems you are not connected to db – ScaisEdge Oct 12 '17 at 13:52
  • It says "Connection success" a line above the error as i implemented into the code so i sort of connected to the database i think. – Bob Oct 12 '17 at 13:54
  • 2
    you are using `mysqli` and then fetching array using `mysql_fetch_array`. Shouldn't these two be the same? – shahsani Oct 12 '17 at 13:54
  • you are not properly connected you are using mysqli for connection and mysql_fecth for fetching these are two different db drivers use mysqli only – ScaisEdge Oct 12 '17 at 13:55
  • What would be the correct code then? Im not really familiar with sql and php yet... – Bob Oct 12 '17 at 13:56

1 Answers1

-1

You have used mysqli for your connection setup, then you used simple mysql functions to get the resultset and the fetch_array logic. You need to be uniform in this case.

I have changed the mysql_query() call to mysqli_query() call, and similarly the mysql_fetch_array() call to mysqli_fetch_array() call.

The final code becomes:

$sql = mysqli_query("SELECT * FROM Schueler", $conn);
while($data = mysqli_fetch_array($sql)) 
{
echo "ID: " . $data['ID'] . " Vorname: " . $data['Vorname'] . " Nachname: " . $data['Nachname'] . " Klasse: " . $data['Klasse'] . "<br>";
}
shahsani
  • 687
  • 7
  • 16
  • Warning: mysqli_query() expects at least 2 parameters, 1 given in /usr/local/ampps/www/php/db.php on line 17 __________ Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in /usr/local/ampps/www/php/db.php on line 18 __________ This way im just ending up with another warning more. – Bob Oct 12 '17 at 13:58
  • Use the edited code, as above. – shahsani Oct 12 '17 at 14:00