0

I am getting the error:

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in /home/mjcrawle/public_html/home/index.php on line 23

Line 23 turns out to be $num_results = mysqli_num_rows($result); but I am thinking the error is further up but I am having trouble finding it.

The actual code that I am using to connect to the DB is (I do understand there is a redundancy if the database cannot connect):

Any help would be wonderful and a reason for the error would be awesome!

/*Connect To DB*/
$conn = mysqli_connect($host, $user, $pwd)
        or die("Could not connect: " . mysql_error()); //connect to server
    mysqli_select_db($conn, $database)
        or die("Error: Could not connect to the database: " . mysql_error());

    /*Check for Connection*/
    if(mysqli_connect_errno()){
        /*Display Error message if fails*/
        echo 'Error, could not connect to the database please try again later.';
    exit();
    }

/* Query for states */
$query = "SELECT StateAbbreviation, StateName, FROM USState ORDER BY StateName";
$result = mysqli_query($conn, $query);
$num_results = mysqli_num_rows($result);
?>

3 Answers 3

3

You have an extra comma before the FROM in query = "SELECT StateAbbreviation, StateName, FROM USState ORDER BY StateName";, you may be getting an error and not having a result when you execute the query.

Sign up to request clarification or add additional context in comments.

Comments

0

If the query fails, mysqli_query returns boolean false

After $result = mysqli_query($conn, $query);, you should test the return value before continuing:

if ( ! $result){
   $error = mysqli_error($conn);
   //do something with the error message
}

See EmCo's answer for why your query is failing.

1 Comment

better to check if ($result !== FALSE). If for whatever reason mysqli_query could return a 0, your test would indicate it failed, though only an actual boolean FALSE indicates that.
0

<?php $con=mysqli_connect('localhost','root','','dbname') or die ("Connection Failed"); ?>

This is the simple method of DB Connection

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.