473,480 Members | 1,850 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Printing query result from MySQL

49 New Member
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
7 3002
Purple
404 Recognized Expert Contributor
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
Muddasir
49 New Member
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
1,584 Recognized Expert Top Contributor
<?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
404 Recognized Expert Contributor
Thanks Ajaxrand - took me a little longer to work out
Jun 13 '07 #5
Muddasir
49 New Member
many thanks Purple and Ajaxrand
its working now..
u guys are great
thanks once again
Jun 13 '07 #6
ak1dnar
1,584 Recognized Expert Top Contributor
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
3,237 Recognized Expert Specialist
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

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

Similar topics

13
2857
by: dogu | last post by:
Noob alert. Code is below. File is saved as a .php. What I'm trying to do: User uses 'select' box drop down list to pick a value. Value ($site) is derived from a db query. This works fine....
2
3408
by: jaysonsch | last post by:
Hello! I am having some problems with a database query that I am trying to do. I am trying to develop a way to search a database for an entry and then edit the existing values. Upon submit, the...
0
2432
by: Doug Reese | last post by:
hello, i have what seems to me a very common operation i'm performing. i need to find the balance on an invoice. i was not having any problems until the production server was upgraded to mysql...
4
3476
by: Bob Bedford | last post by:
We have no access to a mysql NG on my provider's server, so we ask here: We have a long query (long in text) with a UNION between 2 select. We have been informed that some times the query...
2
2051
by: chuy08 | last post by:
Basically I am using PHP 5.1.2 with Apache 2.0.5 on a FreeBSD 5.4 box with Mysql 4.1.1 running. I am attempting to write information to a Mysql table called Jabber. I can connect successfully,...
0
2202
by: taras.di | last post by:
Hi everyone, I've come across an extremely strange problem. The exact same query in both mysql command line client, and mysql query browser gives entirely different results. I was hoping someone...
1
3888
by: JackM | last post by:
I'm not sure if this qualifies as a mysql or a php question so I'm asking in both groups. I am pulling the results of a mysql query from my database and want to print the results into a two...
10
2629
by: Lloyd Harold | last post by:
I'm very new to PHP and attempting to put together a simple script for retrieving MySQL data of personal records. The MySQL table I'm using consists of: 0: id 1: name 2: location (an integer...
3
3113
by: jmooney5115 | last post by:
Hey. I am new to php and am trying to learn. What I'm doing is querying(did I spell this right?) a mySQL database and putting the results into a table on a webpage. I have worked for hours on this...
0
7041
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7044
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7084
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6739
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
4779
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4481
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2984
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
563
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.