0

how would i get this to display a message in place of the Query if no result is found i updated the code but its just showing "N"

<?php

$hostname = "...";
$username = "";
$password = "";
$db = "";

$dbconnect=mysqli_connect($hostname,$username,$password,$db);

if ($dbconnect->connect_error) {
  die("Database connection failed: " . $dbconnect->connect_error);
}
$query=mysqli_query($dbconnect,"SELECT DISTINCT companyname,client_id,feedback,status from review WHERE status=1 ORDER BY RAND() LIMIT 4");
$rows_get = mysqli_num_rows($query);

if ($rows_get >0)
{
$query2=mysqli_query($dbconnect,"SELECT DISTINCT companyname,client_id,feedback,status from review WHERE status=1 ORDER BY RAND() LIMIT 4");
   $row1 = mysqli_fetch_assoc($query2);
   $row2 = mysqli_fetch_assoc($query2);
   $row3 = mysqli_fetch_assoc($query2);
   $row4 = mysqli_fetch_assoc($query2);
   $row5 = mysqli_fetch_assoc($query2);

}else {
   $row1 = "N0 Data";
   $row2 = "N0 Data";
   $row3 = "N0 Data";
   $row4 = "N0 Data";
   $row5 = "N0 Data";
}
?>
4
  • Use a conditional statement on num_rows() instead of what you have now. Commented Nov 21, 2018 at 3:12
  • i tried and it just bugs out Commented Nov 21, 2018 at 3:27
  • I can't see it failing; see what the answer holds below. That's how it's done. If that didn't work, then you'll have to tell them. Commented Nov 21, 2018 at 4:03
  • ya i'll have to see why i'm still new to mysql and php so i'm like scratching my head over this Commented Nov 21, 2018 at 4:12

1 Answer 1

1

Do as follows: After $query insert this:

$rows_get = mysqli_num_rows($query);

if ($rows_get >0)
{
//do all database operation
}else {
 echo " No data found";
}

Hope this helps.

Amend your code for example..

if ($row_get>0){
//i assume you are getting multiple rows
 while ($data =mysqli_fetch_assoc ($query))
{
//run this loop and you will get all you rows.
}
}
Sign up to request clarification or add additional context in comments.

3 Comments

i'm just getting this link i updated the code at the top i'm also useing a few different variables <?php echo $row1['client_id']; ?> <?php echo $row1['feedback']; ?> and <?php echo $row1['companyname']; ?>
No you are doing it wrong. No need to run the query again in if else clause. Also do you getting single row or multiple rows from your query. Explain so that we can answer your queries.
do you have discord or something i'm just confused i have another post on this issue stackoverflow.com/questions/53426712/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.