Viewed   238 times

I'm trying to turn MySQLi query errors to Exceptions, but couldn't - mysqli_sql_exception is thrown only if it failed to connect the DB.

I used mysqli_report(MYSQLI_REPORT_STRICT) and procedural MySQLi functions embedded to custom wrapper class.

Former code:

public function mysqlQuery($SQL) {

    $this->Result = mysqli_query($this->DBlink, $SQL);

    if($this->Result === false)
        throw new MySQLiQueryException($SQL, mysqli_error($this->DBlink), mysqli_errno($this->DBlink));

    return $this->Result;


Question: Is it normal no Warning, nor Exception are thrown when query fails so I have to check if mysqli_query() returned false?



Some time ago I managed to sort this matter out. As it was pointed out in the other answer,


is a correct way to tell mysqli to throw exceptions.

Just make sure you don't wrap every query in a try-catch. This is a very common misconception that as soon as you started using exceptions you should start throwing tries and catches left and right. Quite contrary, try-catch should be used warily. While 99% of your errors shouldn't be handled in place, but rather by a site-wide error handler. You may read more on the topic from my article on PHP error reporting

Monday, October 31, 2022

I went ahead and ran a test where one query uses a prepared statement, and the other builds the entire query then executes that. I'm probably not making what I'm wanting to know easy to understand.

Here's my test code. I was thinking prepared statements sort of held back execution until a $stmt->close() was called to optimize it or something. That doesn't appear to be the case though as the test that builds the query using real_escape_string is at least 10 times faster.


$db = new mysqli('localhost', 'user', 'pass', 'test');

$start = microtime(true);
$a = 'a';
$b = 'b';

$sql = $db->prepare('INSERT INTO multi (a,b) VALUES(?, ?)');
$sql->bind_param('ss', $a, $b);
for($i = 0; $i < 10000; $i++)
    $a = chr($i % 1);
    $b = chr($i % 2);

echo microtime(true) - $start;


Sunday, November 6, 2022

This actually depends on the Mysql server. The default max size for all data combined in the entire query is 1mb. See:

If your data combined is under that "max_allowed_packet" threshold, just use "s" for the binding type for any text field. Infact, you can usually get away with using "s" for any field type at all (date, float, etc).

If your entire entry combined that you want to insert is over 1mb (or whatever you reset it to) in length, you'll want to use mysqli_stmt::send_long_data method and the "b" binding type to send this particular field in chunks.

Wednesday, August 24, 2022

You need to retrieve a mysqli_result object first, with something like...

$res = $stmt->get_result();

... then fetch the number of rows from this object (not $stmt):

$numrows = mysqli_num_rows($res);

UPDATE: get_result method is available in PHP 5.3+ only, for the older versions one should use the following approach:

// $stmt preparing code goes here...

$num_rows = $stmt->num_rows;

// processing cycle:
$stmt->bind_result($some_param, $another_param);
while ($stmt->fetch()) {
   doSomethingElseWith($some_param, $another_param);

As a sidenote, two recommendations: 1) it'd be probably faster to use a single query here and look for value both in TeacherAlias and TeacherUsername fields simultaneously (with OR operator, like TeacherAlias = ? OR TeacherUsername = ?); 2) it'd be easier to work with explicitly stated columns (SELECT id, TeacherAlias AS alias, TeacherUsername AS username...) instead of just (SELECT *) in your query.

Wednesday, December 7, 2022

There are many ways to do it, I guess the most similar would be:

$fieldName = mysqli_fetch_field_direct($result, $i)->name;

Monday, December 26, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :