MySQL Forums
Forum List  »  PHP

UNABLE TO SELECT DATABASE
Posted by: Patrick Onyilimba
Date: September 13, 2016 10:20AM

Hi,
After inserting multiple records in a database, I created and ran a " select data" script, expecting it to return some records, but instead I received "undefined variable" and "warning" messages as follows:

"Notice: Undefined variable: result in C:\wamp\www\mysql\select-datapro.php on line 22
Call Stack
# Time Memory Function Location
1 0.0437 130512 {main}( ) ...\select-datapro.php:0

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in C:\wamp\www\mysql\select-datapro.php on line 22
Call Stack
# Time Memory Function Location
1 0.0437 130512 {main}( ) ...\select-datapro.php:0
2 0.1722 140456 mysqli_num_rows ( ) ...\select-datapro.php:22
0 results."


The "select data" code itself is here:
<?php
$servername = "localhost";
$username = "root";
$password = "patony";
$dbname = "myDBPRO";

//Create connection
$conn = mysqli_connect("localhost", "root", "patony", "myDBPRO");

//Check connection
if (!$conn) {
die ("Connection failed: " . mysqli_connect_error() );
}

$sql = "SELECT id, firstname, lastname FROM myGUESTS2";
$results = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
//Output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "id: " . $row["id"]. " - Name" . $row["firstname"] . " " . $row["lastname"] . "<br>";
}
} else {
echo "0 results";
}
mysqli_close($conn);
?>

I don't really understand what the "notice" and "warning" want me to do to fix the error.

Help!

Options: ReplyQuote


Subject
Written By
Posted
UNABLE TO SELECT DATABASE
September 13, 2016 10:20AM
September 13, 2016 11:51AM
September 16, 2016 11:54AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.