Very easy SQL function returns boolean 1 (true) instead of resource
I have this开发者_如何学Go function
function query($query)
{
return mysql_query($query) or die(mysql_error())
}
but when I call it like
$last = query("SELECT * FROM jubox ORDER BY id desc LIMIT 45");
$last returns 1
echo $last; //1
What am I doing wrong?
EDIT:
I want to return RESOURCE id NOT DATA
Don't mix return
and x OR y
<?php
$mysql = mysql_connect('localhost', 'localonly', 'localonly') or die(mysql_error());
mysql_select_db('test', $mysql) or die(mysql_error($mysql));
mysql_query('CREATE TEMPORARY TABLE jubox_so (id int auto_increment, primary key(id))') or die(mysql_error($mysql));
$last = query("SELECT * FROM jubox_so ORDER BY id desc LIMIT 45");
echo $last;
function query($query)
{
$v = mysql_query($query);
if ( !$v ) {
die(myql_error());
}
return $v;
}
prints (something like) Resource id #6
as expected instead of 1 (which was caused by the bool->string conversion for echo)
Currently, you are trying to echo out a resource, which returns 1 if valid (if echoing a resource, it does indeed echo 1)
You need to use a mysql_fetch_* function to get the data
Look into mysql_fetch_* functions. They return actual rows for you. The mysql_query returns a link_identifier that can be used to fetch the results. See http://php.net/manual/en/book.mysql.php for the different functions available.
You're echoing the result right? In case of a select this is a mysql result object and not true or false (like with update or insert). What you get is a result resource. You need to use functions as fetch_assoc to explore that result.
i think the reason why you print out '1', is because your query result of an error and then it is the returned value of the die() that you get back. Check you query, copy and paste it in phpMyAdmin for exemple, and look for an error
精彩评论