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); ?> Link to comment https://forums.phpfreaks.com/topic/47729-please-can-somebody-help/ 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'"; Link to comment https://forums.phpfreaks.com/topic/47729-please-can-somebody-help/#findComment-233120 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{ } Link to comment https://forums.phpfreaks.com/topic/47729-please-can-somebody-help/#findComment-233121 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 ? Link to comment https://forums.phpfreaks.com/topic/47729-please-can-somebody-help/#findComment-233123 Share on other sites More sharing options...
suzzane2020 Posted April 19, 2007 Share Posted April 19, 2007 wud be grt if u cud respond Link to comment https://forums.phpfreaks.com/topic/47729-please-can-somebody-help/#findComment-233137 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)) { Link to comment https://forums.phpfreaks.com/topic/47729-please-can-somebody-help/#findComment-233138 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 Link to comment https://forums.phpfreaks.com/topic/47729-please-can-somebody-help/#findComment-233144 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!! Link to comment https://forums.phpfreaks.com/topic/47729-please-can-somebody-help/#findComment-233152 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 Link to comment https://forums.phpfreaks.com/topic/47729-please-can-somebody-help/#findComment-233159 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 Link to comment https://forums.phpfreaks.com/topic/47729-please-can-somebody-help/#findComment-233172 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.