PHP Error ? resource(9)

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
error77Asked:
Who is Participating?
 
Chris StanyonCommented:
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
 
mattibuttCommented:
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
 
Ray PaseurCommented:
Do this, and post the results:

var_dump($row);
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.

 
Ray PaseurCommented:
Also, check the man page here to see what MySQL_Query() returns.
http://us.php.net/manual/en/function.mysql-query.php
0
 
Ray PaseurCommented:
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
 
raminhosCommented:
var_dump($row)
0
 
error77Author Commented:
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
 
Ray PaseurCommented:
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
 
Ray PaseurCommented:
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
 
error77Author Commented:
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
 
error77Author Commented:
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
 
error77Author Commented:
Hi ChrisStanyon,

Tried your code:

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

0
 
error77Author Commented:
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
 
error77Author Commented:
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
 
error77Author Commented:
Unless the syntax of this is wrong:

$sql = "SELECT count(*) FROM `keys` WHERE `random_key` = '.$code.' ";  although it's not returning any errors.
0
 
error77Author Commented:
Anyone have any sugestions ?
0
 
Chris StanyonCommented:
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
 
error77Author Commented:
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
 
Chris StanyonCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.