Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

PHP Error ? resource(9)

Posted on 2010-09-19
19
982 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 109

Expert Comment

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

var_dump($row);
0
 
LVL 109

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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 109

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 109

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 109

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
 

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 43

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 43

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 43

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

860 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