Avatar of nikaotech
nikaotech
 asked on

If MYSQLi result = "0", include x...

Hello Experts,

I'm trying to dynamically place one of two menus on a page based on the result of a select statement.  It's actually working, but it's also throwing a couple of errors at the same time.  I am basically checking to see if the value of "dependent_id" is "0" or not.  That field is an integer with values from 0-12. If the value is "0" I need to include "menu1.php".  If it's anything else, "menu2.php".  The determination of which menu to use is working correctly, but as I mentioned, it's also throwing these two errors:

 
Error 1:
Warning: mysqli_fetch_row() expects parameter 1 to be mysqli_result, string given on line:
      while($row=mysqli_fetch_row($result))


Error 2:
Warning: mysqli_free_result() expects parameter 1 to be mysqli_result, string given on line:
      mysqli_free_result($result);


Here is that entire section...
<?php 
 $host = "localhost";
 $username = "user";
 $password = "pass";
 $db_name = "db";
 $db = new mysqli($host, $username, $password, $db_name);
 
 if(mysqli_connect_errno()) {
     echo "Error: Could not connect to database.";
     exit;
 }
		$query="select dependent_id from clients WHERE client_id='".$mysqli->real_escape_string($_REQUEST['client_id'])."'";
		$result = $db->query($query);

			while($row=mysqli_fetch_row($result))
				{  
				if($result = "0"){
					 include("menu1.php");
					}
				else
					{
					include("menu2.php");
					}
				}
		mysqli_free_result($result); 
		$db->close();
?>

Open in new window


Many thanks for any help!
PHPMySQL Server

Avatar of undefined
Last Comment
nikaotech

8/22/2022 - Mon
Pyromanci

This is normally typical when the sql did not run properly.

I would start by surrounding the while with a if.

<?php 
 $host = "localhost";
 $username = "user";
 $password = "pass";
 $db_name = "db";
 $db = new mysqli($host, $username, $password, $db_name);
 
if(mysqli_connect_errno()) {
 echo "Error: Could not connect to database.";
 exit;
}
$query="select dependent_id from clients WHERE client_id='".$mysqli->real_escape_string($_REQUEST['client_id'])."'";
$result = $db->query($query);

if ($result) {
	while($row=mysqli_fetch_row($result))
	{  
		if($result = "0"){
			include("menu1.php");
		}
		else
		{
			include("menu2.php");
		}
	}
	mysqli_free_result($result); 
} else {
	echo "Mysql Error Message: ". $mysqli->error ."<br>";
	echo "Query: ".$query;
}
$db->close();
?>

Open in new window


Then if the result is not successful echo out the error and the offending query.
nikaotech

ASKER
Hmm, oddly enough, I got the same exact result.  I had to swap out the connection vars $db with $mysqli and insert my connection values, but other than that I copied your suggestion exactly as is...
Ray Paseur

This article shows some ways to test for success or failure of a query.
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html

$query="select dependent_id from clients WHERE client_id='".$mysqli->real_escape_string($_REQUEST['client_id'])."'";
$result = $db->query($query);

Open in new window

You would probably want to test $result to see what it contains.  Maybe use var_dump() to look at it.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
COBOLdinosaur

I think you are getting an sql error so you need to capture the error codes:

$result = $db->query($query);
if (!$result)
{
    $err
    = "QUERY FAIL: "
    . $query
    . ' ERRNO: '
    . $mysqli->errno
    . ' ERROR: '
    . $mysqli->error
    ;
    error_log($err);
    die($err);
}

I borrowed that from Ray's article.

Cd&
Ray Paseur

Then the next thing to do would be to decide what variable you really want to test.  Probably line 3 does not contain the if() statement you really want.  Maybe you want to be looking at $row['dependent_id'] instead?
			while($row=mysqli_fetch_row($result))
				{  
				if($result = "0"){
					 include("menu1.php");
					}
				else
					{
					include("menu2.php");
					}
				}

Open in new window

Dave Baldwin

This page http://www.php.net/manual/en/mysqli-result.fetch-row.php seems to indicate that you can't mix object oriented and procedural style code in 'mysqli'.  So if everything else is working, this line...
while($row=mysqli_fetch_row($result))

Open in new window

Should probably be...
 while ($row = $result->fetch_row())

Open in new window

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
nikaotech

ASKER
Still working on this, but I wanted to say thank you so very much to all who are contributing.

Ray, I just used var_dump($result) as you have taught me to be so very valuable in the past, and alas, my query is returning the result I expected.  The record I'm testing with is a "Parent/Guardian", so its "dependent_id" is = 0.

Thus, to test the result output, I just echoed the results:
if ($result) {
      while($row=mysqli_fetch_row($result))
      {  
            if($result = "0"){
                  echo "Parent/Guardian";
                  //include("menu1.php");
            }
            else
            {
                  echo "Child/Dependent";
                  //include("menu2.php");
            }
      }

This echoed Child/Dependent.  Which is strange, because the dependent_id = 0.

var_dump ($result); returns this: string(1) "0"
echo $query; returns this: select dependent_id from clients WHERE client_id='1'
echo $result; returns this: 0

I'm moving on down the list to COBOL's input now and will report back...
Ray Paseur

if($result = "0"){

Open in new window

In PHP a single equal sign is an assignment statement.  A test for equality is a double equal sign.  A test for identicality is a triple equal sign.  So this is probably not the if() statement you want, right?
nikaotech

ASKER
OK so I added the second = and now have this error instead:

Catchable fatal error: Object of class mysqli_result could not be converted to string on:

echo $result;

So the two original errors are gone and there is a new error, but the query is still returning a 0...?
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Dave Baldwin

As I said above... you are mixing oop and procedural versions of the 'mysqli' code.  The '$result' you get is Not the same in the two versions.  I was just running the same experiment with echo $result; and got the same response as you did.  

This page http://www.php.net/manual/en/class.mysqli-result.php shows the 'result objects' that you can use in oop.
ASKER CERTIFIED SOLUTION
Member_2_248744

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
nikaotech

ASKER
That did the trick Slick!  I've been working on this for hours...THANKS SO MUCH GUYS!!!
Ray Paseur

You might want to get some foundation in how PHP object-oriented programming works.  In particular, there are established ways of building test cases for OOP projects.  This book will be a big help to you.
http://www.amazon.com/Objects-Patterns-Practice-Experts-Source/dp/143022925X/
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
nikaotech

ASKER
Thanks Ray! Yes indeed, while I am learning through this build, I will ultimately have to be able to support this site, so thanks for the book lead.