?
Solved

simple if statement

Posted on 2016-10-20
3
Medium Priority
?
55 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 58

Accepted Solution

by:
Julian Hansen earned 2000 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 35

Expert Comment

by:ste5an
ID: 41852000
Shouldn't

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

Open in new window


be sufficient enough?
2
 
LVL 58

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

WordPress Tutorial 3: Plugins, Themes, and Widgets

The three most common changes you will make to your website involve the look (themes), the functionality (plugins), and modular elements (widgets).

In this article we will briefly define each again, and give you directions on how to install them.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
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…
The viewer will learn how to count occurrences of each item in an array.
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.
Suggested Courses

770 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