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
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
Do this, and post the results:
var_dump($row);
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
http://us.php.net/manual/en/function.mysql-query.php
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
var_dump($row)
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?
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
"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
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
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
$result = mysql_query("SELECT count(*) FROM `keys` WHERE `random_key`= '.$code.' ") or die(mysql_error());
but still returns bool(false) ... No match found
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 ???
$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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi ChrisStanyon,
Tried your code:
It's returning 0 ... I don't get it :o/
Tried your code:
It's returning 0 ... I don't get it :o/
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
<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",
// 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
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?
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?
ASKER
Unless the syntax of this is wrong:
$sql = "SELECT count(*) FROM `keys` WHERE `random_key` = '.$code.' "; although it's not returning any errors.
$sql = "SELECT count(*) FROM `keys` WHERE `random_key` = '.$code.' "; although it's not returning any errors.
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.
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.
}
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/
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.
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';";
in order for you to see the actual item you need to echo the fields