Jump to content

query returning no values, but works in database


h20boynz

Recommended Posts

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

 

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

$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???

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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.

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.