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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

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

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

So how can I obtain that information?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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 ;)
Post Reply