Solved

simple if statement

Posted on 2016-10-20
3
42 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 54

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 54

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

823 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