Link to home
Start Free TrialLog in
Avatar of error77
error77

asked on

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
Avatar of mattibutt
mattibutt
Flag of United States of America image

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
Do this, and post the results:

var_dump($row);
Also, check the man page here to see what MySQL_Query() returns.
http://us.php.net/manual/en/function.mysql-query.php
SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
var_dump($row)
Avatar of error77
error77

ASKER

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?
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
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

Avatar of error77

ASKER

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

Avatar of error77

ASKER

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 ???
ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of error77

ASKER

Hi ChrisStanyon,

Tried your code:

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

Avatar of error77

ASKER

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
Avatar of error77

ASKER

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?
Avatar of error77

ASKER

Unless the syntax of this is wrong:

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

ASKER

Anyone have any sugestions ?
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

Avatar of error77

ASKER

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/
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