h20boynz Posted September 16, 2010 Share Posted September 16, 2010 I'm having trouble with a simple SELECT query. I just cannot figure out what the problem is... <?php //Include database connection details include 'login/config.php'; //Connect to mysql server $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if(!$link) { die('Failed to connect to server: ' . mysql_error()); } //Select database $db = mysql_select_db(DB_DATABASE); if(!$db) { die("Unable to select database"); } $qry="SELECT * FROM members"; $result = mysqli_query($link, $qry); echo "<table>"; while($row = mysqli_fetch_array($result, MYSQL_ASSOC)) { $getid = ($row['member_ID']); $firstname = ($row['firstname']); $lastname = ($row['lastname']); $email = ($row['email']); echo "<tr><td>$firstname</td><td>$email</td></tr>"; } echo "</table>"; ?> I know I have a connection to the DB, and I know that the query will return values as I have tested in in phpmyadmin. Can anyone see anything obvious I am missing? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/213614-query-returning-no-values-but-works-in-database/ Share on other sites More sharing options...
coupe-r Posted September 16, 2010 Share Posted September 16, 2010 Try taking out your parenthese on the $row[] vars. and try this echo '<tr><td>'.$firstname.'</td><td>'.$email.'</td></tr>'; Quote Link to comment https://forums.phpfreaks.com/topic/213614-query-returning-no-values-but-works-in-database/#findComment-1111867 Share on other sites More sharing options...
Pikachu2000 Posted September 16, 2010 Share Posted September 16, 2010 Perhaps if you describe the actual problem, and include any error messages, someone can help fix it. Quote Link to comment https://forums.phpfreaks.com/topic/213614-query-returning-no-values-but-works-in-database/#findComment-1111870 Share on other sites More sharing options...
h20boynz Posted September 16, 2010 Author Share Posted September 16, 2010 Perhaps if you describe the actual problem, and include any error messages, someone can help fix it. I don't get any error messages, and it returns no values...nothing. If I echo out the $link I get 'resource id # 3'... if I echo out $result I get nothing... Im not sure what else to insert to show me why I am not getting a result... I alos tried the changes suggested above...no success I have done everything suggeste in your signature also...I can echo out the $qry and it is as expected....in PHPmyAdmin it returns the results expected??? Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/213614-query-returning-no-values-but-works-in-database/#findComment-1111875 Share on other sites More sharing options...
mikosiko Posted September 16, 2010 Share Posted September 16, 2010 $qry="SELECT * FROM members"; $result = mysqli_query($link, $qry); read what the manual say about mysql_query() format here http://php.net/manual/en/function.mysql-query.php and you will found what is wrong also is good to enable error display or control it with die(mysql_error()); at the end of your mysql_query line Quote Link to comment https://forums.phpfreaks.com/topic/213614-query-returning-no-values-but-works-in-database/#findComment-1111878 Share on other sites More sharing options...
h20boynz Posted September 16, 2010 Author Share Posted September 16, 2010 $qry="SELECT * FROM members"; $result = mysqli_query($link, $qry); read what the manual say about mysql_query() format here http://php.net/manual/en/function.mysql-query.php and you will found what is wrong also is good to enable error display or control it with die(mysql_error()); at the end of your mysql_query line I've taken a look and man...I just cannot see it! I added this: $qry="SELECT * FROM members"; $result = mysqli_query($qry); if (!$result) { $message = 'Invalid query: ' . mysql_error() . "\n"; $message .= 'Whole query: ' . $qry; die($message); } The result is this: 'Invalid query: Whole query: SELECT * FROM members' So there is no mysql error and the select looks right to me...I??? Quote Link to comment https://forums.phpfreaks.com/topic/213614-query-returning-no-values-but-works-in-database/#findComment-1111879 Share on other sites More sharing options...
mikosiko Posted September 16, 2010 Share Posted September 16, 2010 ok... let start with what you didn't see: $ result = mysqli_query($link, $qry); if you read the manual carefully you should have notice that the format is (being $link optional) $result = mysqli_query($qry, $link); right? now... change that line and add $result = mysqli_query($qry) or die(mysql_error()); now that is assuming that you effectively are connecting to the DB... I will like to see what do you have in your config.php file are DB_HOST, DB_USER, DB_PASSWORD and DB_DATABASE variables?.... if so... why they don't have the $ as prefix? and run that code again Quote Link to comment https://forums.phpfreaks.com/topic/213614-query-returning-no-values-but-works-in-database/#findComment-1111880 Share on other sites More sharing options...
h20boynz Posted September 16, 2010 Author Share Posted September 16, 2010 I did notice I had the query, resource backwards but I have tried swapping them and just using the $qry on its own. Either way, I get no response. I have made the suggested changes: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta content="text/html; charset=utf-8" http-equiv="Content-Type" /> <title>Untitled 1</title> </head> <body> <?php //Include database connection details include 'login/config.php'; //Connect to mysql server $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if(!$link) { die('Failed to connect to server: ' . mysql_error()); } //Select database $db = mysql_select_db(DB_DATABASE); if(!$db) { die("Unable to select database"); } $qry="SELECT * FROM members"; $result = mysqli_query($qry) or die(mysql_error()); if (!$result) { $message = 'Invalid query: ' . mysql_error() . "\n"; $message .= 'Whole query: ' . $qry; die($message); } echo '<table>'; while($row = mysqli_fetch_array($result, MYSQL_ASSOC)) { $getid = ($row[member_ID]); $firstname = ($row[firstname]); $lastname = ($row[lastname]); $email = ($row[email]); echo '<tr><td>'.$firstname.'</td><td>'.$email.'</td></tr>'; } echo '</table>'; ?> </body> </html> And now the response is just a blank page...sorry for being so hopeless...I've spent hours on what should be a simple query and it just is not playing ball with me Quote Link to comment https://forums.phpfreaks.com/topic/213614-query-returning-no-values-but-works-in-database/#findComment-1111881 Share on other sites More sharing options...
schilly Posted September 16, 2010 Share Posted September 16, 2010 not sure if it makes a difference but you're using mysql_connect and mysql_select_db instead of mysqli_connect and mysqli_select_db. Quote Link to comment https://forums.phpfreaks.com/topic/213614-query-returning-no-values-but-works-in-database/#findComment-1111882 Share on other sites More sharing options...
PFMaBiSmAd Posted September 16, 2010 Share Posted September 16, 2010 Your are mixing mysql and mysqli statements. You cannot do that. Stick to one family of instructions for any connection that you make. If you were doing this on a system with error_reporting set to E_ALL and display_errors set to ON, you would have gotten errors alerting you to the fact that your connection was not compatible with the query statement. Quote Link to comment https://forums.phpfreaks.com/topic/213614-query-returning-no-values-but-works-in-database/#findComment-1111884 Share on other sites More sharing options...
mikosiko Posted September 16, 2010 Share Posted September 16, 2010 not sure if it makes a difference but you're using mysql_connect and mysql_select_db instead of mysqli_connect and mysqli_select_db. man ... I'm blind too I didn't see that "i" .... Jezzz Quote Link to comment https://forums.phpfreaks.com/topic/213614-query-returning-no-values-but-works-in-database/#findComment-1111885 Share on other sites More sharing options...
h20boynz Posted September 16, 2010 Author Share Posted September 16, 2010 Thanks guys...that just cracked it! SOLVED! AWESOME HELP! Quote Link to comment https://forums.phpfreaks.com/topic/213614-query-returning-no-values-but-works-in-database/#findComment-1111887 Share on other sites More sharing options...
schilly Posted September 16, 2010 Share Posted September 16, 2010 error_reporting(E_ALL); is a good one for debugging. Quote Link to comment https://forums.phpfreaks.com/topic/213614-query-returning-no-values-but-works-in-database/#findComment-1111889 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.