Page 1 of 1

Return types from mysql (not the same as the last thread)

Posted: Wed Sep 27, 2006 5:09 pm
by Luke
When you perform a query like this...

Code: Select all

SELECT * FROM `table` WHERE id = 1
And you use php to iterate through the results, are they ALWAYS strings? Because I've been tinkering with this for a while and it seems no matter what type of data, it gives you, if you do a gettype() on it, it returns "string" for example:

Code: Select all

public function load($conditions, $fields_array=null){
		$select_fields = "*";
		if($fields_array) $select_fields = implode(",", $fields_array);
		$sql = "SELECT " . $select_fields . " FROM `" . $this->table . "` WHERE " . $conditions . " LIMIT 1";
		//echo $sql;
		$result = $this->query($sql);
		if($result->length()){
			foreach($this->fields as $key => $value){
				$value->setValue($result->{$key});
                                echo gettype($result->{$key}); // Always outputs "string"
			}
			return true;
		}
		return false;
	}
By the way, I'm using d11's mysql result iterator here

Posted: Wed Sep 27, 2006 5:18 pm
by volka
The Ninja Space Goat wrote:And you use php to iterate through the results, are they ALWAYS strings?
Yo.
PHP is using mysql's c api.
http://dev.mysql.com/doc/refman/5.1/en/mysql-fetch-row.html wrote:MYSQL_ROW mysql_fetch_row(MYSQL_RES *result)
[...]
Return ValuesA MYSQL_ROW structure for the next row. NULL if there are no more rows to retrieve or if an error occurred.
http://dev.mysql.com/doc/refman/5.1/en/c-api-datatypes.html wrote:MYSQL_ROWThis is a type-safe representation of one row of data. It is currently implemented as an array of counted byte strings.
One exception

Code: Select all

<?php
$db = mysql_connect('localhost', 'localuser', 'localpass') or die(mysql_error());
$result = mysql_query('SELECT NULL', $db) or die(mysql_error());
$row = mysql_fetch_row($result);

echo gettype($row[0]);
?>
prints
NULL

Posted: Wed Sep 27, 2006 5:21 pm
by Luke
So how can I obtain that information?

Posted: Wed Sep 27, 2006 5:34 pm
by feyd

Posted: Wed Sep 27, 2006 5:36 pm
by Luke
well maybe this is the same thread as the last one, because that's the same response I got there too...

alright, thanks guys.

i used to be a blond if you guys are wondering.. :P

Posted: Wed Sep 27, 2006 5:39 pm
by volka
Jenk wrote:mysql_field_type may be of use - mostly because of the user comments regarding php types vs mysql types :)
or http://dev.mysql.com/doc/refman/5.1/en/ ... table.html
or http://dev.mysql.com/doc/refman/5.0/en/ ... table.html
and maybe many more ;)