Question:
With PHP and MYSQL, why are even specific results stored in an array?
?
2009-07-20 12:42:40 UTC
That is to say, if you select a specific field, why does it need to store the results in an array? I understand if you select a whole row, but if it is just one value, why doesn't it just store that value in a simple variable? Is there an advantage to the way it is done, or is that just the way it is?
Five answers:
Atif Majid
2009-07-22 02:37:05 UTC
When you run mysql_query function in PHP, it returns a result resource if the query is of type SELECT, SHOW, DESCRIBE, EXPLAIN. It can contain one or multiple rows. Even if you run a "Select count(*) as cnt from TABLE_NAME" the result will be one row having one column (cnt). Now there are multiple ways to get the result from result resource. You can use mysql_fetch_array (containing numeric indexes as well as associative array), mysql_fetch_associative (associative array, having column name as key and column value as value), mysql_fetch_object returns an object. So it depends whichever function you want to use. You can have more detailed reading on these function in PHP Mannual (http://www.php.net/manual/en/ref.mysql.php)
psikic72032
2009-07-20 13:37:49 UTC
It is because you are getting a result set. Even if your select statement only references one field, it could return more than one record, with each record only containing that field. Each array element is one record.
Zyzzyx
2009-07-20 12:48:27 UTC
I don't actually know this, but I would suppose it has to do with supporting a single method of results presentation.
i8badhaggis
2009-07-20 13:31:04 UTC
I have to say I'm confused by the question
anonymous
2009-07-22 12:00:13 UTC
there is a function for that it is mysql_result()



example:



$query = mysql_query("SELECT name FROM users WHERE name='Jim'");

$num_rows = mysql_num_rows($query);

if($num_rows == "1"){

$result = mysql_result($query, 0);

echo $result;

}

else{

echo "Nothing in there";

}


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...