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
342
votes
13 answers

mysqli or PDO - what are the pros and cons?

In our place we're split between using mysqli and PDO for stuff like prepared statements and transaction support. Some projects use one, some the other. There is little realistic likelihood of us ever moving to another RDBMS. I prefer PDO for the…
Polsonby
  • 22,825
  • 19
  • 59
  • 74
171
votes
6 answers

MySQL vs MySQLi when using PHP

Which is better, MySQL or MySQLi? And why? Which should I use? I mean better not just in terms of performance, but any other relevant feature.
Anand
  • 7,654
  • 9
  • 46
  • 60
157
votes
1 answer

What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such

In my local/development environment, the MySQLi query is performing OK. However, when I upload it on my web host environment, I get this error: Fatal error: Call to a member function bind_param() on a non-object in... Here is the code: global…
siopaoman
  • 1,733
  • 2
  • 12
  • 7
129
votes
10 answers

Call to undefined method mysqli_stmt::get_result

Here's my code: include 'conn.php'; $conn = new Connection(); $query = 'SELECT EmailVerified, Blocked FROM users WHERE Email = ? AND SLA = ? AND `Password` = ?'; $stmt = $conn->mysqli->prepare($query); $stmt->bind_param('sss', $_POST['EmailID'],…
Kumar Kush
  • 2,495
  • 11
  • 32
  • 42
129
votes
4 answers

What is the difference between MySQL, MySQLi and PDO?

What is the difference between MySQL, MySQLi and PDO? Which one is the best suited to use with PHP-MySQL?
Chintan Parekh
  • 1,291
  • 2
  • 9
  • 4
128
votes
26 answers

How to solve "Fatal error: Class 'MySQLi' not found"?

I am doing a tutorial and am getting this error: Fatal error: Class 'MySQLi' not found (LONG URL) on line 8 The code on line 8 is: $mysqli = new MySQLi($db_server, $db_user, $db_pass, $db_name); I saw online someone said to see if it was turned…
Tylor
  • 1,363
  • 2
  • 9
  • 12
125
votes
24 answers

Commands out of sync; you can't run this command now

I am trying to execute my PHP code, which calls two MySQL queries via mysqli, and get the error "Commands out of sync; you can't run this command now". Here is the code I am using
Joshxtothe4
  • 4,061
  • 10
  • 53
  • 83
117
votes
4 answers

Can I mix MySQL APIs in PHP?

I have searched the net and so far what I have seen is that you can use mysql_ and mysqli_ together meaning:
N3mo
  • 1,363
  • 2
  • 8
  • 11
110
votes
19 answers

mysqli_real_connect(): (HY000/2002): No such file or directory

mysqli_real_connect(): (HY000/2002): No such file or directory PhpMyAdmin error on MacOS. I want answer I really have no idea what I need to do to resolve this.
luigi
  • 1,111
  • 2
  • 7
  • 3
100
votes
11 answers

Do I have to guard against SQL injection if I used a dropdown?

I understand that you should NEVER trust user input from a form, mainly due to the chance of SQL injection. However, does this also apply to a form where the only input is from a dropdown(s) (see below)? I'm saving the $_POST['size'] to a Session…
Tatters
  • 1,187
  • 2
  • 9
  • 18
95
votes
12 answers

On a function that gets settings from a DB I ran into the error

I'm busy on a function that gets settings from a DB, and suddenly, I ran into this error: Fatal error: Call to a member function bind_param() on boolean in C:\xampp2\htdocs\application\classes\class.functions.php on line 16 Normally, this would…
sushibrain
  • 2,712
  • 5
  • 33
  • 62
94
votes
14 answers

Headers and client library minor version mismatch

In PHP I'm getting the following warning whenever I try to connect to a database (via mysql_connect) Warning: mysql_connect(): Headers and client library minor version mismatch. Headers:50162 Library:50524 In my php -i output I have the following…
Ian Hunter
  • 9,466
  • 12
  • 61
  • 77
92
votes
8 answers

How can I enable the MySQLi extension in PHP 7?

I have installed PHP 7 and MySQL 5.5.47 on Ubuntu 14.04 (Trusty Tahr). I have checked installed extension using: sudo apt-cache search php7-* It outputs: php7.0-common - Common files for packages built from the PHP source libapache2-mod-php7.0 -…
Mohammad Sayeed
  • 2,025
  • 1
  • 16
  • 27
90
votes
4 answers

Example of how to use bind_result vs get_result

I would like to see an example of how to call using bind_result vs. get_result and what would be the purpose of using one over the other. Also the pro and cons of using each. What is the limitation of using either and is there a difference.
Arian Faurtosh
  • 17,987
  • 21
  • 77
  • 115
85
votes
3 answers

MySQLi prepared statements error reporting

I'm trying to get my head around MySQli and I'm confused by the error reporting. I am using the return value of the MySQLi 'prepare' statement to detect errors when executing SQL, like this: $stmt_test = …
Columbo
  • 2,896
  • 7
  • 44
  • 54
1
2 3
99 100