Solved

simple if statement

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

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 34

Expert Comment

by:ste5an
ID: 41852000
Shouldn't

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

Open in new window


be sufficient enough?
2
 
LVL 57

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
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.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

695 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