473,395 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Printing query result from MySQL

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 3000
Purple
404 Expert 256MB
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
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 Expert 1GB
<?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 Expert 256MB
Thanks Ajaxrand - took me a little longer to work out
Jun 13 '07 #5
many thanks Purple and Ajaxrand
its working now..
u guys are great
thanks once again
Jun 13 '07 #6
ak1dnar
1,584 Expert 1GB
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 Expert 2GB
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
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
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
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
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
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
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
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
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
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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.