mysql_query -- question about results "location"
Forgive me if this is a particularly stupid question!
mysql_query($query)
returns a boolean, but you can also assign it to a variable
$results = mysq开发者_运维问答l_query($query)
and then use the other mysql_
functions to extract data.
Out of curiosity, how does mysq_query($query)
act as both a boolean and a data container at the same time? What's happening "under the hood" during these steps?
(yes, I am a n00b..., please be kind!)
If you notice, when it returns true/false, you can't use it with the other functions such as mysql_fetch_assoc()
.
From the mysql_query()
documentation:
For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.
For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error.or FALSE on error.
What happens, is for statements that do not return data, it responds true/false on whether or not the query was successful.
When there is a result set, you will see it returns a MySQL resource. This is a special value that allows PHP to figure out what data set you are talking about. You then pass this resource to other MySQL function to retrieve the data.
See: http://www.php.net/manual/en/language.types.resource.php
Doing
var_dump(mysql_query($query));
should tell you it's a resource (as well as its id)
精彩评论