Page 1 of 1

my query is not displaying[solved]

Posted: Sun Jun 29, 2008 12:35 pm
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>

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

Posted: Sun Jun 29, 2008 12:54 pm
by onion2k
There's an error. If you didn't use @ to suppress errors you might see what it is.

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

Posted: Sun Jun 29, 2008 12:57 pm
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.

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

Posted: Sun Jun 29, 2008 1:04 pm
by swissbeets
ok thank you i will try everything and get back to you with the results

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

Posted: Sun Jun 29, 2008 1:22 pm
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......

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

Posted: Sun Jun 29, 2008 1:25 pm
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

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

Posted: Sun Jun 29, 2008 2:56 pm
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.