I am trying to get this code to do else if there are no more rows to display but not working could anyone point me in the right direction for fixing this.
if($mymethod == "LOADGEMINVENTORY")
{
$result = mysqli_query($con,"SELECT * FROM gemshop_table LIMIT $startrow, 6")or
die(mysql_error());//Pulles six items at a time to display.
if(!mysql_error()){ echo ("DISPLAYING6OFINVENTORY^");
while($row = mysqli_fetch_array($result))
{
echo $row['id'];echo (",");echo $row['image'];echo ("^");
}
}else{echo ("NOINVENTORY");}
}
You're mixing mysqli
with mysql
. You should really use one of these in your code.
If you're using mysqli, you can do the following to get number of rows:
$row_count = $result->num_rows;
if( $row_count == 0 ) {
echo "NOINVENTORY";
}
These is no while/else condition in PHP (or most programming language that I know of).
while() will automatically loop through the data until the end. So in your case, if you want to show something after all the data has been retrieved, you would just show it after the while() loop.
while($row = mysqli_fetch_array($result))
{
echo $row['id'];echo (",");echo $row['image'];echo ("^");
}
echo "Show anything you want after the data above.";
However, it seems to me that you want to show the records when you have data, and NOINVENTORY when there's none. For that, use if() statement with num_rows
if(!mysql_error())
{
if (0 == mysqli_num_rows ($result))
{
echo "NOINVENTORY";
}
else
{
while($row = mysqli_fetch_array($result))
{
echo $row['id'];echo (",");echo $row['image'];echo ("^");
}
}
}