<?php

$test_table_name = 'test_mysqli_stmt_send_long_data_packet_size_libmysql_table_1';
require 'table.inc';
if (!($stmt = mysqli_stmt_init($link))) {
    printf("[001] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
}
if (!mysqli_query($link, "DROP TABLE IF EXISTS test_mysqli_stmt_send_long_data_packet_size_libmysql_table_1")) {
    printf("[002] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
}
if (!mysqli_query($link, sprintf("CREATE TABLE test_mysqli_stmt_send_long_data_packet_size_libmysql_table_1(id INT NOT NULL AUTO_INCREMENT, label LONGBLOB, PRIMARY KEY(id)) ENGINE = %s", $engine))) {
    printf("[003] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
}
if (!mysqli_stmt_prepare($stmt, "INSERT INTO test_mysqli_stmt_send_long_data_packet_size_libmysql_table_1(id, label) VALUES (?, ?)")) {
    printf("[004] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
}
$id = null;
$label = null;
if (!mysqli_stmt_bind_param($stmt, "ib", $id, $label)) {
    printf("[005] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
}
if (!($res = mysqli_query($link, "SHOW VARIABLES LIKE 'max_allowed_packet'"))) {
    printf("[006] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
}
if (!($row = mysqli_fetch_assoc($res))) {
    printf("[007] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
}
mysqli_free_result($res);
if (0 === ($max_allowed_packet = (int) $row['Value'])) {
    printf("[008] Cannot determine max_allowed_packet size and/or bogus max_allowed_packet setting used.\n");
}
    printf("[018] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
}
if (!is_object($res = mysqli_stmt_get_result($stmt)) || 'mysqli_result' != get_class($res)) {
    printf("[019] Expecting object/mysqli_result got %s/%s, [%d] %s\n", gettype($res), $res, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
}
$id = $label = null;
if (!mysqli_stmt_bind_result($stmt, $id, $label)) {
    printf("[020] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
}
$row = mysqli_fetch_assoc($res);
if (NULL !== $id || NULL !== $label) {
    printf("[021] Bound variables should not have been set\n");
}
mysqli_free_result($res);
mysqli_stmt_close($stmt);
if (!($stmt = mysqli_stmt_init($link)) || !mysqli_stmt_prepare($stmt, "SELECT id, label FROM test ORDER BY id ASC LIMIT 2") || !mysqli_stmt_execute($stmt)) {
    printf("[022] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
}
if (!is_object($res = mysqli_stmt_get_result($stmt)) || 'mysqli_result' != get_class($res)) {
    printf("[023] Expecting object/mysqli_result got %s/%s, [%d] %s\n", gettype($res), $res, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
}
if (!in_array($res->type, array(MYSQLI_STORE_RESULT, MYSQLI_USE_RESULT))) {
    printf("[024] Unknown result set type %s\n", $res->type);
}
if ($res->type !== MYSQLI_STORE_RESULT) {
    printf("[025] Expecting int/%d got %s/%s", MYSQLI_STORE_RESULT, gettype($res->type), $res->type);
}
mysqli_free_result($res);
mysqli_stmt_close($stmt);
mysqli_close($link);
if (NULL !== ($res = mysqli_stmt_get_result($stmt))) {
Example #3
1
 public function safeinsert($table, $dataArray)
 {
     $field = "";
     $safeparam = "";
     $params = "";
     $paramarr = [];
     if (!is_array($dataArray) || count($dataArray) <= 0) {
         $this->halt('没有要插入的数据');
         return false;
     }
     $paramsnum = 0;
     while (list($key, $val) = each($dataArray)) {
         $nowtype = is_string($val) ? 's' : 'i';
         $paramarr[] = $val;
         $field .= "{$key},";
         $safeparam .= "?,";
         $paramsnum++;
         $params .= $nowtype;
         //之过滤字符串,int字形不用过滤
     }
     $field = substr($field, 0, -1);
     $safeparam = substr($safeparam, 0, -1);
     $sql = "insert into {$table}({$field}) values({$safeparam})";
     $stmt = mysqli_stmt_init($this->link);
     mysqli_stmt_prepare($stmt, $sql);
     array_unshift($paramarr, $stmt, $params);
     //把资源句柄和字符类型插入数组前两位
     //参数要传引用。具体见PHP手册mysqli_stmt_bind_param
     $parmlist = array();
     foreach ($paramarr as $key => $value) {
         $parmlist[$key] =& $paramarr[$key];
     }
     call_user_func_array("mysqli_stmt_bind_param", $parmlist);
     $result = mysqli_stmt_execute($stmt);
     $this->write_log("安全插入");
     if (!$result) {
         return false;
     }
     return true;
 }
Example #4
0
function update_vote($image_id)
{
    //get number of votes and update
    global $link;
    /*$result = mysqli_query($link, "SELECT `amount` FROM `votes_amount` WHERE `imageID`=".$image_id.";") or die(mysqli_error($link));
    	$amount = mysqli_fetch_assoc($result);
    	$new_amount = $amount['amount']+1;
    	mysqli_query($link, "UPDATE `votes_amount` SET `amount`=".$new_amount." WHERE `imageID`=".$image_id.";") or die(mysqli_error($link));*/
    $stmt = mysqli_stmt_init($link);
    mysqli_stmt_prepare($stmt, "SELECT `amount` FROM `votes_amount` WHERE `imageID`=?;") or die(mysqli_error($link));
    mysqli_stmt_bind_param($stmt, 'i', $image_id);
    mysqli_stmt_execute($stmt);
    $result = mysqli_stmt_get_result($stmt);
    mysqli_stmt_close($stmt);
    $amount = mysqli_fetch_assoc($result);
    $new_amount = $amount['amount'] + 1;
    $stmt = mysqli_prepare($link, "UPDATE `votes_amount` SET `amount`=" . $new_amount . " WHERE `imageID`=?;") or die(mysqli_error($link));
    mysqli_stmt_bind_param($stmt, 'i', $image_id);
    mysqli_stmt_execute($stmt);
    mysqli_stmt_close($stmt);
    //return ajax data
    if (isset($_SESSION['id']) && !isset($_POST['action']) && !isset($_POST['votePic'])) {
        $data = array('new_amount' => $new_amount, 'imageID' => $image_id);
    } elseif (isset($_POST['action']) && $_POST['action'] == 'anonymous_voting') {
        //get another two images
        $result = mysqli_query($link, "SELECT * FROM `image` ORDER BY RAND() LIMIT 2;") or die(mysqli_error($link));
        $data = array();
        while ($row = mysqli_fetch_assoc($result)) {
            $data[] = $row;
        }
    }
    mysqli_close($link);
    return $data;
}
Example #5
0
 public function insertItems(Items $items)
 {
     $con = self::openConnection();
     $affected = 0;
     mysqli_begin_transaction($con);
     $stm = mysqli_stmt_init($con);
     $sql = "INSERT INTO category VALUES (?, ?, ?)";
     mysqli_stmt_prepare($stm, $sql);
     foreach ($items->getItems() as $item) {
         $code = $item->getCode();
         $name = $item->getName();
         $parent = $item->getParent() == null ? null : $item->getParent()->getCode();
         mysqli_stmt_bind_param($stm, 'sss', $code, $name, $parent);
         mysqli_stmt_execute($stm);
         if (mysqli_affected_rows($con) == 1) {
             $affected++;
         }
     }
     if ($affected > 0) {
         mysqli_commit($con);
     } else {
         mysqli_rollback($con);
     }
     return $affected;
 }
Example #6
0
 public function insertItems(Items $items)
 {
     $con = self::openConnection();
     $affected = 0;
     mysqli_begin_transaction($con);
     $stm = mysqli_stmt_init($con);
     $sql = "INSERT INTO product VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
     mysqli_stmt_prepare($stm, $sql);
     foreach ($items->getItems() as $item) {
         $code = $item->getCode();
         $articul = $item->getArticul();
         $name = $item->getName();
         $bmuID = $item->getBasicMeasurementUnit() == null ? null : $item->getBasicMeasurementUnit()->getId();
         $price = $item->getPrice();
         $curID = $item->getCurrency() == null ? null : $item->getCurrency()->getId();
         $muID = $item->getMeasurementUnit() == null ? null : $item->getMeasurementUnit()->getId();
         $parent = $item->getParent() == null ? null : $item->getParent()->getCode();
         mysqli_stmt_bind_param($stm, 'sssdddds', $code, $articul, $name, $bmuID, $price, $curID, $muID, $parent);
         mysqli_stmt_execute($stm);
         if (mysqli_affected_rows($con) == 1) {
             $affected++;
         }
     }
     if ($affected > 0) {
         mysqli_commit($con);
     } else {
         mysqli_rollback($con);
     }
     return $affected;
 }
function bind_twice($link, $engine, $sql_type1, $sql_type2, $bind_type1, $bind_type2, $bind_value1, $bind_value2, $offset)
{
    if (!mysqli_query($link, "DROP TABLE IF EXISTS test_mysqli_stmt_bind_param_type_juggling_table_1")) {
        printf("[%03d + 1] [%d] %s\n", $offset, mysqli_errno($link), mysqli_error($link));
        return false;
    }
    mysqli_autocommit($link, true);
    $sql = sprintf("CREATE TABLE test_mysqli_stmt_bind_param_type_juggling_table_1(col1 %s, col2 %s) ENGINE=%s", $sql_type1, $sql_type2, $engine);
    if (!mysqli_query($link, $sql)) {
        printf("[%03d + 2] [%d] %s\n", $offset, mysqli_errno($link), mysqli_error($link));
        return false;
    }
    if (!($stmt = mysqli_stmt_init($link))) {
        printf("[%03d + 3] [%d] %s\n", $offset, mysqli_errno($link), mysqli_error($link));
        return false;
    }
    if (!mysqli_stmt_prepare($stmt, "INSERT INTO test_mysqli_stmt_bind_param_type_juggling_table_1(col1, col2) VALUES (?, ?)")) {
        printf("[%03d + 4] [%d] %s\n", $offset, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
        return false;
    }
    if (!mysqli_stmt_bind_param($stmt, $bind_type1 . $bind_type2, $bind_value1, $bind_value1)) {
        printf("[%03d + 5] [%d] %s\n", $offset, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
        return false;
    }
    if (!mysqli_stmt_execute($stmt)) {
        printf("[%03d + 6] [%d] %s\n", $offset, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
        return false;
    }
    if (!mysqli_stmt_bind_param($stmt, $bind_type1 . $bind_type2, $bind_value1, $bind_value2)) {
        printf("[%03d + 7] [%d] %s\n", $offset, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
        return false;
    }
    if (!mysqli_stmt_execute($stmt)) {
        printf("[%03d + 8] [%d] %s\n", $offset, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
        return false;
    }
    mysqli_stmt_close($stmt);
    if (!($res = mysqli_query($link, "SELECT col1, col2 FROM test_mysqli_stmt_bind_param_type_juggling_table_1"))) {
        printf("[%03d + 9] [%d] %s\n", $offset, mysqli_errno($link), mysqli_error($link));
        return false;
    }
    if (2 !== ($tmp = mysqli_num_rows($res))) {
        printf("[%03d + 10] Expecting 2 rows, got %d rows [%d] %s\n", $offset, $tmp, mysqli_errno($link), mysqli_error($link));
    }
    $row = mysqli_fetch_assoc($res);
    if ($row['col1'] != $bind_value1 || $row['col2'] != $bind_value1) {
        printf("[%03d + 11] Expecting col1 = %s, col2 = %s got col1 = %s, col2 = %s - [%d] %s\n", $offset, $bind_value1, $bind_value1, $row['col1'], $row['col2'], mysqli_errno($link), mysqli_error($link));
        return false;
    }
    $row = mysqli_fetch_assoc($res);
    if ($row['col1'] != $bind_value1 || $row['col2'] != $bind_value2) {
        printf("[%03d + 12] Expecting col1 = %s, col2 = %s got col1 = %s, col2 = %s - [%d] %s\n", $offset, $bind_value1, $bind_value2, $row['col1'], $row['col2'], mysqli_errno($link), mysqli_error($link));
        return false;
    }
    mysqli_free_result($res);
    return true;
}
function mysqli_query_insert($type, $len, $runs, $host, $user, $passwd, $db, $port, $socket)
{
    $errors = $times = array();
    foreach ($runs as $k => $run) {
        $times['INSERT ' . $type . ' ' . $run . 'x = #rows overall'] = microtime(true);
        do {
            if (!($link = @mysqli_connect($host, $user, $passwd, $db, $port, $socket))) {
                $errors[] = sprintf("INSERT %s %dx = #rows  connect failure (original code = %s)", $type, $run, $flag_original_code ? 'yes' : 'no');
                break 2;
            }
            if (!mysqli_query($link, "DROP TABLE IF EXISTS test")) {
                $errors[] = sprintf("INSERT %s %dx = #rows drop table failure (original code = %s): [%d] %s", $type, $run, $flag_original_code ? 'yes' : 'no', mysqli_errno($link), mysqli_error($link));
                break 2;
            }
            if (!mysqli_query($link, sprintf("CREATE TABLE test(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, label %s)", $type))) {
                $errors[] = sprintf("INSERT %s %dx = #rows create table failure (original code = %s): [%d] %s", $type, $run, $flag_original_code ? 'yes' : 'no', mysqli_errno($link), mysqli_error($link));
                break 2;
            }
            $label = '';
            for ($i = 0; $i < $len; $i++) {
                $label .= chr(mt_rand(65, 90));
            }
            $start = microtime(true);
            if (!($stmt = mysqli_stmt_init($link))) {
                $error[] = sprintf("INSERT %s %dx = #rows mysqli_stmt_init() failed (original code = %s): [%d] %s", $type, $run, $flag_original_code ? 'yes' : 'no', mysqli_errno($link), mysqli_error($link));
                break 2;
            }
            $ret = mysqli_stmt_prepare($stmt, "INSERT INTO test(id, label) VALUES (?, ?)");
            $times['INSERT ' . $type . ' ' . $run . 'x = #rows stmt_init() + stmt_prepare()'] += microtime(true) - $start;
            if (!$ret) {
                $error[] = sprintf("INSERT %s %dx = #rows mysqli_stmt_init() failed (original code = %s): [%d] %s", $type, $run, $flag_original_code ? 'yes' : 'no', mysqli_errno($link), mysqli_error($link));
                break 2;
            }
            $start = microtime(true);
            $ret = mysqli_stmt_bind_param($stmt, 'is', $i, $label);
            $times['INSERT ' . $type . ' ' . $run . 'x = #rows stmt_bind_param()'] += microtime(true) - $start;
            if (!$ret) {
                $error[] = sprintf("INSERT %s %dx = #rows mysqli_stmt_bind_param failed (original code = %s): [%d] %s", $type, $run, $flag_original_code ? 'yes' : 'no', mysqli_errno($link), mysqli_error($link));
                break 2;
            }
            for ($i = 1; $i <= $run; $i++) {
                $start = microtime(true);
                $ret = mysqli_stmt_execute($stmt);
                $times['INSERT ' . $type . ' ' . $run . 'x = #rows stmt_execute()'] += microtime(true) - $start;
                if (!$ret) {
                    $errors[] = sprintf("INSERT %s %dx = #rows stmt_execute failure (original code = %s): [%d] %s", $type, $run, $flag_original_code ? 'yes' : 'no', mysqli_errno($link), mysqli_error($link));
                    break 3;
                }
            }
            mysqli_stmt_close($stmt);
            mysqli_close($link);
        } while (false);
        $times['INSERT ' . $type . ' ' . $run . 'x = #rows overall'] = microtime(true) - $times['INSERT ' . $type . ' ' . $run . 'x = #rows overall'];
    }
    return array($errors, $times);
}
function func_test_mysqli_stmt_param_count_table_1_mysqli_stmt_param_count($stmt, $query, $expected, $offset)
{
    if (!mysqli_stmt_prepare($stmt, $query)) {
        printf("[%03d] [%d] %s\n", $offset, mysqli_stmt_errno($stmt), mysqli_error($stmt));
        return false;
    }
    if ($expected !== ($tmp = mysqli_stmt_param_count($stmt))) {
        printf("[%03d] Expecting %s/%d, got %s/%d\n", $offset + 3, gettype($expected), $expected, gettype($tmp), $tmp);
    }
    return true;
}
function test_format($link, $format, $from, $order_by, $expected, $offset)
{
    if (!($stmt = mysqli_stmt_init($link))) {
        printf("[%03d] Cannot create PS, [%d] %s\n", $offset, mysqli_errno($link), mysqli_error($link));
        return false;
    }
    if ($order_by) {
        $sql = sprintf('SELECT %s AS _format FROM %s ORDER BY %s', $format, $from, $order_by);
    } else {
        $sql = sprintf('SELECT %s AS _format FROM %s', $format, $from);
    }
    if (!mysqli_stmt_prepare($stmt, $sql)) {
        printf("[%03d] Cannot prepare PS, [%d] %s\n", $offset + 1, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
        return false;
    }
    if (!mysqli_stmt_execute($stmt)) {
        printf("[%03d] Cannot execute PS, [%d] %s\n", $offset + 2, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
        return false;
    }
    if (!mysqli_stmt_store_result($stmt)) {
        printf("[%03d] Cannot store result set, [%d] %s\n", $offset + 3, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
        return false;
    }
    if (!is_array($expected)) {
        $result = null;
        if (!mysqli_stmt_bind_result($stmt, $result)) {
            printf("[%03d] Cannot bind result, [%d] %s\n", $offset + 4, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
            return false;
        }
        if (!mysqli_stmt_fetch($stmt)) {
            printf("[%03d] Cannot fetch result,, [%d] %s\n", $offset + 5, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
            return false;
        }
        if ($result !== $expected) {
            printf("[%03d] Expecting %s/%s got %s/%s with %s - %s.\n", $offset + 6, gettype($expected), $expected, gettype($result), $result, $format, $sql);
        }
    } else {
        $order_by_col = $result = null;
        if (!mysqli_stmt_bind_result($stmt, $order_by_col, $result)) {
            printf("[%03d] Cannot bind result, [%d] %s\n", $offset + 7, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
            return false;
        }
        reset($expected);
        while ((list($k, $v) = each($expected)) && mysqli_stmt_fetch($stmt)) {
            if ($result !== $v) {
                printf("[%03d] Row %d - expecting %s/%s got %s/%s [%s] with %s - %s.\n", $offset + 8, $k, gettype($v), $v, gettype($result), $result, $order_by_col, $format, $sql);
            }
        }
    }
    mysqli_stmt_free_result($stmt);
    mysqli_stmt_close($stmt);
    return true;
}
Example #11
0
/**
 * This function removes record with id $_GET[ID'] from the table Jobs
 */
function deleteJob()
{
    global $db_conn;
    $redirectlocation = "index.php?action=jobs";
    $stmt = mysqli_stmt_init($db_conn);
    global $db_conn;
    $sql = "DELETE FROM `jobs` WHERE `JobID` = ?";
    if (!mysqli_stmt_prepare($stmt, $sql)) {
        print "Failed to prepare statement\n";
    } else {
        mysqli_stmt_bind_param($stmt, "i", $_GET['id']);
        mysqli_execute($stmt);
        mysqli_stmt_close($stmt);
    }
    immediate_redirect_to($redirectlocation);
    // return to jobs
}
Example #12
0
function saveOrder($dt)
{
    global $link, $basket;
    $goods = myBasket();
    $stmt = mysqli_stmt_init($link);
    $sql = 'INSERT INTO orders(title, author, pubyear,price, quantity, orderid, datetime)
          VALUES(?,?,?,?,?,?,?)';
    if (mysqli_stmt_prepare($stmt, $sql)) {
        return false;
    }
    foreach ($goods as $item) {
        mysqli_stmt_bind_param($stmt, 'ssiiisi', $item['title'], $item['author'], $item['pubyear'], $item['price'], $item['quantity'], $basket['orderid'], $dt);
        mysqli_stmt_execute($stmt);
        mysqli_stmt_close($stmt);
        setcookie('basket', "", time() - 3600);
        return true;
    }
}
Example #13
0
function db_query($sql, $bind = null)
{
    $db = get_var('db');
    $query = false;
    $stmt = mysqli_stmt_init($db);
    $sql = trim($sql);
    if (mysqli_stmt_prepare($stmt, $sql)) {
        if (!empty($bind)) {
            $types = '';
            $values = array();
            foreach ($bind as $key => &$value) {
                $value = stripslashes($value);
                if (is_numeric($value)) {
                    $float = floatval($value);
                    $types .= $float && intval($float) != $float ? 'd' : 'i';
                } else {
                    $types .= 's';
                }
                $values[$key] =& $bind[$key];
            }
            $params = array_merge(array($stmt, $types), $bind);
            call_user_func_array('mysqli_stmt_bind_param', $params);
        }
        if (mysqli_stmt_execute($stmt)) {
            if (preg_match('/^(SELECT|SHOW)/i', $sql)) {
                if (db_native_driver()) {
                    $query = mysqli_stmt_get_result($stmt);
                    mysqli_stmt_close($stmt);
                } else {
                    return $stmt;
                }
            } else {
                $query = TRUE;
                mysqli_stmt_close($stmt);
            }
        } else {
            trigger_error(mysqli_stmt_error($stmt), E_USER_WARNING);
        }
    } else {
        trigger_error(mysqli_error($db), E_USER_WARNING);
    }
    return $query;
}
Example #14
0
function executeQuery($conn, $sql, array $parameters = []){
	/*For matching the data type for binding*/
	$typesTable = [
		'integer' => 'i',
		'double' => 'd',
		'string' => 's'
	];
	$type = '';
	$stmt = mysqli_stmt_init($conn);
	
	if (!mysqli_stmt_prepare($stmt, $sql)){
		raiseIssue('failed to prepare statement');
		return false;
	}
	/*This bit should only run if any parameters are provided*/
	if (!empty($parameters)){
		foreach ($parameters as $parameter){
			/*Look up the type from the types table */
			$type .= $typesTable[gettype($parameter)];
		}
		array_unshift($parameters, $stmt, $type);
		/*bit hacky because of call_user_func_array, it will not like $parameters by itself so it needs to be "passed in by reference" but calltime pass by reference is deprecated*/
		$preparedParams = [];
		foreach ($parameters as $index => &$label){
			$preparedParams[$index] = &$label;
		}
		
		call_user_func_array('mysqli_stmt_bind_param', $preparedParams);
	}
	mysqli_stmt_execute($stmt);
	
	/*Generating the result set for use. This gives you the column names as keys on each row*/
	$result = mysqli_stmt_get_result($stmt);
	$resultSet = [];
	if(!$result){ return $resultSet; /*skips the result fetching if no results obtained*/}
	while ($row = mysqli_fetch_assoc($result)){
		$resultSet[] = $row;
	}
	mysqli_stmt_close($stmt);
	
	return $resultSet;
}
function func_test_mysqli_stmt_num_rows($stmt, $query, $expected, $offset)
{
    if (!mysqli_stmt_prepare($stmt, $query)) {
        printf("[%03d] [%d] %s\n", $offset, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
        return false;
    }
    if (!mysqli_stmt_execute($stmt)) {
        printf("[%03d] [%d] %s\n", $offset + 1, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
        return false;
    }
    if (!mysqli_stmt_store_result($stmt)) {
        printf("[%03d] [%d] %s\n", $offset + 2, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
        return false;
    }
    if ($expected !== ($tmp = mysqli_stmt_num_rows($stmt))) {
        printf("[%03d] Expecting %s/%d, got %s/%d\n", $offset + 3, gettype($expected), $expected, gettype($tmp), $tmp);
    }
    mysqli_stmt_free_result($stmt);
    return true;
}
Example #16
0
 public function insertUnits(Units $units)
 {
     $con = self::openConnection();
     $affected = 0;
     mysqli_begin_transaction($con);
     $stm = mysqli_stmt_init($con);
     $sql = "INSERT INTO currency (code) VALUE (?)";
     mysqli_stmt_prepare($stm, $sql);
     foreach ($units->getUnits() as $unit) {
         $code = $unit->getCode();
         mysqli_stmt_bind_param($stm, 's', $code);
         mysqli_stmt_execute($stm);
         if (mysqli_affected_rows($con) == 1) {
             $affected++;
         }
     }
     if ($affected > 0) {
         mysqli_commit($con);
     } else {
         mysqli_rollback($con);
     }
     return $affected;
 }
Example #17
0
function add_login($user_id, $username, $passwd)
{
    if (!($db_link = get_connection())) {
        return -1;
    }
    $sql = 'insert into login (id, username, passwd) values (?,?,?)';
    $stmt = mysqli_stmt_init($db_link);
    if (mysqli_stmt_prepare($stmt, $sql)) {
        mysqli_stmt_bind_param($stmt, 'iss', $user_id, $username, $passwd);
        if (mysqli_stmt_execute($stmt)) {
            mysqli_stmt_close($stmt);
            mysqli_close($db_link);
            return 1;
        } else {
            mysqli_stmt_close($stmt);
            mysqli_close($db_link);
            return -2;
        }
    } else {
        mysqli_close($db_link);
        return -2;
    }
}
Example #18
0
 public static function prepare($conn, $sqlElement, $params, $bind = true)
 {
     if ($conn && strlen($sqlElement) > 0) {
         $sql = mysqli_stmt_init($conn);
         mysqli_stmt_prepare($sql, (string) $sqlElement);
         if (!$bind) {
             return $sql;
         }
         if (is_array($params)) {
             $t = "";
             $cnt = count($params);
             for ($i = 0; $i < $cnt; $i++) {
                 $v = $params[$i];
                 if (is_string($v)) {
                     $t .= "s";
                 } else {
                     if (is_int($v)) {
                         $t .= "i";
                     } else {
                         if (is_float($v)) {
                             $t .= "d";
                         } else {
                             $t .= "b";
                         }
                     }
                 }
                 $ar[] =& $params[$i];
             }
             if ($t) {
                 call_user_func_array('mysqli_stmt_bind_param', array_merge(array($sql, $t), $ar));
             }
         }
         return $sql;
     }
     return false;
 }
}
// FIXME - different versions return different values ?!
if (NULL !== ($tmp = mysqli_stmt_fetch($stmt)) && false !== $tmp) {
    printf("[006] Expecting NULL or boolean/false, got %s/%s\n", gettype($tmp), $tmp);
}
if (!mysqli_stmt_execute($stmt)) {
    printf("[007] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
}
if (true !== ($tmp = mysqli_stmt_fetch($stmt))) {
    printf("[008] NULL, got %s/%s\n", gettype($tmp), $tmp);
}
mysqli_stmt_close($stmt);
if (!($stmt = mysqli_stmt_init($link))) {
    printf("[009] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
}
if (!mysqli_stmt_prepare($stmt, "SELECT id, label FROM test ORDER BY id LIMIT 2")) {
    printf("[010] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
}
if (!mysqli_stmt_execute($stmt)) {
    printf("[011] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
}
$id = NULL;
$label = NULL;
if (true !== ($tmp = mysqli_stmt_bind_result($stmt, $id, $label))) {
    printf("[012] Expecting boolean/true, got %s/%s\n", gettype($tmp), $tmp);
}
if (true !== ($tmp = mysqli_stmt_fetch($stmt))) {
    printf("[013] Expecting boolean/true, got %s/%s, [%d] %s\n", gettype($tmp), $tmp, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
}
if (!mysqli_kill($link, mysqli_thread_id($link))) {
    printf("[014] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
function func_mysqli_stmt_get_result($link, $engine, $bind_type, $sql_type, $bind_value, $offset, $type_hint = null)
{
    if (!mysqli_query($link, "DROP TABLE IF EXISTS test_mysqli_stmt_get_result_types_table_1")) {
        printf("[%04d] [%d] %s\n", $offset, mysqli_errno($link), mysqli_error($link));
        return false;
    }
    if (!mysqli_query($link, sprintf("CREATE TABLE test_mysqli_stmt_get_result_types_table_1(id INT, label %s, PRIMARY KEY(id)) ENGINE = %s", $sql_type, $engine))) {
        // don't bail - column type might not be supported by the server, ignore this
        return false;
    }
    if (!($stmt = mysqli_stmt_init($link))) {
        printf("[%04d] [%d] %s\n", $offset + 1, mysqli_errno($link), mysqli_error($link));
        return false;
    }
    if (!mysqli_stmt_prepare($stmt, "INSERT INTO test_mysqli_stmt_get_result_types_table_1(id, label) VALUES (?, ?)")) {
        printf("[%04d] [%d] %s\n", $offset + 2, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
        return false;
    }
    $id = null;
    if (!mysqli_stmt_bind_param($stmt, "i" . $bind_type, $id, $bind_value)) {
        printf("[%04d] [%d] %s\n", $offset + 3, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
        mysqli_stmt_close($stmt);
        return false;
    }
    for ($id = 1; $id < 4; $id++) {
        if (!mysqli_stmt_execute($stmt)) {
            printf("[%04d] [%d] %s\n", $offset + 3 + $id, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
            mysqli_stmt_close($stmt);
            return false;
        }
    }
    mysqli_stmt_close($stmt);
    $stmt = mysqli_stmt_init($link);
    if (!mysqli_stmt_prepare($stmt, "SELECT id, label FROM test_mysqli_stmt_get_result_types_table_1")) {
        printf("[%04d] [%d] %s\n", $offset + 7, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
        mysqli_stmt_close($stmt);
        return false;
    }
    if (!mysqli_stmt_execute($stmt)) {
        printf("[%04d] [%d] %s\n", $offset + 8, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
        mysqli_stmt_close($stmt);
        return false;
    }
    $result = mysqli_stmt_result_metadata($stmt);
    if (!($res = mysqli_stmt_get_result($stmt))) {
        printf("[%04d] [%d] %s\n", $offset + 9, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
        mysqli_stmt_close($stmt);
        return false;
    }
    $num = 0;
    $fields = mysqli_fetch_fields($result);
    while ($row = mysqli_fetch_assoc($res)) {
        $bind_res =& $row['label'];
        if (!gettype($bind_res) == 'unicode') {
            if ($bind_res !== $bind_value && (!$type_hint || $type_hint !== gettype($bind_res))) {
                printf("[%04d] [%d] Expecting %s/'%s' [type hint = %s], got %s/'%s'\n", $offset + 10, $num, gettype($bind_value), $bind_value, $type_hint, gettype($bind_res), $bind_res);
                mysqli_free_result($res);
                mysqli_stmt_close($stmt);
                return false;
            }
        }
        $num++;
    }
    if ($num != 3) {
        printf("[%04d] [%d] %s, expecting 3 results, got only %d results\n", $offset + 11, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt), $num);
        mysqli_free_result($res);
        mysqli_stmt_close($stmt);
        return false;
    }
    mysqli_free_result($res);
    mysqli_stmt_close($stmt);
    return true;
}
}
$label = null;
if (mysqli_stmt_bind_param($stmt, "s", $label)) {
    printf("[014] expected error - got ok\n");
}
while (mysqli_stmt_fetch($stmt)) {
    if (1 !== ($tmp = mysqli_stmt_field_count($stmt))) {
        printf("[015] Expecting int/1, got %s/%s\n", gettype($tmp), $tmp);
    }
}
if (!mysqli_stmt_prepare($stmt, 'INSERT INTO test(id) VALUES (100)')) {
    printf("[016] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
}
if (0 !== ($tmp = mysqli_stmt_field_count($stmt))) {
    printf("[017] Expecting int/0, got %s/%s\n", gettype($tmp), $tmp);
}
if (!mysqli_stmt_prepare($stmt, "UPDATE test SET label = 'z' WHERE id = 1") || !mysqli_stmt_execute($stmt)) {
    printf("[018] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
}
if (0 !== ($tmp = mysqli_stmt_field_count($stmt))) {
    printf("[019] Expecting int/0, got %s/%s\n", gettype($tmp), $tmp);
}
mysqli_stmt_close($stmt);
if (mysqli_stmt_prepare($stmt, 'SELECT id FROM test')) {
    printf("[020] Prepare should fail, statement has been closed\n");
}
if (!is_null($tmp = mysqli_stmt_field_count($stmt))) {
    printf("[011] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
}
mysqli_close($link);
print "done!";
function func_mysqli_stmt_get_result_geom($link, $engine, $sql_type, $bind_value, $offset)
{
    if (!mysqli_query($link, "DROP TABLE IF EXISTS test_mysqli_stmt_get_result_geom_table_1")) {
        printf("[%04d] [%d] %s\n", $offset, mysqli_errno($link), mysqli_error($link));
        return false;
    }
    if (!mysqli_query($link, sprintf("CREATE TABLE test_mysqli_stmt_get_result_geom_table_1(id INT, label %s, PRIMARY KEY(id)) ENGINE = %s", $sql_type, $engine))) {
        // don't bail - column type might not be supported by the server, ignore this
        return false;
    }
    for ($id = 1; $id < 4; $id++) {
        $sql = sprintf("INSERT INTO test_mysqli_stmt_get_result_geom_table_1(id, label) VALUES (%d, %s)", $id, $bind_value);
        if (!mysqli_query($link, $sql)) {
            printf("[%04d] [%d] %s\n", $offset + 2 + $id, mysqli_errno($link), mysqli_error($link));
        }
    }
    if (!($stmt = mysqli_stmt_init($link))) {
        printf("[%04d] [%d] %s\n", $offset + 6, mysqli_errno($link), mysqli_error($link));
        return false;
    }
    if (!mysqli_stmt_prepare($stmt, "SELECT id, label FROM test_mysqli_stmt_get_result_geom_table_1")) {
        printf("[%04d] [%d] %s\n", $offset + 7, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
        mysqli_stmt_close($stmt);
        return false;
    }
    if (!mysqli_stmt_execute($stmt)) {
        printf("[%04d] [%d] %s\n", $offset + 8, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
        mysqli_stmt_close($stmt);
        return false;
    }
    if (!($res = mysqli_stmt_get_result($stmt))) {
        printf("[%04d] [%d] %s\n", $offset + 9, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
        mysqli_stmt_close($stmt);
        return false;
    }
    $result = mysqli_stmt_result_metadata($stmt);
    $fields = mysqli_fetch_fields($result);
    if ($fields[1]->type != MYSQLI_TYPE_GEOMETRY) {
        printf("[%04d] [%d] %s wrong type %d\n", $offset + 10, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt), $fields[1]->type);
    }
    $num = 0;
    while ($row = mysqli_fetch_assoc($res)) {
        $bind_res =& $row['label'];
        if (!($stmt2 = mysqli_stmt_init($link))) {
            printf("[%04d] [%d] %s\n", $offset + 11, mysqli_errno($link), mysqli_error($link));
            return false;
        }
        if (!mysqli_stmt_prepare($stmt2, "INSERT INTO test_mysqli_stmt_get_result_geom_table_1(id, label) VALUES (?, ?)")) {
            printf("[%04d] [%d] %s\n", $offset + 12, mysqli_stmt_errno($stmt2), mysqli_stmt_error($stmt2));
            return false;
        }
        $id = $row['id'] + 10;
        if (!mysqli_stmt_bind_param($stmt2, "is", $id, $bind_res)) {
            printf("[%04d] [%d] %s\n", $offset + 13, mysqli_stmt_errno($stmt2), mysqli_stmt_error($stmt2));
            return false;
        }
        if (!mysqli_stmt_execute($stmt2)) {
            printf("[%04d] [%d] %s\n", $offset + 14, mysqli_stmt_errno($stmt2), mysqli_stmt_error($stmt2));
            return false;
        }
        mysqli_stmt_close($stmt2);
        if (!($res_normal = mysqli_query($link, sprintf("SELECT id, label FROM test_mysqli_stmt_get_result_geom_table_1 WHERE id = %d", $row['id'] + 10)))) {
            printf("[%04d] [%d] %s\n", $offset + 15, mysqli_errno($link), mysqli_error($link));
            return false;
        }
        if (!($row_normal = mysqli_fetch_assoc($res_normal))) {
            printf("[%04d] [%d] %s\n", $offset + 16, mysqli_errno($link), mysqli_error($link));
            return false;
        }
        if ($row_normal['label'] != $bind_res) {
            printf("[%04d] PS and non-PS return different data.\n", $offset + 17);
            return false;
        }
        mysqli_free_result($res_normal);
        $num++;
    }
    if ($num != 3) {
        printf("[%04d] [%d] %s, expecting 3 results, got only %d results\n", $offset + 18, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt), $num);
        mysqli_free_result($res);
        mysqli_stmt_close($stmt);
        return false;
    }
    mysqli_free_result($res);
    mysqli_stmt_close($stmt);
    return true;
}
Example #23
0
require 'table.inc';
if (!($stmt = mysqli_stmt_init($link))) {
    printf("[003] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
}
// properly initialized?
if ('' !== ($tmp = mysqli_stmt_error($stmt))) {
    printf("[004] Expecting int/0, got %s/%s\n", gettype($tmp), $tmp);
}
if (mysqli_stmt_prepare($stmt, "SELECT i_do_not_exist_believe_me FROM test_mysqli_stmt_error_table_1 ORDER BY id")) {
    printf("[005] Statement should have failed!\n");
}
// set after error server?
if ('' === ($tmp = mysqli_stmt_error($stmt))) {
    printf("[006] Expecting string/any non empty, got %s/%s\n", gettype($tmp), $tmp);
}
if (!mysqli_stmt_prepare($stmt, "SELECT id FROM test_mysqli_stmt_error_table_1 ORDER BY id")) {
    printf("[007] [%d] %s\n", mysqli_stmt_error($stmt), mysqli_stmt_error($stmt));
}
// reset after error & success
if ('' !== ($tmp = mysqli_stmt_error($stmt))) {
    printf("[008] Expecting empty string, got %s/%s\n", gettype($tmp), $tmp);
}
mysqli_kill($link, mysqli_thread_id($link));
if (true === ($tmp = mysqli_stmt_execute($stmt))) {
    printf("[009] Expecting boolean/false, got %s/%s\n", gettype($tmp), $tmp);
}
// set after client error
if ('' === ($tmp = mysqli_stmt_error($stmt))) {
    printf("[010] Execting string/any non empty, got %s/%s\n", gettype($tmp), $tmp);
}
mysqli_stmt_close($stmt);
if (!mysqli_stmt_prepare($stmt, 'SELECT id, label FROM test_mysqli_stmt_bind_param_call_user_func_table_1 WHERE id = ?')) {
    printf("[046] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
}
$id = 1;
$params = array(0 => 'i', 1 => &$id);
if (!call_user_func_array(array($stmt, 'bind_param'), $params)) {
    printf("[047] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
}
if (!mysqli_stmt_execute($stmt)) {
    printf("[048] [%d] (Message might vary with MySQL Server version, e.g. No data supplied for parameters in prepared statement)\n", mysqli_stmt_errno($stmt));
}
mysqli_stmt_close($stmt);
if (!($stmt = mysqli_stmt_init($link))) {
    printf("[049] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
}
if (!mysqli_stmt_prepare($stmt, 'SELECT id, label FROM test_mysqli_stmt_bind_param_call_user_func_table_1 WHERE id = ?')) {
    printf("[050] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
}
$types = 'i';
$id = 1;
$params = array(0 => $stmt, 1 => 'i', 2 => &$id);
if (!call_user_func_array('mysqli_stmt_bind_param', $params)) {
    printf("[051] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
}
if (!mysqli_stmt_execute($stmt)) {
    printf("[052] [%d] (Message might vary with MySQL Server version, e.g. No data supplied for parameters in prepared statement)\n", mysqli_stmt_errno($stmt));
}
print "done!";
error_reporting(0);
$test_table_name = 'test_mysqli_stmt_bind_param_call_user_func_table_1';
require_once "clean_table.inc";
Example #25
0
include_once 'mysql.php';
if (isset($_POST['id']) && isset($_POST['name'])) {
    $result = mysqli_query($link, "SELECT * FROM `user` WHERE `userFBID`=" . $_POST['id'] . " AND `userFBNAME`='" . $_POST['name'] . "';") or die(mysqli_error($link));
    $row = mysqli_num_rows($result);
    if ($row == '0') {
        mysqli_query($link, "INSERT INTO `user`(`userFBID`, `userFBNAME`, `active`) VALUES(" . $_POST['id'] . ", '" . $_POST['name'] . "', '1');") or die(mysqli_error($link));
    }
    $result = mysqli_query($link, "SELECT `userID` FROM `user` WHERE `userFBID` = '" . $_POST['id'] . "' AND `userFBNAME` = '" . $_POST['name'] . "';") or die(mysqli_error($link));
    $id = mysqli_fetch_assoc($result);
    session_start();
    $_SESSION['id'] = $id['userID'];
    mysqli_close($link);
}
if (isset($_POST['email']) && isset($_POST['password'])) {
    $stmt = mysqli_stmt_init($link);
    mysqli_stmt_prepare($stmt, "SELECT `email`, `password`, `active` FROM `user` WHERE `email`=?;") or die(mysqli_error($link));
    mysqli_stmt_bind_param($stmt, 's', $_POST['email']);
    mysqli_stmt_execute($stmt);
    $result = mysqli_stmt_get_result($stmt);
    mysqli_stmt_close($stmt);
    $user = mysqli_fetch_assoc($result);
    if (!empty($user)) {
        if ($user['active'] == '0') {
            $hash = hash('md5', $_POST["email"]);
            $stmt = mysqli_prepare($link, "UPDATE `verify_email` SET `email`=?, `hash`='" . $hash . "';") or die(mysqli_error($link));
            mysqli_stmt_bind_param($stmt, 's', $_POST['email']);
            mysqli_stmt_execute($stmt);
            mysqli_stmt_close($stmt);
            mysqli_close($link);
            $to = $_POST['email'];
            $subject = 'Email varification';
Example #26
0
$tmp = NULL;
$link = NULL;
$test_table_name = 'test_mysqli_stmt_sqlstate_table_1';
require 'table.inc';
if (!($stmt = mysqli_stmt_init($link))) {
    printf("[004] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
}
if (NULL !== ($tmp = mysqli_stmt_sqlstate($stmt))) {
    printf("[005] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
}
if (!mysqli_stmt_prepare($stmt, "SELECT id FROM test_mysqli_stmt_sqlstate_table_1")) {
    printf("[006] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
}
if ('00000' !== ($tmp = mysqli_stmt_sqlstate($stmt))) {
    printf("[007] Expecting string/00000, got %s/%s. [%d] %s\n", gettype($tmp), $tmp, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
}
if (mysqli_stmt_prepare($stmt, "SELECT believe_me FROM i_dont_belive_that_this_table_exists")) {
    printf("[008] Should fail! [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
}
if ('' === ($tmp = mysqli_stmt_sqlstate($stmt))) {
    printf("[009] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
}
mysqli_stmt_close($stmt);
if (NULL !== ($tmp = mysqli_stmt_sqlstate($stmt))) {
    printf("[010] Expecting NULL, got %s/%s\n");
}
mysqli_close($link);
print "done!";
error_reporting(0);
$test_table_name = 'test_mysqli_stmt_sqlstate_table_1';
require_once "clean_table.inc";
if (NULL !== ($tmp = mysqli_stmt_get_warnings($stmt))) {
    printf("[004] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
}
if (!mysqli_stmt_prepare($stmt, "DROP TABLE IF EXISTS test") || !mysqli_stmt_execute($stmt)) {
    printf("[005] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
}
if (false !== ($tmp = mysqli_stmt_get_warnings($stmt))) {
    printf("[006] Expecting boolean/false, got %s/%s\n", gettype($tmp), $tmp);
}
if (!mysqli_stmt_prepare($stmt, "CREATE TABLE test(id SMALLINT, label CHAR(1))") || !mysqli_stmt_execute($stmt)) {
    printf("[007] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
}
if (false !== ($tmp = mysqli_stmt_get_warnings($stmt))) {
    printf("[008] Expecting boolean/false, got %s/%s\n", gettype($tmp), $tmp);
}
if (!mysqli_stmt_prepare($stmt, "INSERT INTO test(id, label) VALUES (100000, 'a'), (100001, 'b')") || !mysqli_stmt_execute($stmt)) {
    printf("[009] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
}
if (!is_object($warning = mysqli_stmt_get_warnings($stmt))) {
    printf("[010] Expecting mysqli_warning object, got %s/%s\n", gettype($warning), $warning);
}
if ('mysqli_warning' !== get_class($warning)) {
    printf("[011] Expecting object of type mysqli_warning got type '%s'", get_class($warning));
}
if (!method_exists($warning, 'next')) {
    printf("[012] Object mysqli_warning seems to lack method next()\n");
}
$i = 0;
do {
    if ('' == $warning->message) {
        printf("[013 - %d] Message should not be empty\n", $i);
function func_mysqli_stmt_bind_datatype($link, $engine, $bind_type, $sql_type, $bind_value, $offset, $alternative = null)
{
    if (!mysqli_query($link, "DROP TABLE IF EXISTS test")) {
        printf("[%03d] [%d] %s\n", $offset, mysqli_errno($link), mysqli_error($link));
        return false;
    }
    if (!mysqli_query($link, sprintf("CREATE TABLE test(id INT NOT NULL, label %s, PRIMARY KEY(id)) ENGINE = %s", $sql_type, $engine))) {
        // don't bail - it might be that the server does not support the data type
        return false;
    }
    if (!($stmt = mysqli_stmt_init($link))) {
        printf("[%03d] [%d] %s\n", $offset + 1, mysqli_errno($link), mysqli_error($link));
        return false;
    }
    if (!mysqli_stmt_prepare($stmt, "INSERT INTO test(id, label) VALUE (?, ?)")) {
        printf("[%03d] [%d] %s\n", $offset + 2, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
        return false;
    }
    $id = 1;
    if (!mysqli_stmt_bind_param($stmt, "i" . $bind_type, $id, $bind_value)) {
        printf("[%03d] [%d] %s\n", $offset + 3, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
        return false;
    }
    if (!mysqli_stmt_execute($stmt)) {
        printf("[%03d] [%d] %s\n", $offset + 4, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
        return false;
    }
    mysqli_stmt_close($stmt);
    if (!($res = mysqli_query($link, "SELECT id, label FROM test"))) {
        printf("[%03d] [%d] %s\n", $offset + 5, mysqli_errno($link), mysqli_error($link));
        return false;
    }
    if (!($row = mysqli_fetch_assoc($res))) {
        printf("[%03d] [%d] %s\n", $offset + 5, mysqli_errno($link), mysqli_error($link));
        return false;
    }
    if ($alternative) {
        if ($row['id'] != $id || $row['label'] != $bind_value && $row['label'] != $alternative) {
            printf("[%03d] Testing '%s', '%s': expecting '%s'/'%s' (%s), got '%s'/'%s'\n", $offset + 6, $bind_type, $sql_type, $id, $bind_value, gettype($bind_value), $row['id'], $row['label']);
            return false;
        }
    } else {
        if ($row['id'] != $id || $row['label'] != $bind_value) {
            printf("[%03d] Testing '%s', '%s': expecting '%s'/'%s', got '%s'/'%s'\n", $offset + 6, $bind_type, $sql_type, $id, $bind_value, $row['id'], $row['label']);
            return false;
        }
    }
    mysqli_free_result($res);
    return true;
}
function testStatement($offset, $link, $sql, $expected_lib, $expected_mysqlnd, $check_mysqlnd, $compare)
{
    if (!($stmt = mysqli_stmt_init($link))) {
        printf("[%04d - %s] [%d] %s\n", $offset, $sql, mysqli_errno($link), mysqli_error($link));
        return false;
    }
    if (!@mysqli_stmt_prepare($stmt, $sql)) {
        /* Not all server versions will support all statements */
        /* Failing to prepare is OK */
        return true;
    }
    if (empty($expected_lib) && false !== $res) {
        printf("[%04d - %s] No metadata expected\n", $offset + 1, $sql);
        return false;
    } else {
        if (!empty($expected_lib) && false == $res) {
            printf("[%04d - %s] Metadata expected\n", $offset + 2, $sql);
            return false;
        }
    }
    if (!empty($expected_lib)) {
        if (!is_object($res)) {
            printf("[%04d - %s] [%d] %s\n", $offset + 3, $sql, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
            return false;
        }
        if (get_class($res) != 'mysqli_result') {
            printf("[%04d - %s] Expecting object/mysqli_result got object/%s\n", $offset + 4, $sql, get_class($res));
            return false;
        }
        $meta = array('num_fields' => mysqli_num_fields($res), 'fetch_field' => mysqli_fetch_field($res), 'fetch_field_direct0' => mysqli_fetch_field_direct($res, 0), 'fetch_field_direct1' => @mysqli_fetch_field_direct($res, 1), 'fetch_fields' => count(mysqli_fetch_fields($res)), 'field_count' => $res->field_count, 'field_seek-1' => @mysqli_field_seek($res, -1), 'field_seek0' => mysqli_field_seek($res, 0), 'field_tell' => mysqli_field_tell($res));
        if (is_object($meta['fetch_field'])) {
            $meta['fetch_field']->charsetnr = 'ignore';
            $meta['fetch_field']->flags = 'ignore';
        }
        if (is_object($meta['fetch_field_direct0'])) {
            $meta['fetch_field_direct0']->charsetnr = 'ignore';
            $meta['fetch_field_direct0']->flags = 'ignore';
        }
        if (is_object($meta['fetch_field_direct1'])) {
            $meta['fetch_field_direct1']->charsetnr = 'ignore';
            $meta['fetch_field_direct1']->flags = 'ignore';
        }
        mysqli_free_result($res);
        if ($meta != $expected_lib) {
            printf("[%04d - %s] Metadata differs from expected values\n", $offset + 5, $sql);
            var_dump($meta);
            var_dump($expected_lib);
            return false;
        }
    }
    if (function_exists('mysqli_stmt_get_result')) {
        /* mysqlnd only */
        if (!mysqli_stmt_execute($stmt)) {
            printf("[%04d - %s] [%d] %s\n", $offset + 6, $sql, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
            return false;
        }
        $res = mysqli_stmt_get_result($stmt);
        if (false === $res && !empty($expected_mysqlnd)) {
            printf("[%04d - %s] Expecting resultset [%d] %s\n", $offset + 7, $sql, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
            return false;
        } else {
            if (empty($expected_mysqlnd) && false !== $res) {
                printf("[%04d - %s] Unexpected resultset [%d] %s\n", $offset + 8, $sql, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
                return false;
            }
        }
        if (!is_object($res)) {
            printf("[%04d - %s] [%d] %s\n", $offset + 9, $sql, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
            return false;
        }
        if ('mysqli_result' != get_class($res)) {
            printf("[%04d - %s] Expecting object/mysqli_result got object/%s\n", $offset + 10, $sql, get_class($res));
            return false;
        }
        $meta_res = array('num_fields' => mysqli_num_fields($res), 'fetch_field' => mysqli_fetch_field($res), 'fetch_field_direct0' => mysqli_fetch_field_direct($res, 0), 'fetch_field_direct1' => @mysqli_fetch_field_direct($res, 1), 'fetch_fields' => count(mysqli_fetch_fields($res)), 'field_count' => mysqli_field_count($link), 'field_seek-1' => @mysqli_field_seek($res, -1), 'field_seek0' => mysqli_field_seek($res, 0), 'field_tell' => mysqli_field_tell($res));
        if (is_object($meta_res['fetch_field'])) {
            $meta_res['fetch_field']->charsetnr = 'ignore';
            $meta_res['fetch_field']->flags = 'ignore';
        }
        if (is_object($meta_res['fetch_field_direct0'])) {
            $meta_res['fetch_field_direct0']->charsetnr = 'ignore';
            $meta_res['fetch_field_direct0']->flags = 'ignore';
        }
        if (is_object($meta_res['fetch_field_direct1'])) {
            $meta_res['fetch_field_direct1']->charsetnr = 'ignore';
            $meta_res['fetch_field_direct1']->flags = 'ignore';
        }
        mysqli_free_result($res);
        if ($check_mysqlnd && $meta_res != $expected_mysqlnd) {
            printf("[%04d - %s] Metadata differs from expected\n", $offset + 11, $sql);
            var_dump($meta_res);
            var_dump($expected_mysqlnd);
        } else {
            if ($meta_res['field_count'] < 1) {
                printf("[%04d - %s] Metadata seems wrong, no fields?\n", $offset + 12, $sql);
                var_dump($meta_res);
                var_dump(mysqli_fetch_assoc($res));
            }
        }
        if ($compare && $meta_res != $meta) {
            printf("[%04d - %s] Metadata returned by mysqli_stmt_result_metadata() and mysqli_stmt_get_result() differ\n", $offset + 13, $sql);
            var_dump($meta_res);
            var_dump($meta);
        }
    }
    mysqli_stmt_close($stmt);
    return true;
}
require 'table.inc';
if (!($stmt = mysqli_stmt_init($link))) {
    printf("[003] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
}
// properly initialized?
if ('' !== ($tmp = mysqli_stmt_error($stmt))) {
    printf("[004] Expecting int/0, got %s/%s\n", gettype($tmp), $tmp);
}
if (mysqli_stmt_prepare($stmt, "SELECT i_do_not_exist_believe_me FROM test ORDER BY id")) {
    printf("[005] Statement should have failed!\n");
}
// set after error server?
if ('' === ($tmp = mysqli_stmt_error($stmt))) {
    printf("[006] Expecting string/any non empty, got %s/%s\n", gettype($tmp), $tmp);
}
if (!mysqli_stmt_prepare($stmt, "SELECT id FROM test ORDER BY id")) {
    printf("[007] [%d] %s\n", mysqli_stmt_error($stmt), mysqli_stmt_error($stmt));
}
// reset after error & success
if ('' !== ($tmp = mysqli_stmt_error($stmt))) {
    printf("[008] Expecting empty string, got %s/%s\n", gettype($tmp), $tmp);
}
mysqli_kill($link, mysqli_thread_id($link));
if (true === ($tmp = mysqli_stmt_execute($stmt))) {
    printf("[009] Expecting boolean/false, got %s/%s\n", gettype($tmp), $tmp);
}
// set after client error
if ('' === ($tmp = mysqli_stmt_error($stmt))) {
    printf("[010] Execting string/any non empty, got %s/%s\n", gettype($tmp), $tmp);
}
mysqli_stmt_close($stmt);