php if num_rows

Hi All,

I'm going crazy with an IF statement.

This is my working code
if ($result3 = $conn->query($sql3)) { 
while($row3 = $result3->fetch_assoc()) { 
echo $row3["photopath"]; 
} 
}

Open in new window

and i'm trying to say
if ($row3 ->num_rows === 0) {
echo 'ProfilePics/Like.png'; }
else
<RUN WORKING CODE ABOVE>

Open in new window


Any help would be greatly appreciated.

Cheers,

Pete
universalmonsterAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Karthik RamachandranCommented:
What exactly you are trying to achieve? Is there an error in this code?
Julian HansenCommented:
Based on your post I am assuming this comes first
if ($row3 ->num_rows === 0) {
echo 'ProfilePics/Like.png'; }
else

Open in new window

In which case Where is $row3 being set?
Dave BaldwinFixer of ProblemsCommented:
You're using the wrong item to try to get 'num_rows'.  It should be like this.  Note that if the number of rows is 0, doing $result3->fetch_assoc() won't work.
$number_rows = $result3->num_rows;

Open in new window

Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Ray PaseurCommented:
How to use the MySQL database, showing MySQL, MySQLi and PDO extensions.  Code examples are included.
http://www.experts-exchange.com/articles/11177/PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html
William NettmannPHP Web DeveloperCommented:
$result3 = $conn->query($sql3);

  if ($result3->num_rows === 0) {
    echo 'ProfilePics/Like.png'; 
  } else {
    while($row3 = $result3->fetch_assoc()) { 
    echo $row3["photopath"]; 
    } 
  }

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Vimal DMSenior Software EngineerCommented:
HI,

You need to change the variable names and conditions as follows,

if ($result3 = $conn->query($sql3)) { 

	if ($result3->num_rows === 0) {
		echo 'ProfilePics/Like.png'; 
		exit;
	}else{
		while($row3 = $result3->fetch_assoc()) { 
			echo $row3["photopath"]; 
		} 
	}

}

Open in new window

Ray PaseurCommented:
The problem with testing $result3->num_rows for zero is that this assumes two things, to wit, that $result3 is an object, and that it was created by a query that succeeded.  A better strategy might be to test the results of the query before using data that might or might not have been created by a successful or failing query.  It's all in the article, with code samples.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.