2

I am a little perplexed by the mysqli interface in PHP. When I set out yesterday to change my old mysql code to mysqli, the docs steered me down the road of ::prepare, ::bind_params and ::execute.

In my efforts to deal with the results, I ended up writing my own fetch_assoc - and had problems which I discussed on SO. One commentator queried what I was doing not using ::fetch_assoc. I had not even noticed the mysqli_result class. When I morphed my mysqli code from ::preapre to ::query, the result handling became MUCH easier - with one exception: when previously I used ::bind_results to extract the columns, the PHP associative array that resulted would carry the correct data types; when instead I now use ::fetch_assoc etc, all I get is an array of strings.

I am coding up a converter now. I just thought that since I already committed one cardinal error in my understanding of the mysqli library, I better post here to see whether there's an "official" way of getting type-congruent associative result arrays.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Ollie2893
  • 427
  • 1
  • 5
  • 11
  • Have you tried fetch_object() or fetch_row()? – Chris Aug 26 '10 at 14:12
  • I haven't tried. The manual says for fetch_object: "Returns an object with string properties that corresponds to the fetched row." Not promising... – Ollie2893 Aug 26 '10 at 14:14
  • You've hit on a big wish that I've had for `MySQLi`... The ability to execute a prepared statement and return a `MySQLi_Result` object... But as for now if you want to use prepared statements, you need to completely re-engineer the `MySQLi_Result` class to work with the statement... – ircmaxell Aug 26 '10 at 14:33
  • ...or you take the next step and use pdo instead of mysqli, http://docs.php.net/pdo – VolkerK Aug 26 '10 at 14:40
  • Ha ha! I just love you Volker. I will take a look at the PDO stuff. I am a little drained at this moment. 3 rewrites in 24hrs have fried my brain. Anyway, you all seem to confirm that type-congruent fetch does not exist in mysqli_result. I meanwhile found the type map for fetch_fields and just completed a small set of fetch_correct functions. That has done the trick. Thanks all. – Ollie2893 Aug 26 '10 at 14:59

1 Answers1

5

If you're using mysqlnd as the transport client and set the option MYSQLI_OPT_INT_AND_FLOAT_NATIVE on the mysqli object you get in fact the native types.

echo 'phpversion: ', phpversion(), "\n";

$m = new mysqli('localhost', 'localonly', 'localonly', 'test');
$m->options(MYSQLI_OPT_INT_AND_FLOAT_NATIVE, true);
$m->query('CREATE TABLE soTest (x int)');
$m->query('INSERT INTO soTest (x) VALUES (1),(2)');

$r = $m->query('SELECT x from soTest');
var_dump($r->fetch_assoc());

prints

phpversion: 5.3.3
array(1) {
  ["x"]=>
  int(1)
}

This feature has been added in PHP 5.3.


To get you started with PDO (should you choose to use it):

<?php
$pdo = new PDO('mysql:host=localhost;dbname=test', 'localonly', 'localonly');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// the pdo_mysql driver uses emulated prepared statements by default
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

// afaik only the mysqlnd client supports native types, with libmysql you'll get only strings/null
echo 'client: ', $pdo->getAttribute(PDO::ATTR_CLIENT_VERSION), "\n";

// set up test environment
$pdo->exec('CREATE TEMPORARY TABLE soTest (x int, y varchar(16))');
$pdo->exec("INSERT INTO soTest (x,y) VALUES (1,'a'),(2,null)");

// statement with positional parameter
$stmt = $pdo->prepare('SELECT x,y FROM soTest WHERE x>?');
$stmt->setFetchMode(PDO::FETCH_ASSOC);

$stmt->execute( array(0) );
foreach( $stmt as $row ) {
  foreach( $row as $col ) {
    echo gettype($col), '(', $col, ') ';
  }
  echo "\n";
}

prints

client: mysqlnd 5.0.7-dev - 091210 - $Revision: 300533 $
integer(1) string(a) 
integer(2) NULL() 
Dharman
  • 30,962
  • 25
  • 85
  • 135
VolkerK
  • 95,432
  • 20
  • 163
  • 226