473,545 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
+ 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 3007
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.......sha ll i use mysql_fetch()
Jun 13 '07 #3
ak1dnar
1,584 Recognized Expert Top Contributor
<?php
include("datast ore.php");

$cat=$_POST["category"];


$query = "select * from search";
$result=mysql_q uery($query);
$num=mysql_numr ows($result);


$flag=0;
$i=0;

while($i<$num)
{
$cat_var=mysql_ result($result, $i,"category_na me");

If($cat==$cat_v ar)

$flag=1;
$i++;
}

if ($flag==1)
{

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

$result=mysql_q uery($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.......sha ll 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
2870
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. Value selected is used as the 'where' clause of the 2nd query. If $site is a single word, the 2nd query works like a charm. If $site is more than one...
2
3418
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 new values are updated in all corresponding tables (the function of the pages in question). However, on the page that does the DB update, I also...
0
2441
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 v4.0.13-standard for pc-linux. there must be a better way to query for this information than the method i'm using, since the result with v4.0 is...
4
3488
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 doesn't return any result. We have tried on our server and we always get a result. BUT, trying on the hosting server, many times the query doesn't return...
2
2054
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, query data, and write data pretty much without problem. My problem is this. I am trying to do some duplicate checking on this table so I query...
0
2208
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 out there could shed some light. Ok, the query (I've stripped it bare, the real query is a bit more complex)...
1
3898
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 column table. I know how to get the results into a single column table just fine using: while($row = mysql_fetch_array($result)) { print "<table...
10
2633
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 relating to a separate table of locations). 3: details
3
3120
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 and I finally got the answer I was looking for...almost. The problem is that the first result is not displayed. For example, my table in the database...
0
7487
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7934
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6003
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5349
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4966
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3476
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3459
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1033
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
731
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.