Jump to content


Photo

Really simple mysql php function help please


  • Please log in to reply
4 replies to this topic

#1 theone

theone
  • Members
  • PipPip
  • Member
  • 17 posts

Posted 09 April 2006 - 12:53 PM

Hey guys.
At the risk of sounding stupid, im still going to ask this...
Why does this not work?...
<?
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);
}
?>

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
theone

#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 09 April 2006 - 12:57 PM

Do you get any errors? The code looks ok. What are you expecting?

Ken

#3 theone

theone
  • Members
  • PipPip
  • Member
  • 17 posts

Posted 09 April 2006 - 01:03 PM

[!--quoteo(post=362969:date=Apr 9 2006, 07:57 AM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Apr 9 2006, 07:57 AM) View Post[/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
theone

#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 09 April 2006 - 02:05 PM

Add "or die" clauses to you mysql statements:
<?
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
}
?>

Ken

#5 theone

theone
  • Members
  • PipPip
  • Member
  • 17 posts

Posted 09 April 2006 - 02:55 PM

Thanks for the reply.
It turned out if i put the variable user in quotes it works :-/

Thanks all the same.
theone




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users