my query is not displaying[solved]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
swissbeets
Forum Newbie
Posts: 20
Joined: Thu Jun 26, 2008 7:56 pm

my query is not displaying[solved]

Post by swissbeets »

there are only 2 products as of right now but the only things being displayed is

"
$



$

"


<body>
<?php
$db=mysql_connect ("localhost", "root", "") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db("widget") or die ("Could not select database");


$query = sprintf("SELECT * FROM products");
$result = @mysql_query($query);


while($row = @mysql_fetch_array($result)){
echo "<p>";
echo $row['product_name']."<br />";
echo "$".$row['product_price']."<br />"."<br />";
echo $row['product_description']."<br />";
echo "</p>";
} ?></body>
Last edited by swissbeets on Sun Jun 29, 2008 3:10 pm, edited 1 time in total.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: my query is not displaying but it seems my code is right

Post by onion2k »

There's an error. If you didn't use @ to suppress errors you might see what it is.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: my query is not displaying but it seems my code is right

Post by califdon »

It appears that your query isn't returning any data. First of all, remove the @ from the beginning of your mysql_query() and mysql_fetch_array() functions; that suppresses any warnings or errors. Then, I'm not sure why you're doing this:

Code: Select all

$query = sprintf("SELECT * FROM products");
$result = @mysql_query($query);
I wouldn't use sprintf() at all. And you should use an or die(mysql_error()) after your query, too.

That may shed some light on why no data is being returned.
swissbeets
Forum Newbie
Posts: 20
Joined: Thu Jun 26, 2008 7:56 pm

Re: my query is not displaying but it seems my code is right

Post by swissbeets »

ok thank you i will try everything and get back to you with the results
swissbeets
Forum Newbie
Posts: 20
Joined: Thu Jun 26, 2008 7:56 pm

Re: my query is not displaying but it seems my code is right

Post by swissbeets »

$query = "SELECT * FROM products" ;
$result = mysql_query($query);


$query = "SELECT * FROM products" ;
$result = mysql_query($query);


while($row = mysql_fetch_array($result)){
echo "<p>";
echo $row['product_name']."<br />";
echo "$".$row['product_price']."<br />"."<br />";
echo $row['product_description']."<br />";
echo "</p>";
}
confirm_query($result);
if (!$result) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}

this is my new code but the out put is the same.

i decided to run both of those query checks because i was getting no errors, i feel like it is probably a small mistake somewhere
i am new to this but in the while loop $row is being defined and then compared to the array, as long as there are still values in the array then the whlie loops should still be running correct? but does that make sense that it is running twice and there are only 2 products right now so it is collecting some information it is just not showing what it collected......
swissbeets
Forum Newbie
Posts: 20
Joined: Thu Jun 26, 2008 7:56 pm

Re: my query is not displaying but it seems my code is right

Post by swissbeets »

ok i apologize that was a terrible error to have even posted, the problem was in my database i had the "Product_name" and "Product_price"...sorry about that but thank you very much for your help
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: my query is not displaying but it seems my code is right

Post by califdon »

Not a problem. It's very common to only see an obvious error after you have explained the issue to someone else, such as posting in a forum. One request: when your problem has been solved, please return to your original posting and EDIT the Subject to include the word [SOLVED], so that others won't waste time reading through your question to see if they can help. Thanks.

P.S. Feel welcome to return with other questions, but please, in the future, enclose your code in [syntax=php]...[/syntax] Code tags, to make your code more readable.
Post Reply