Richlilley Posted April 19, 2007 Share Posted April 19, 2007 Hello I have asked this before but not really sorted it out. I have set up the datadbase. The searcg function workd fine it finds the products from the Title, but the category list just returns the comment No Products Found. Here is the code On my mysql database i have Category_id in row 4, please help new to php!! Thanks <?php // open connection $connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!"); // select database mysql_select_db($db) or die ("Unable to select database!"); // create query if (! isset($search)) { $query = "SELECT * FROM Products WHERE Category_id = 4"; } else { $query = "SELECT * FROM Products WHERE Title LIKE '%$search%'"; } // execute query $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); // see if any rows were returned if (mysql_num_rows($result) > 0) { // yes // print them one after another ECHO "<table width='90%' height='257' border='0'>"; while($row = mysql_fetch_array($result)) { ECHO "<tr>"; ECHO "<td height='36' colspan='4'><span class='titlestyle'>$row[Title]</span></td>"; ECHO "<tr>"; ECHO "<td colspan='2' rowspan='2'><a href="; echo "javascript:popupWindow('"; echo "productimages/index_PREVIEW_standmenu_0001.jpg',500,400);"; echo "><img src='productimages/$row[5]' width='150' height='150'></a></td>"; ECHO "<td width='68%' height='127' colspan='2'><span class='descriptionstyle'>$row[Description]</span></td>"; ECHO "</tr>"; ECHO "<tr>"; ECHO "<td>$row[Price]</td>"; ECHO "<td><a href='add-to-basket.php?productID=$row[0]&cat=$cat'><img src='images/add-icon.gif' width='95' height='19'></a></td>"; ECHO "</tr>"; ECHO "<tr>"; ECHO "<td colspan='4'><hr></td>"; ECHO "</tr>"; } ECHO "</table>"; } else { // no // print status message echo "<p align='center'><font color='#808080' face='Arial'>No products found.</font></p>"; } // free result set memory mysql_free_result($result); // close connection mysql_close($connection); ?> Quote Link to comment Share on other sites More sharing options...
nikkieijpen Posted April 19, 2007 Share Posted April 19, 2007 set your 4 between single quotes: $query = "SELECT * FROM Products WHERE Category_id = '4'"; Quote Link to comment Share on other sites More sharing options...
suzzane2020 Posted April 19, 2007 Share Posted April 19, 2007 try doin it this way $num=mysql_num_rows($result) if($num!=0) { } else{ } Quote Link to comment Share on other sites More sharing options...
cyrilsnodgrass Posted April 19, 2007 Share Posted April 19, 2007 You say you have category_id in row 4, but the query is searching for a category_id that is equal to 4. Have I mis-understood or am I missing something ? Quote Link to comment Share on other sites More sharing options...
suzzane2020 Posted April 19, 2007 Share Posted April 19, 2007 wud be grt if u cud respond Quote Link to comment Share on other sites More sharing options...
Richlilley Posted April 19, 2007 Author Share Posted April 19, 2007 Hello have tried to change to try doin it this way $num=mysql_num_rows($result) if($num!=0) { } else{ } still the same message unless I am putting in wrong place. Please shed some light:- <?php // open connection $connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!"); // select database mysql_select_db($db) or die ("Unable to select database!"); // create query if (! isset($search)) { $query = "SELECT * FROM Products WHERE Category_id = '4'"; } else { $query = "SELECT * FROM Products WHERE Title LIKE '%$search%'"; } // execute query $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); // see if any rows were returned if ($num=mysql_num_rows($result) > 0) { // yes // print them one after another ECHO "<table width='90%' height='257' border='0'>"; while($row = mysql_fetch_array($result)) { Quote Link to comment Share on other sites More sharing options...
suzzane2020 Posted April 19, 2007 Share Posted April 19, 2007 not this way // see if any rows were returned if ($num=mysql_num_rows($result) > 0) change it to $num=mysql_num_rows($result); if($num!=0) { } SELECT * FROM Products WHERE Category_id = '4'" copy this and paset it in the database n see if u get the record if not try it without the quotes Quote Link to comment Share on other sites More sharing options...
Richlilley Posted April 19, 2007 Author Share Posted April 19, 2007 Hello ah getting a result from mysql of MySQL returned an empty result set (i.e. zero rows). (Query took 0.0003 sec) So I something wrong then!! Quote Link to comment Share on other sites More sharing options...
suzzane2020 Posted April 19, 2007 Share Posted April 19, 2007 is there a row with category_id 4 remove the quotes this time not required fro a numeric value Quote Link to comment Share on other sites More sharing options...
Richlilley Posted April 19, 2007 Author Share Posted April 19, 2007 Hello thanks for your help. Ive got a Products table and a Category table. Category_id is 4th row in the products table. Both tables are linked so a new category is placed in Category table in row 1 as CAT. So what I am trying to get is when a new category is entered and stored, the link on the home page will find all products in that category?? Not sure if this helps Rich Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.