Solved

PHP Error ? resource(9)

Posted on 2010-09-19
19
957 Views
Last Modified: 2013-12-12
Hi All,

I am trying to see what a query is returning so I've used var_dump() which is returning:

resource(9) of type (mysql result)

This is the query code:

$code = $_POST['code'];
$result = mysql_query("SELECT count(*) FROM `keys` WHERE `random_key`<> '.$code.' ") or die(mysql_error());

$row = mysql_fetch_row($result);

Then I've added:

var_dump($result); which is returning:

resource(9) of type (mysql result)

Any ideas please?

thanks
0
Comment
Question by:error77
  • 9
  • 5
  • 3
  • +2
19 Comments
 
LVL 11

Expert Comment

by:mattibutt
ID: 33711612
you are only fetching row number so its returning record number 9
in order for you to see the actual item you need to echo the fields
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 33711615
Do this, and post the results:

var_dump($row);
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 33711622
Also, check the man page here to see what MySQL_Query() returns.
http://us.php.net/manual/en/function.mysql-query.php
0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 200 total points
ID: 33711629
And as long as we are learning PHP, beware of this timebomb:

$code = $_POST['code'];

Instead you almost certainly want this safer example:

$code = mysql_real_escape_string($_POST['code']);
0
 
LVL 3

Expert Comment

by:raminhos
ID: 33711658
var_dump($row)
0
 

Author Comment

by:error77
ID: 33711661
OK I have a record in the table = "1234567890" and it's exactly what I'm posting in my form ...


Now ... var_dump($row); is returning: bool(false)

I have this code also:

if($row[0] == 1){

 echo 'yes, theres are match';
 
}else{


 echo 'No match found';
 die;

... and it's returning: No match found when there SHOULD be a match :o/

I cannot understand what's wrong :o(

Any ideas anyone pls?
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 33711674
I think your query is wrong.  Print it out with var_dump, too.

"SELECT count(*) FROM `keys` WHERE `random_key`<> '.$code.' "

The <> says NOT EQUAL

The periods around $code seem to be in the wrong place.

HTH, ~Ray
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 33711682
Sorry - clicked submit too soon.

After you visualize the query, you might want to change it to this...

I'll check back later today to see if you've gotten things cleared up.  Best, ~Ray
$sql = "SELECT count(*) FROM `keys` WHERE `random_key` <> '$code' ";

$res = mysql_query($sql);

if (!$res)

{

    echo "QUERY FAILED";

    echo htmlentities($sql);

    die( mysql_error() );

}

// ... REST OF THE PROCESS HERE

Open in new window

0
 

Author Comment

by:error77
ID: 33711689
Actually query is:

$result = mysql_query("SELECT count(*) FROM `keys` WHERE `random_key`= '.$code.' ") or die(mysql_error());

but still returns bool(false) ... No match found

0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:error77
ID: 33711699
OK I've added your code and a bit more:

$sql = "SELECT count(*) FROM `keys` WHERE `random_key` = '$code' ";
$res = mysql_query($sql);
if (!$res)
{
    echo "QUERY FAILED";
    echo htmlentities($sql);
    die( mysql_error() );
} else {
      echo $res;
}

returns: Resource id #9

What's that? Resource id #9 ???
0
 
LVL 42

Accepted Solution

by:
Chris Stanyon earned 300 total points
ID: 33711758
When you run mysql_query it returns a resource, which you can then need to read to get at your data.

To view the info returned by your query, you need to do something with the resource.

For example:



$sql = "SELECT count(*) FROM `keys` WHERE `random_key` = '$code' ";

$res = mysql_query($sql);

if (!$res)

{

    echo "QUERY FAILED";

    echo htmlentities($sql);

    die( mysql_error() );

} else {

     //now you've got a resource, lets look at the data.

     $returnedData = mysql_fetch_array($res);

     echo $returnedData[0]; //This will echo your count.

}

Open in new window

0
 

Author Comment

by:error77
ID: 33711825
Hi ChrisStanyon,

Tried your code:

It's returning 0 ... I don't get it :o/

0
 

Author Comment

by:error77
ID: 33711834
This is the whole code including the form:

<form method="post" action="index.php"> Code:<input type="text" size="12" maxlength="12" name="code">:<br />
<input type="submit" value="submit" />
</form>

<?php
// Make a MySQL Connection
mysql_connect("localhost", "root", "") or die(mysql_error());

// Select a Datase
mysql_select_db("testdb") or die(mysql_error());

// Check value of code in table
$sql = "SELECT count(*) FROM `keys` WHERE `random_key` = '$code' ";
$res = mysql_query($sql);
if (!$res)
{
    echo "QUERY FAILED";
    echo htmlentities($sql);
    die( mysql_error() );
} else {
       //now you've got a resource, lets look at the data.
     $returnedData = mysql_fetch_array($res);
     echo $returnedData[0]; //This will echo your count.
      
      
      
}


?>

Hope this helps

thanks
0
 

Author Comment

by:error77
ID: 33711891
Something really strange:

Although $code is returning the posted code $returnedData[0] returns 0 BUT when I hardcode the string I am posting on the form $returnedData[0] returns 1 although for me they look exactly the same?
0
 

Author Comment

by:error77
ID: 33711918
Unless the syntax of this is wrong:

$sql = "SELECT count(*) FROM `keys` WHERE `random_key` = '.$code.' ";  although it's not returning any errors.
0
 

Author Comment

by:error77
ID: 33711988
Anyone have any sugestions ?
0
 
LVL 42

Expert Comment

by:Chris Stanyon
ID: 33712006
error77,

In you query above you are still including the periods (full stops in English), so your query, once the variable has been replace looks like this:

SELECT count(*) FROM `keys` WHERE `random_key` = '.1234567890.' "
//notice the periods around the code.

The reason you're getting 0 returned is because that's the count of records with a code ".1234567890." which is probably correct (no records match that)

Also, when you post your form to index.php the value that is typed into your Text field is accessed by $_POST['code']. It will only be accessible through $code if you have register_globals turned on - which you definitely shouldn't have.

Finally, as Ray says, you ought to escape your data before feeding into a query.






$code = mysql_real_escape_string($_POST['code']);



$sql = "SELECT count(*) FROM keys WHERE random_key = '$code';";



$res = mysql_query($sql);

if (!$res)

{

    echo "QUERY FAILED";

    echo htmlentities($sql);

    die( mysql_error() );

}

else

{

     //now you've got a resource, lets look at the data.

     $returnedData = mysql_fetch_row($res);

     echo $returnedData[0]; //This will echo your count.

}

Open in new window

0
 

Author Comment

by:error77
ID: 33712085
I tried the code I got the following error:

QUERY FAILEDSELECT count(*) FROM keys WHERE random_key = '1234567890';You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'keys WHERE random_key = '1234567890'' at line 1

For some reason it does not like not having the quotes between the column names :o/
0
 
LVL 42

Expert Comment

by:Chris Stanyon
ID: 33712178
error77,

KEYS is a reserved word in MySQL, so that's why it needs the quotes around it. Either put the quotes around it or change your table name to a non-reserved word.



$sql = "SELECT count(*) FROM `keys` WHERE random_key = '$code';";



OR



$sql = "SELECT count(*) FROM my_keys WHERE random_key = '$code';";

Open in new window

0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now