Page 1 of 1

Resource id #5 error

Posted: Mon Nov 22, 2010 9:06 am
by m1nistry
Hello Guys,

I am getting an Resource id #5 error.... in that code

Code: Select all

<?php
include('design/header.php');
require('connect.php');

$albumid = $_SESSION['album'];
$id = $_SESSION['id'];


 // find location of the photo
 $image = mysql_query("SELECT * FROM items WHERE userid='$id' AND albumid='$albumid'");
echo "$image"; 
 while ($row = mysql_fetch_assoc($image))
{
	$location = $row['location'];
	echo "Album Cover Change, Succes! Go Back To <a href=albums.php>Albums</a>.";
 	$setcover = mysql_query("UPDATE albums SET cover = '$location'
WHERE userid = '$id' AND id = '$albumid'") or die("Error While Changing your album's cover.Please Try Again.");

}
?>
The echo "$image"; Is there just to test what is getting out from that query... and there is where the Resource id #5 is coming... any help?

Re: Resource id #5 error

Posted: Mon Nov 22, 2010 9:10 am
by Celauran
You can't echo an SQL result. What is it you're trying to do?

Re: Resource id #5 error

Posted: Mon Nov 22, 2010 9:14 am
by m1nistry
Celauran wrote:You can't echo an SQL result. What is it you're trying to do?
I am tring to update an database record... the first query is using to take some data's to use them in the UPDATE query...but the first query fails and the second is getting the Resource id #5 as result and using it to fill the data's...

If you check the code you will undestund i bilive

Re: Resource id #5 error

Posted: Mon Nov 22, 2010 9:20 am
by Celauran
Since the second query depends on the first, it's guaranteed to fail if the first fails. Have you tried the first query manually? Does that work? Can you paste the exact error you're getting?

Re: Resource id #5 error

Posted: Mon Nov 22, 2010 9:26 am
by m1nistry
Celauran wrote:Since the second query depends on the first, it's guaranteed to fail if the first fails. Have you tried the first query manually? Does that work? Can you paste the exact error you're getting?
Well Look, I Will post you the whole code, and an screen with the result...

Code: Select all

<?php
include('design/header.php');
require('connect.php');

$albumid = $_SESSION['album'];
$id = $_SESSION['id'];


 // find location of the photo
 $image = mysql_query("SELECT * FROM items WHERE userid='$id' AND albumid='$albumid'");
 echo "$image";
 while ($row = mysql_fetch_assoc($image))
{
	$location = $row['location'];
	echo "Album Cover Change, Succes! Go Back To <a href=albums.php>Albums</a>.";
 	$setcover = mysql_query("UPDATE albums SET cover = '$location'
WHERE userid = '$id' AND id = '$albumid'") or die("Error While Changing your album's cover.Please Try Again.");

}
?>
Screen
Image

Re: Resource id #5 error

Posted: Mon Nov 22, 2010 9:29 am
by Celauran
That's not an error you're seeing, that's the result of echo $image. If it's not echoing the success message, though, then it looks like it's not entering the while loop. What does var_dump($image) display?

Re: Resource id #5 error

Posted: Mon Nov 22, 2010 9:31 am
by m1nistry
Celauran wrote:That's not an error you're seeing, that's the result of echo $image.
Yeap, i know that... but i say that it's error cuz it's not normal, i mean the action is to take some data and echo them, well the data wassen't Recurce id #5 but it's echoing Recurce id #5 ... So pretty much error?

Anyway... thing is not if it's error or not... any help to fix that?

Re: Resource id #5 error

Posted: Tue Nov 23, 2010 7:40 am
by m1nistry
guys? any help?

Re: Resource id #5 error

Posted: Tue Nov 23, 2010 8:58 am
by Celauran
Have you echoed the query to make sure all fields have values assigned properly? What about echo mysql_num_rows($image)?

Re: Resource id #5 error

Posted: Tue Nov 23, 2010 11:10 am
by s992
m1nistry wrote: Yeap, i know that... but i say that it's error cuz it's not normal, i mean the action is to take some data and echo them, well the data wassen't Recurce id #5 but it's echoing Recurce id #5 ... So pretty much error?

Anyway... thing is not if it's error or not... any help to fix that?
No, that is normal. $image is defined as the result of a MySQL query, which is a resource. You are echoing a resource, which is why you are seeing that.

Re: Resource id #5 error

Posted: Tue Nov 23, 2010 12:19 pm
by McInfo
Where is session_start()?

Re: Resource id #5 error

Posted: Wed Nov 24, 2010 3:03 pm
by m1nistry
McInfo wrote:Where is session_start()?
session_start() is included via the header.php
Celauran wrote:Have you echoed the query to make sure all fields have values assigned properly? What about echo mysql_num_rows($image)?
s992 wrote:
m1nistry wrote: Yeap, i know that... but i say that it's error cuz it's not normal, i mean the action is to take some data and echo them, well the data wassen't Recurce id #5 but it's echoing Recurce id #5 ... So pretty much error?

Anyway... thing is not if it's error or not... any help to fix that?
No, that is normal. $image is defined as the result of a MySQL query, which is a resource. You are echoing a resource, which is why you are seeing that.
i don't realy care about echoing the result, i just need to don't get resource id #5 so the UPDATE query take the right VALUES...

Re: Resource id #5 error

Posted: Wed Nov 24, 2010 3:45 pm
by Celauran
m1nistry wrote:i don't realy care about echoing the result, i just need to don't get resource id #5 so the UPDATE query take the right VALUES...
As has already been explained, you're getting the resource id #5 'error' specifically because you're trying to echo an SQL result. Comment out the line echo $image and that will stop being displayed.

Re: Resource id #5 error

Posted: Tue Nov 30, 2010 2:53 pm
by m1nistry
Celauran wrote:
m1nistry wrote:i don't realy care about echoing the result, i just need to don't get resource id #5 so the UPDATE query take the right VALUES...
As has already been explained, you're getting the resource id #5 'error' specifically because you're trying to echo an SQL result. Comment out the line echo $image and that will stop being displayed.
You can't undestund what i am saying... even if i don't echo the result the query takes "Recurce id #5" as data and import them!

Re: Resource id #5 error

Posted: Tue Nov 30, 2010 5:17 pm
by McInfo
Run this. Read the output. Study the logic.

Code: Select all

<?php
session_start();
require 'connect.php';

echo '<pre>';

echo "This is what I found in the session:\n";
print_r($_SESSION);
echo "\n";

$continue = true;

if (! isset($_SESSION['album'])) {
    echo "I found no session key named 'album'.\n";
    $continue = false;
}

if (! isset($_SESSION['id'])) {
    echo "I found no session key named 'id'.\n";
    $continue = false;
}

if (! $continue) {
    echo "I cannot continue.\n";

} else {
    echo "I found the two session keys I need.\n\n";

    $query = sprintf(
        "SELECT * FROM items WHERE userid = '%s' AND albumid = '%s'",
        mysql_real_escape_string($_SESSION['album']),
        mysql_real_escape_string($_SESSION['id'])
    );
    echo "I am going to run this query:\n";
    echo $query, "\n\n";

    $result = mysql_query($query);
    echo "The result of the query is ";
    var_dump($result);
    echo "The string representation is '$result'\n\n";

    if (is_resource($result)) {

        $numRows = mysql_num_rows($result);
        printf("The result set has %d row(s).\n\n", $numRows);

        if ($numRows > 0) {
            $rows = array();
            echo "I will now fetch the row(s).\n";
            while ($row = mysql_fetch_assoc($result)) {
                $rows[] = $row;
            }
            echo "I found this data:\n";
            print_r($rows);
        }

    }
}
echo '</pre>';