0

I'm trying to find the best way to see if mysqli has results or not. I want the code to execute if it has a valid query and to do nothing if not.

Here is the code I tried that does not work:

$result = mysqli_query($con,"SELECT * FROM promoCode where code = '$code'")
    or die("Error in mySql statement!");

if (mysqli_num_rows($result)==1) { echo "yahoo it worked! "; }
Chris Kempen
  • 9,321
  • 5
  • 38
  • 52
Papa De Beau
  • 3,546
  • 17
  • 76
  • 133

1 Answers1

6

==1 checks for exactly 1 record.

What you need is

if (mysqli_num_rows($result) >= 1 ) { echo "yahoo it worked! "; }

Check the documentation here

karthikr
  • 92,866
  • 25
  • 190
  • 186
  • 1
    Check the example in the reference link (Example #2 Procedural style). You are missing the query. – karthikr Sep 30 '13 at 20:52