Solved

simple if statement

Posted on 2016-10-20
3
46 Views
Last Modified: 2016-10-20
I'd like to use an if statement before the "while loop" to check if there is a row coming form the database or not.
My code is like this

$sql = "SELECT * FROM  xxxxxx where xxxx='$uid' and yil='$yil' and donem='$donem'";
$result = $conn->query($sql);

then I fetch the data  / i want to use an if statement here before the loop

"if ($row is not empty then fetch the data.) { "
my while loop is:
while($row = $result->fetch_assoc()) {
0
Comment
Question by:Braveheartli
  • 2
3 Comments
 
LVL 55

Accepted Solution

by:
Julian Hansen earned 500 total points
ID: 41851996
Two options
1. Check num_rows

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
       //...
    }
}

Open in new window

2. Use while like this
$row = $result->fetch_assoc(){
while($row) {
  // do stuff with $row
  $row = $result->fetch_assoc(){
}

Open in new window

0
 
LVL 33

Expert Comment

by:ste5an
ID: 41852000
Shouldn't

  while($row = $result->fetch_assoc()) {
       //...
    }

Open in new window


be sufficient enough?
2
 
LVL 55

Expert Comment

by:Julian Hansen
ID: 41852035
@ste5an - it should be - there might be extenuating circumstances though.

@Braveheartli - ste5an is right - if there are no record $row will be a false value and the while will not start.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question