By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,742 Members | 1,227 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,742 IT Pros & Developers. It's quick & easy.

Printing query result from MySQL

P: 49
i am having problem in printing the query result from MySQL db...

actually i am developing a very simple search module.
when the user select category from the given categories in drop down list....the coresponding product against that category shuld be displayed....

table= search

fields

product_id, product_name, category_name, price
Expand|Select|Wrap|Line Numbers
  1. <?
  2. $cat=$_POST['category']
  3.  
  4. $query = "SELECT product_name FROM search WHERE category_name=$cat";
  5.  
  6. $result=mysql_query($query);
  7.  
  8. echo "the products are:'$res'";
  9.  
  10. ?>
[Please use CODE tags when posting source code. Thanks! --pbmods]
Jun 13 '07 #1
Share this Question
Share on Google+
7 Replies


Purple
Expert 100+
P: 404
Hi Muddasir and welcome to TSDN,

I have taken a look at your code and whilst you have done the mysql_query, you appear to be missing the fetch.. This maybe due to you snippet missing the relevent line of code. If that the case, can you post the code with the query and result processing logic.. If its not the case a broad pointer would be to take a look at mysql_result and mysql_fetch functions in the php.net manual..

Hope this helps

Purple
Jun 13 '07 #2

P: 49
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. include("datastore.php");
  3.  
  4. $cat=$_POST["category"];
  5.  
  6.  
  7. $query = "select * from search";
  8. $result=mysql_query($query);
  9. $num=mysql_numrows($result);
  10.  
  11.  
  12. $flag=0;
  13. $i=0;
  14.  
  15. while($i<$num)
  16. {
  17.     $cat_var=mysql_result($result,$i,"category_name");    
  18.  
  19.     If($cat==$cat_var) 
  20.  
  21.     $flag=1;
  22.     $i++;
  23. }
  24.  
  25. if ($flag==1)
  26. {
  27.  
  28. $query = "SELECT product_name FROM search WHERE category_name=$cat";
  29.  
  30. $result=mysql_query($query);
  31. echo "the result is:'$res'";
  32.  
  33. }
  34. else
  35. {
  36.  
  37. echo "no reult found";
  38.  
  39. }
  40. ?>
i dont know why i am unable to print the result of that query.......shall i use mysql_fetch()
Jun 13 '07 #3

ak1dnar
Expert 100+
P: 1,584
<?php
include("datastore.php");

$cat=$_POST["category"];


$query = "select * from search";
$result=mysql_query($query);
$num=mysql_numrows($result);


$flag=0;
$i=0;

while($i<$num)
{
$cat_var=mysql_result($result,$i,"category_name");

If($cat==$cat_var)

$flag=1;
$i++;
}

if ($flag==1)
{

$query = "SELECT product_name FROM search WHERE category_name=$cat";

$result=mysql_query($query);
echo "the result is:'$res'";

}
else
{

echo "no reult found";

}
?>

i dont know why i am unable to print the result of that query.......shall i use mysql_fetch()
Expand|Select|Wrap|Line Numbers
  1. # Rest of the code
  2. $query = "SELECT product_name FROM search WHERE category_name='$cat'";// Make sure to use ' ' if this $cat is not a int value. 
  3.  
  4. $result=mysql_query($query);
  5. while($result_row = mysql_fetch_assoc($result))
  6. {
  7. echo "the result is :".$result_row['product_name']."<br>";
  8. }
  9. # Rest of the code
  10.  
Please wrap your codings with relevant [code] tags.
Jun 13 '07 #4

Purple
Expert 100+
P: 404
Thanks Ajaxrand - took me a little longer to work out
Jun 13 '07 #5

P: 49
many thanks Purple and Ajaxrand
its working now..
u guys are great
thanks once again
Jun 13 '07 #6

ak1dnar
Expert 100+
P: 1,584
many thanks Purple and Ajaxrand
its working now..
u guys are great
thanks once again
You are welcome Muddasir and keep in touch with TSDN !

Purple, I appreciate your Post
Thanks,
Ajaxrand
Jun 13 '07 #7

Motoma
Expert 2.5K+
P: 3,235
If you find yourself doing more work with PHP and MySQL, you may want to take a look at the article I wrote on building a MySQL class to make performing these common functions much more accessible.
Jun 13 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.