Jump to content

Archived

This topic is now archived and is closed to further replies.

theone

Really simple mysql php function help please

Recommended Posts

Hey guys.
At the risk of sounding stupid, im still going to ask this...
Why does this not work?...
[code]
<?
mysql_connect($db_host, $db_user, $db_pass);
mysql_select_db($db_name);
$sql = "SELECT * FROM news_site WHERE show = '1'";
$results = mysql_query($sql);
while ($data = mysql_fetch_array($results)) {
    var_dump($data);
}
?>
[/code]

I usually use the mysql_db_query function however i read at w3schools.com that that function is bein depreciated an that mysql_select_db and mysql_query should be used instead. But i cant seem to figure out why it isnt working :(

Please help me... im a dumbass ,lol.

Thanks in advance guys,
Dave

Share this post


Link to post
Share on other sites
Do you get any errors? The code looks ok. What are you expecting?

Ken

Share this post


Link to post
Share on other sites
[!--quoteo(post=362969:date=Apr 9 2006, 07:57 AM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Apr 9 2006, 07:57 AM) [snapback]362969[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Do you get any errors? The code looks ok. What are you expecting?

Ken
[/quote]

Yeh. The error is:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in X:\www\thebestunsigned\v4\index.php on line 23

Thnx for the reply

Share this post


Link to post
Share on other sites
Add "or die" clauses to you mysql statements:
[code]<?
mysql_connect($db_host, $db_user, $db_pass) or die("Problem connecting to MySQL<br />" . mysql_error());
mysql_select_db($db_name) or die("Problem selecting database $dbname<br />" . mysql_error());
$sql = "SELECT * FROM news_site WHERE show = '1'";
$results = mysql_query($sql) or die('Problem with query: ' . $sql . '<br />' . mysql_error());
while ($data = mysql_fetch_assoc($results)) { // will return an associative array
    echo '<pre>' . print_r($data,true) . '</pre>';  // will display on the screen cleaner
}
?>[/code]

Ken

Share this post


Link to post
Share on other sites
Thanks for the reply.
It turned out if i put the variable user in quotes it works :-/

Thanks all the same.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.