Jump to content

[SOLVED] IF statement problem


Sorrow

Recommended Posts

Ok here is what is happening I am doing a search option and when I click no the search button I need to display the results. it is working great but when there is no result that was found I need to echo "No Item was found"

Here is the code that I have

 

include 'opendb.php';

$STitle = $_POST[sTitle];
$query  = "SELECT * FROM reviews WHERE m_Title like '%$STitle%' ORDER BY m_Title";
$result = mysql_query($query);

while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
if ($row < 1) {
   echo "<br> No titles found<br><br><a href='http://www.web.com/reveiws1.php'> Back to Reviews</a>";
   
}else { 
echo "<a href='/moviereview.php?index={$row['m_index']}'>{$row['m_Title']} </a><br>" ;
}
}


mysql_close($con);

 

Any suggestion on what I am doing wrong please ??

Link to comment
https://forums.phpfreaks.com/topic/130347-solved-if-statement-problem/
Share on other sites

close, but you are checking in the wrong place

 

include 'opendb.php';

$STitle = $_POST[sTitle];
$query  = "SELECT * FROM reviews WHERE m_Title like '%$STitle%' ORDER BY m_Title";
$result = mysql_query($query);

if(mysql_num_rows($result))
{
  while($row = mysql_fetch_array($result, MYSQL_ASSOC))
  {
    echo "<a href='/moviereview.php?index={$row['m_index']}'>{$row['m_Title']} </a><br>" ;
  }
}
else
{
  echo "<br> No titles found<br><br><a href='http://www.web.com/reveiws1.php'> Back to Reviews</a>";
}

mysql_close($con);

use the mysql_num_rows function to get the number of rows and use that - like below

 

include 'opendb.php';

$STitle = $_POST[sTitle];
$query  = "SELECT * FROM reviews WHERE m_Title like '%$STitle%' ORDER BY m_Title";
$result = mysql_query($query);
$numRows = mysql_num_rows($result);

while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
if ($numRows  < 1) {
   echo "<br> No titles found<br><br><a href='http://www.web.com/reveiws1.php'> Back to Reviews</a>";
   
}else { 
echo "<a href='/moviereview.php?index={$row['m_index']}'>{$row['m_Title']} </a><br>" ;
}
}


mysql_close($con);

use the mysql_num_rows function to get the number of rows and use that - like below

 

include 'opendb.php';

$STitle = $_POST[sTitle];
$query  = "SELECT * FROM reviews WHERE m_Title like '%$STitle%' ORDER BY m_Title";
$result = mysql_query($query);
$numRows = mysql_num_rows($result);

while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
if ($numRows  < 1) {
   echo "<br> No titles found<br><br><a href='http://www.web.com/reveiws1.php'> Back to Reviews</a>";
   
}else { 
echo "<a href='/moviereview.php?index={$row['m_index']}'>{$row['m_Title']} </a><br>" ;
}
}


mysql_close($con);

 

...the IF needs to be OUTSIDE the while, or you will never reach it

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.