Questions tagged [mysqli]

The mysqli PHP extension is a PHP database driver. Not to be confused with MySQL database.

The mysqli PHP extension is the successor to the mysql PHP extension. It provides a low-level mapping to MySQL's C interface. The extension's principle features consist of:

  • An object-oriented interface
  • Support for prepared statements
  • Support for multiple statements
  • Support for transactions
  • Enhanced debugging support
  • Embedded server support

Deprecation of the mysql extension

The mysql extension was deprecated in PHP version 5.5, and removed in version 7.0. Code written for modern servers must use the mysqli or pdo extensions instead.

In addition to an object-oriented interface, most mysqli features also provide an equivalent procedural interface through functions prefixed mysqli_. However, these functions were primarily intended for users transitioning away from legacy code using the mysql extension. Code in a modern environment is expected to use object-oriented programming.

The mysqli extension's prepared statement support makes use of ? placeholders bound to variable references for input, and variable references bound to columns when fetching output rows. Please note that, in order to use some aspects of mysqli prepared statements (most notably mysqli_stmt_get_result), your installation of PHP must use the Mysql Native Driver (mysqlnd), which also provides improved performance over the older MySQL Client Library.

A simple MySQLi SELECT query example:

The following example retrieves 2 output columns from a SELECT query using both an integer and a string parameter.

// A form post has supplied the input values in:
// $_POST['fruit']
// $_POST['age']

// Enable mysqli error reporting. Errors will be reported as exceptions
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

// Open a new connection to the MySQL server.
$mysqli = new mysqli("host", "user", "password", "database");

// Set the correct connection charset
$mysqli->set_charset('utf8mb4');

// SQL string with input placeholders
$SQL = "SELECT firstName, email FROM users WHERE favorite_fruit = ? AND age > ?";

// Prepare the statement
$stmt = $mysqli->prepare($SQL);

// Bind parameters:
// "s" indicates a string parameter (favorite_fruit)
// "i" indicates an integer parameter (age)
$stmt->bind_param("si", $_POST['fruit'], $_POST['age']);

// Execute the statement
$stmt->execute();

// Bind result variables to fetch the columns returned:
// Supply one variable for each column. Variables are bound by reference
$stmt->bind_result($firstName, $email);

// Fetch rows:
// On each loop iteration, the variables $firstName, $email will be 
// populated with values from the currently fetched row.
while ($stmt->fetch()) {
    echo "Name: $firstName, Email: $email\n";
}

// Close the prepared statement (optional)
$stmt->close();

Related tags

24962 questions
32
votes
2 answers

Converting MySQL result array to JSON

I want to convert my database query's result array to JSON format in PHP. Here is my code: $row = mysql_fetch_array($result) I want to convert $row to JSON format and pass the JSON data to a jQuery plugin.
user213559
32
votes
9 answers

Pass by reference problem with PHP 5.3.1

Ok, this is a weird problem, so please bear with me as I explain. We upgraded our dev servers from PHP 5.2.5 to 5.3.1. Loading up our code after the switch, we start getting errors like: Warning: Parameter 2 to mysqli_stmt::bind_param() expected to…
Spot
  • 7,962
  • 9
  • 46
  • 55
31
votes
1 answer

Why is object oriented PHP with mysqli better than the procedural approach?

I have been using the procedural approach with mysql* until recently. Now I want to shift to mysqli and object oriented approach. Many online resources and books state that OOP is better than procedural even in case of PHP. After going through some…
Stacy J
  • 2,721
  • 15
  • 58
  • 92
31
votes
3 answers

PHP Commands Out of Sync error

I am using two prepared statements in PHP/MySQLi to retrieve data from a mysql database. However, when I run the statements, I get the "Commands out of sync, you can't run the command now" error. Here is my code: $stmt =…
user191125
  • 321
  • 1
  • 3
  • 5
31
votes
6 answers

How do I display a MySQL error in PHP for a long query that depends on the user input?

In PHP, I am trying to execute a long MySQL query that depends on the user input. However, my query fails with the following message, "Query Failed". Actually I have printed this message whenever the query fails, but I am having hard time looking…
Traveling Salesman
  • 2,209
  • 11
  • 46
  • 83
30
votes
6 answers

Call to a member function bind_param() on a non-object

I am trying to bind a variable in this prepared statement, but i keep receiving the error: Call to a member function bind_param() on a non-object The function is called, and variables are passed to it. When i change the function to just echo the…
mcbeav
  • 11,893
  • 19
  • 54
  • 84
30
votes
3 answers

Turning query errors to Exceptions in MySQLi

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…
Roman Newaza
  • 11,405
  • 11
  • 58
  • 89
29
votes
5 answers

using nulls in a mysqli prepared statement

In a mysqli prepared statement, a NULL gets turned into '' (in the case of a string) or 0 (in the case of an integer). I would like to store it as a true NULL. Is there any way of doing this?
ceejayoz
  • 176,543
  • 40
  • 303
  • 368
29
votes
14 answers

Codeigniter: fatal error call to undefined function mysqli_init()

I just changed my server and experience these errors below: Fatal error: Call to undefined function mysqli_init() in /home/blacktwitter/public_html/system/database/drivers/mysqli/mysqli_driver.php on line 126 A PHP Error was encountered Severity:…
Bsos
  • 341
  • 2
  • 5
  • 10
29
votes
3 answers

PHP UPDATE prepared statement

I'm trying to learn the proper way to use prepared statements to avoid SQL injections etc. When I execute the script I get a message from my script saying 0 Rows Inserted, I expect this to say 1 Rows Inserted and of course update the table. I'm not…
user0129e021939232
  • 6,205
  • 24
  • 87
  • 140
28
votes
1 answer

Fetching one row only with MySQLi

How can I only fetch one INDEXED row with MySQLi? I'm currently doing this: $row = $result->fetch(MYSQLI_ASSOC); $row = $row[0]; Is there another way? I'm aware of mysqli_fetch_row but it doesn't return an associative array.
oaziz
  • 1,362
  • 1
  • 17
  • 32
28
votes
7 answers

Why is mysqli giving a "Commands out of sync" error?

I am trying to run the following.
JM4
  • 6,740
  • 18
  • 77
  • 125
28
votes
6 answers

mysqli + xdebug breakpoint after closing statement result in many warnings

I have a piece of code like this: $conn = new mysqli($host, $username, $passwd, $dbname); ... $stmt = $conn->prepare('SELECT ...'); $stmt->bind_param(...); $stmt->execute(); $stmt->bind_result(...); while($stmt->fetch()) { // do something…
AbcAeffchen
  • 14,400
  • 15
  • 47
  • 66
28
votes
2 answers

Difference between mysql & mysqli

Possible Duplicate: mysql vs mysqli in php What is the difference between mysql_* functions and mysqli_* functions? Is there any technical reasons behind we shouldn't use mysql_* functions?
Natasha
  • 980
  • 4
  • 16
  • 33
28
votes
9 answers

Difference between "->" and "::" in PHP MySQLi OOP

Can anyone tell the difference between mysqli->commit and mysqli::commit? The header in this page is mysqli::commit, but in examples they use mysqli->commit.
John