Jump to content

SQL Query Result: Resource #3???


xProteuSx

Recommended Posts

I am sending a query to my DB and the output is "Resource #3"  The code is as follows:

<?php
include ("dbconnect.php"); //connect to the DB
$emailquery = "SELECT users_email FROM `users` WHERE `users_handle` LIKE '%Proteus%'";
$email_result = mysql_query($emailquery) or die ("The query caused the following error:<br><br>" . mysql_error());
echo ("$email_result");
?>

What does this mean?  I cannot figure it out ...
Link to comment
https://forums.phpfreaks.com/topic/27403-sql-query-result-resource-3/
Share on other sites

When you run a query with a function such as mysql_query(), a resource [b]to[/b] the result set is returned, not the results themselves. You have to then retrieve just the data you are after. So, in a case like yours, you'd want to do something like this:
[code]
<?php
$sql = mysql_query("SELECT users_email FROM users WHERE users_handle LIKE '%Proteus%'");

// see if your result holds any records
if (mysql_num_rows($sql) > 0) {
  // It does, so let's display them:
  while ($row = mysql_fetch_array($sql)) {
    echo "$row[users_email]<br />\n";
  }
}
?>
[/code]

Hope this helps.
[b]$email_result[/b] is a resource handle, that you use to fetch the result, it is not the result handle!

[code]<?php
include ("dbconnect.php"); //connect to the DB
$emailquery = "SELECT users_email FROM `users` WHERE `users_handle` LIKE '%Proteus%'";
$email_result = mysql_query($emailquery) or die ("The query caused the following error: " . mysql_error());

$result = mysql_fetch_assoc ( $email_result );

echo $result['users_email'];
?>[/code]

Also don't do stuff like...

[code]echo ( "junk" );[/code]

[b]echo[/b] is not a function, it's [b]language construct[/b], so using [b]()[/b], is not proper coding, even if it does work[b].[/b]
obsidian, printf, thanks so much for your replies.  Both methods work.  I wish I knew about 'mysql_fetch_assoc' earlier, but my book does not mention this function.  I guess that's what happens when you can only afford a MySQL book written in 2000.  Six years is a long time for something like MySQL ...

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.