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
21
votes
5 answers

Set character set using MySQLi

I'm fetching data in Arabic from MySQL tables with MySQLi. So I usually use this in procedural style: mysql_query("SET NAMES 'utf8'"); mysql_query('SET CHARACTER SET utf8'); Now I am using the OOP style so I am trying to see if there is something…
sys_debug
  • 3,883
  • 17
  • 67
  • 98
21
votes
3 answers

What are client-side prepared statements?

Trying to learn something new - specifically trying to choose wether to use MySQLi or PDO for future projects when working with MySQL - I stumbled upon this page which shows an overview of options available to me. At the bottom of this page is a…
Repox
  • 15,015
  • 8
  • 54
  • 79
20
votes
6 answers

MYSQLi error: User already has more than 'max_user_connections' active connections

I have this error below on a site I am running. I don't understand why is that as it works fine on my localhost. Is it something to do with the host? I am on an Unix server. Warning: mysqli::mysqli() [mysqli.mysqli]: (42000/1203): User dbo343879423…
Run
  • 54,938
  • 169
  • 450
  • 748
20
votes
10 answers

PHP Startup Unable to load dynamic library /usr/lib/php/20151012/php_mysqli.dll

I have ubuntu 14.04 EC2 instance. I have installed php 7 on it. when I execute any php command like php --version or any other. I get following error PHP Warning: PHP Startup: Unable to load dynamic library '/usr/lib/php/20151012/php_mysqli.dll' -…
Jass
  • 3,345
  • 3
  • 24
  • 41
20
votes
5 answers

Strange problem with the length of the field gotten from database

I am using php 5.4 with this backwards compatibility script: https://github.com/ircmaxell/password_compat/blob/master/lib/password.php that shouldn't matter though, because I can get the hashing and verification process working in my registration…
Cbas
  • 6,003
  • 11
  • 56
  • 87
20
votes
1 answer

Having a problem getting mysqli_query to execute

Here's the problem: I started a swap today to use mysqli. No biggie, just had to change a few statements. Everything went fine, no errors... Except that I can't get it to execute any queries at all. I have double and triple checked my syntax. I even…
GilloD
  • 561
  • 2
  • 6
  • 18
20
votes
3 answers

How do I configure php to enable pdo and include mysqli on CentOS?

PHP Version 5.3.3 on CentOS (x86_64, RHEL 6) Apparently my PHP installation was configured to exclude Mysqli and disable PDO for some reason. Therefore, I believe this is what's causing them to not work when I try to use the php command from the…
Lakitu
  • 424
  • 1
  • 4
  • 12
19
votes
8 answers

How to bind mysqli bind_param arguments dynamically in PHP?

I have been learning to use prepared and bound statements for my sql queries, and I have come out with this so far, it works okay but it is not dynamic at all when comes to multiple parameters or when there no parameter needed, public function…
Run
  • 54,938
  • 169
  • 450
  • 748
19
votes
2 answers

mysqli_fetch_assoc() performance PHP5.4 vs PHP7.0

I have large MySQL query (1.8M rows, 25 columns) and I need to make 2 dimensional array from it (memory table based on primary key). Code works as expected, but $table creation takes a long time in PHP7.0. What is the reason why PHP7.0 performs so…
Zmrzka
  • 310
  • 1
  • 11
19
votes
1 answer

check if the query returns zero rows

I am using this code, but I don't understand how to check if the query returns zero rows. How can I check that? $results = $mysqli->query("SELECT ANNOUNCE_NUMBER,ANNOUNCEMENTS,ANNOUNCE_TYPE,POST_DATE FROM home ORDER BY ANNOUNCE_NUMBER DESC"); while…
user3196424
  • 537
  • 3
  • 8
  • 16
19
votes
3 answers

Warning: mysqli_query() expects parameter 1 to be mysqli, null given in

I am trying to build a simple custom CMS, but I'm getting an error: Warning: mysqli_query() expects parameter 1 to be MySQLi, null given in Why am I getting this error? All my code is already MySQLi and I am using two parameters, not…
Philip
  • 569
  • 2
  • 5
  • 27
19
votes
3 answers

Retrieving Multiple Result sets with stored procedure in php/mysqli

I have a stored procedure that has multiple result sets. How do I advance to the 2nd result set in mysqli to get those results? Let's say it's a stored proc like: create procedure multiples( param1 INT, param2 INT ) BEGIN SELECT * FROM table1 WHERE…
MacAnthony
  • 4,471
  • 2
  • 23
  • 26
19
votes
17 answers

The mysqli extension is missing. Please check your PHP configuration

I have looked through all of the forums that I could find relevant to this question and my problem yet nothing works. I have apache2.2 with php5, phpMyAdmin, and MySQL. I have uncommented the extension, I have checked my phpinfo() and mysqli does…
Wesley
  • 259
  • 1
  • 3
  • 12
18
votes
2 answers

Cannot pass parameter by reference in MySQLi

I am trying to pass a string into my MySQLi prepared statement but it gives me the error: Cannot pass parameter by reference in MySQLi Here is the relevant code: $kv = json_encode(array($key => $value)); $stmt->prepare("insert into rules…
chustar
  • 12,225
  • 24
  • 81
  • 119
18
votes
9 answers

How to get an integer from MySQL as integer in PHP?

When data is returned from MySQL, it is automatically returned as strings, regardless of the MySQL data type. Is there any way to tell MySQL/PHP to maintain the data types (e.g. int), so if you query an int column, you get an integer in PHP instead…
fbwb
  • 199
  • 1
  • 2
  • 5