Jump to content

Really simple mysql php function help please


theone

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
Link to comment
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
Link to comment
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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.