I was happy with the response I got here: How to fetch field type and value?. It was working fine, I could get the data types as easily as doing what was suggested:
$datatype = $result->fetch_field_direct($count)->type;
where $count
was a number that got incremented withing the foreach loop
but then I got to experiment with PDO to prevent SQL injections, and everything was fine (after several bumps in the road, as I don't fully understand PDOs, I admit), until i decided to upload my code to the webserver and then I discovered they don't support the use of mysqld, meaning I can no longer use the get_result
function
$result = $stmt->get_result();
while I found a workaround (posted in the php reference comment section) for the get_result, I'm yet to found a replacement to the fetch_field_direct()
, any idea how can I obtain the datatype? maybe the workaround code might help (it's above my level of understanding php, but does a great work to simulate the basic functionality of get_result()
. As I can do:
$result = iimysqli_stmt_get_result($stmt);
$proceso = iimysqli_result_fetch_array($result);
...
foreach(array_keys($proceso) as $key){
$datatype = $result->fetch_field_direct($count)->type; // this is no longer working,
//I get error Fatal error: Call to undefined method iimysqli_result::fetch_field_direct()
// It used to work when $result was equal to $stmt->get_result();
... // example line
echo "<input class='$mysql_data_type_hash[$datatype]' type='hidden' name='$key' value='$proceso[$key]'><br/>";
...
}
I wonder if it's possible have a iimysqli_result_fetch_field_direct()
method to overcome not being able to use the standard one. If so, how? (where to begin at least)
Just to make it more clear: I want your help to know if there is a way to expand the workaround I found in the php reference page so I can get the datatype of a field. Since otherwise, I'm afraid will have to ditch it, and work my way around the fact I can't use the $stmt->get_result()
functionality.
I've read a little about using $stmt->bind_result(), but it seems a pain to use when one has a lot of fields to deal with, so I would like to avoid it (if possible of course), as the workaround (the code below) is already helping me to get the value (I only need to figure out the data type).
Workaround for get_result()
(Not mine, I did not write it, and to be honest I don't fully understand it, but it workds), this is a direct copy of the comment:
<?php
class iimysqli_result
{
public $stmt, $nCols;
}
function iimysqli_stmt_get_result($stmt)
{
/** EXPLANATION:
* We are creating a fake "result" structure to enable us to have
* source-level equivalent syntax to a query executed via
* mysqli_query().
*
* $stmt = mysqli_prepare($conn, "");
* mysqli_bind_param($stmt, "types", ...);
*
* $param1 = 0;
* $param2 = 'foo';
* $param3 = 'bar';
* mysqli_execute($stmt);
* $result _mysqli_stmt_get_result($stmt);
* [ $arr = _mysqli_result_fetch_array($result);
* || $assoc = _mysqli_result_fetch_assoc($result); ]
* mysqli_stmt_close($stmt);
* mysqli_close($conn);
*
* At the source level, there is no difference between this and mysqlnd.
**/
$metadata = mysqli_stmt_result_metadata($stmt);
$ret = new iimysqli_result;
if (!$ret) return NULL;
$ret->nCols = mysqli_num_fields($metadata);
$ret->stmt = $stmt;
mysqli_free_result($metadata);
return $ret;
}
function iimysqli_result_fetch_array(&$result)
{
$ret = array();
$code = "return mysqli_stmt_bind_result(\$result->stmt ";
for ($i=0; $i<$result->nCols; $i++)
{
$ret[$i] = NULL;
$code .= ", \$ret['" .$i ."']";
};
$code .= ");";
if (!eval($code)) { return NULL; };
// This should advance the "$stmt" cursor.
if (!mysqli_stmt_fetch($result->stmt)) { return NULL; };
// Return the array we built.
return $ret;
}
?>