Solved

simple if statement

Posted on 2016-10-20
3
30 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 51

Accepted Solution

by:
Julian Hansen earned 500 total points
Comment Utility
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 32

Expert Comment

by:Stefan Hoffmann
Comment Utility
Shouldn't

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

Open in new window


be sufficient enough?
2
 
LVL 51

Expert Comment

by:Julian Hansen
Comment Utility
@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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now