• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 233
  • Last Modified:

if statement, do you see anything wrong with this?

hi guys,
i want to add an if statement to my code,
basically if the query, doesnt return anything, i want to show an error msg,

<?php
if(count($rows) == 0)
 {
 echo '<div align="center"><font color="#FF3300">Sorry, no artist was found. Please try a different category.
 </font></div><br>';
 }
else {
}
?>
thanks
0
djsoltan
Asked:
djsoltan
  • 7
  • 6
1 Solution
 
Harisha M GCommented:
Hi, you need to use mysql_num_rows()...

<?php
    $result = mysql_query(....);

    if(mysql_num_rows($result) == 0)
    {
        echo '<div align="center"><font color="#FF3300">Sorry, no artist was found. Please try a different category. </font></div><br>';
    }
    else
    {
    }
?>



---
Harish
0
 
djsoltanAuthor Commented:
hi harish, thanks for your help.
can you give me more hin on this line?
---
$result = mysql_query(....);
---
i use the url to pass info, for example.
mysite.com/index.php?artist=m
so this basically looks up all the artists that their names start with m
now if it doesnt find anything under m, i want that error to pup up
any idea what needs to be between the (...)?
thank you
0
 
Harisha M GCommented:
djsoltan, it depends on what "$rows" means. Can you post your code ?
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
djsoltanAuthor Commented:
here harish, does this help?  

 $query   = "SELECT * FROM video WHERE artist=".$_GET['artist'];
    $result  = mysql_query($query) or die('Error : ' . mysql_error());  
    $row     = mysql_fetch_array($result, MYSQL_ASSOC);  
0
 
Harisha M GCommented:

 $query   = "SELECT * FROM video WHERE artist=".$_GET['artist'];
    $result  = mysql_query($query) or die('Error : ' . mysql_error());  

    if(mysql_num_rows($result) == 0)
    {
        echo '<div align="center"><font color="#FF3300">Sorry, no artist was found. Please try a different category. </font></div><br>';
    }
    else
    {
    }
   

    $row     = mysql_fetch_array($result, MYSQL_ASSOC);  
0
 
djsoltanAuthor Commented:
it shows the error, in both cases ( wheather the number of rows are zero or if its not )
let me copy paste all my code, this might help.
p.s i just noticed that i looked at the wrong code for the $row so ignore my previous code please.
here are the codes
-------------------------------------------
artist.php ( this is where the error msg should popup)


<? require('include/pagedresults.php'); ?>
<? require('include/sort-artists.php'); ?>

<html>
<head>
</head>
<body bgcolor="#F5F5F5" onLoad="MM_preloadImages('images/watch.gif','images/all-video-blue.gif')">
<div align="center">
<table width="756" height="68" border="0" align="center" class"body" id="table">
  <tr valign="top" bgcolor="#FFFFFF">
    <td valign="top" height="6%" bgcolor="#F5F5F5"><div align="center"><? show_header(); ?>
      <? show_header_ad(); ?>
    </div></td>
  </tr>
  <tr valign="top">
    <td height="20" bgcolor="#F5F5F5"><div align="right">
      <div align="center">        
        <table width="100%" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td width="83%" valign="top"><table width="601" border="0" align="center" cellpadding="0" cellspacing="0">
              <tr>
                <td width="594" height="49" align="left"><table width="603" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="71828a" background="../images/background.jpg" bgcolor="#000000">
                    <tr class="body">
                      <td width="520"><div align="left" class="style6">Browse by artist</div></td>
                    </tr>
                  </table>
                    <img src="" width="0" height="0" alt=""></td>
              </tr>
              <tr>
                <td><div align="center">
                    <table width="100%" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#CCCCCC" background="../images/background.jpg">
                      <tr bgcolor="#C6E2FF">
                        <td width="608" align="center" valign="middle" background="../images/background.jpg" bgcolor="#C6E2FF"><div align="left" class="style6">
                            <div align="center">Artists starting with the letter (<?php echo htmlspecialchars($_GET['artist']); ?>)</div>
                        </div></td>
                      </tr>
                    </table>
                </div></td>
              </tr>
              <?php while ($row = $rs->fetchArray()): ?>
              <?
$i++;
if($i==1){
     $bgclr="#FFFFFF";
}else{
    $bgclr="#eeeeee";
    $i=0;
}
          ?>
              <tr>
                <td><div align="center">
                    <table width="100%" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#FFFFFF" bgcolor="#FFFFFF">
                      <tr bgcolor="<?=$bgclr?>" >
                        <td bgcolor="<?=$bgclr?>" width="70" height="39" align="center" valign="middle"><p align="center"> <a href="more-videos.php?artist=<?=$row['artist']?>"><img src="<?=$row['image']?>" border="0"></a></p></td>
                        <td bgcolor="<?=$bgclr?>" width="332" align="left" valign="middle"><div align="center"><a href="<?=$row['url']?>"> </a>
                              <?=$row['artist']?><a href="<?=$row['url']?>"></a> <font color="#FF6600"><strong> </strong></font></div></td>
                        <td align="center" valign="middle" bgcolor="<?=$bgclr?>"><a href="player.php?id=<?=$row['id']?>" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('id','','images/watch.gif',1)"></a><a href="player.php?id=<?=$row['id']?>" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('id','','images/watch.gif',1)"></a><a href="more-videos.php?artist=<?=$row['artist']?>" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('allview<?=$row['id']?>','','images/all-video-grey.gif',0)"></a><a href="more-videos.php?artist=<?=$row['artist']?>" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('allview<?=$row['id']?>','','images/all-video-blue.gif',1)"><img src="images/all-video-grey.gif" name="allview<?=$row['id']?>" width="195" height="20" border="0"></a></td>
                        </tr>
                    </table>
                </div></td>
              </tr>
              <?php endwhile; ?>
                    


            </table>
              <table width="600" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#000000">
                <tr class="body">
                  <td width="260" background="../images/background.jpg"><div align="left" class="style6">Page Navigation </div></td>
                  <td width="260" background="../images/background.jpg"><span class="style6">
                    <?=$rs->getPageNav("artist={$_GET['artist']}")?>
                  </span></td>
                </tr>
              </table>                   
                  
                   <?php
 $query   = "SELECT * FROM video WHERE artist=".$_GET['artist'];
    $result  = mysql_query($query) or die('Error : ' . mysql_error());  

    if(mysql_num_rows($result) == 0)
    {
        echo '<div align="center"><font color="#FF3300">Sorry, no artist was found. Please try a different category. </font></div><br>';
    }
    else
    {
    }
   

    $row     = mysql_fetch_array($result, MYSQL_ASSOC);  

                  ?>
              </td>
            <td width="17%"><div align="center">

              </div>
            </td>
          </tr>
        </table>
      </div>
      </div>      </td>
  </tr>
  <tr valign="top" bgcolor="#FFFFFF">
    <td height="7%" bgcolor="#F5F5F5"><div align="center">
      <? show_back(); ?>
      <? show_footer(); ?>
</div></td>
  </tr>
</table>
</div>
</body>
</html>

---------------
include file ( sort-artist.php)
<?
if(!isset($_GET['artist']))
{
$query = "select * from video ORDER BY id DESC";
}
// if artist is there //
else
{
// get the artist info from database
$query   = "SELECT artist,image,id FROM video WHERE artist LIKE '".$_GET["artist"]."%'GROUP BY artist DESC ORDER BY artist ASC";
}
$rs = new MySQLPagedResultSet($query,10,$cnx);
     
?>
----------
include file ( pageresults.php )
<?php
class MySQLPagedResultSet
{

  var $results;
  var $pageSize;
  var $page;
  var $row;
 
  function MySQLPagedResultSet($query,$pageSize,$cnx)
  {
    $resultpage = $_GET['resultpage'];
   
    $this->results = @mysql_query($query,$cnx);
    $this->pageSize = $pageSize;
    if ((int)$resultpage <= 0) $resultpage = 1;
    if ($resultpage > $this->getNumPages())
      $resultpage = $this->getNumPages();
    $this->setPageNum($resultpage);
  }
 
  function getNumPages()
  {
    if (!$this->results) return FALSE;
   
    return ceil(mysql_num_rows($this->results) /
                (float)$this->pageSize);
  }
 
  function setPageNum($pageNum)
  {
    if ($pageNum > $this->getNumPages() or
        $pageNum <= 0) return FALSE;
 
    $this->page = $pageNum;
    $this->row = 0;
    mysql_data_seek($this->results,($pageNum-1) * $this->pageSize);
  }
 
  function getPageNum()
  {
    return $this->page;
  }
 
  function isLastPage()
  {
    return ($this->page >= $this->getNumPages());
  }
 
  function isFirstPage()
  {
    return ($this->page <= 1);
  }
 
  function fetchArray()
  {
    if (!$this->results) return FALSE;
    if ($this->row >= $this->pageSize) return FALSE;
    $this->row++;
    return mysql_fetch_array($this->results);
  }
 
  function getPageNav($queryvars = '')
  {
  $nav = '';

    if (!$this->isFirstPage())
           {
           $nav .= "<a href=\"?resultpage=".
         ($this->getPageNum()-1).'&'.$queryvars.'">&nbsp;Prev&nbsp;</a>';
                }       
             
      if ($this->getNumPages() > 1)
      for ($i=1; $i<=$this->getNumPages(); $i++)
      {
   // Active
        if ($i==$this->page)
           $nav .= "$i&nbsp;";
        
   //numbers
       else
      $nav .= "<a href=\"?resultpage={$i}&".$queryvars."\">&nbsp;$i&nbsp;</a> ";
      }
           //previous
        
      
        //next    

    if (!$this->isLastPage())
    {
      $nav .= "<a href=\"?resultpage=".
              ($this->getPageNum()+1).'&'.$queryvars.'">&nbsp;Next&nbsp;</a> ';
    }
   
    return $nav;
  }
}
  ?>
--------------------------
0
 
Harisha M GCommented:
djsoltan, what error did it show ?

Is it a PHP error or MySQL error ?

I think your query is wrong.. try this:

 $query   = "SELECT * FROM video WHERE artist LIKE '".$_GET['artist']."'";
0
 
djsoltanAuthor Commented:
before it was showing,
--
Error : Unknown column 'm' in 'where clause'
--

now i dont see an error anymore with the new query you provided, but still, it doesnt matter if there are any rows returned or not, the error popsup :-s
check the link below,

----
link with rows returned:
http://70.86.133.233/mv/artists.php?artist=m

---
link for no rows returned,
http://70.86.133.233/mv/artists.php?artist=z
0
 
Harisha M GCommented:
Is there any artist by name "m" ? or is it just the starting letter ? Try giving the complete artist name

In the "else" part, write the code to show the result..
0
 
djsoltanAuthor Commented:
ohhhhhhhhhhhhhhhhhhhhhh,
i see what you mean,, i noticed if i give it the full aritst name, it works.
but what i want it to do is to go with the first letter.
so checks to see if the artist with the first letter exists or not, if not echo the error.
is that possible?
0
 
Harisha M GCommented:
Try this query then..

$query   = "SELECT * FROM video WHERE artist LIKE '".$_GET['artist']."%'";
0
 
djsoltanAuthor Commented:
omgggggggggggggggggggg,
i love you man,,
thank you veryyyyyyyyyyyy much,,,,,,,,,,,,,,,,,,,,,,,,,
>:D<
0
 
Harisha M GCommented:
Glad I could help..

Thanks for the grade and complements :o)


0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now