Jump to content


Photo

Connecting to a database


Best Answer DavidAM, 24 March 2013 - 11:44 PM

$sql = 'SELECT ...';
$results = mysql_query($sql) or die(mysql_error() . '<BR>' . $sql);

Should show you if there is an error in the SQL statement.

On the other hand: you misspelled the variable name in the while loop

$results = mysql_query("SELECT member_id, bank, reward_1, reward_2, reward_3, reward_4 FROM Points_Rewards") or die("Could Not Find Data");

//fetch tha data from the database
while ($row = mysql_fetch_array($result))


$results is plural in the query, but singular in the fetch. Go to the full post


  • Please log in to reply
2 replies to this topic

#1 Icewolf

Icewolf

    Advanced Member

  • Members
  • PipPipPip
  • 58 posts

Posted 24 March 2013 - 11:24 PM

Hi 
Can anyone help me with this code? It looks like it is geting to the part where the SQL is ran but we are getting to the die part of the code. I am trying to figure out am I missing something. I am new to PHP so I am not sure if this is correct or not. I have searched the web and everything looks good to me but I can't figure out what I a missing.
Thanks
Andy

<?php $username = "Entered username";
$password = "entered password";
$hostname = "localhost";
 

//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
 or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";

//select a database to work with
$selected = mysql_select_db("pdogclan_points",$dbhandle)
  or die("Did this change");

//execute the SQL query and return records
$results = mysql_query("SELECT member_id, bank, reward_1, reward_2, reward_3, reward_4 FROM Points_Rewards") or die("Could Not Find Data");

//fetch tha data from the database
while ($row = mysql_fetch_array($result))   
echo "ID: {$row['member_id']}". "Bank: {$row['bank']}". "Reward 1: {$row['reward_1']}". "Reward 2: {$row['reward_2']}". "Reward 3: {$row['reward_3']}". "Reward 4: {$row['reward_4']}";//display the results

//close the connection
mysql_close($dbhandle);
?>

 



#2 DavidAM

DavidAM

    Advanced Member

  • Gurus
  • 1,955 posts
  • LocationSpring, TX USA

Posted 24 March 2013 - 11:44 PM   Best Answer

$sql = 'SELECT ...';
$results = mysql_query($sql) or die(mysql_error() . '<BR>' . $sql);

Should show you if there is an error in the SQL statement.

On the other hand: you misspelled the variable name in the while loop

$results = mysql_query("SELECT member_id, bank, reward_1, reward_2, reward_3, reward_4 FROM Points_Rewards") or die("Could Not Find Data");

//fetch tha data from the database
while ($row = mysql_fetch_array($result))


$results is plural in the query, but singular in the fetch.
-- I haven't lost my mind, it's backed up on tape ... somewhere!

#3 Icewolf

Icewolf

    Advanced Member

  • Members
  • PipPipPip
  • 58 posts

Posted 25 March 2013 - 10:28 PM

Thank you we got it to work.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com