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
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);

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.