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
16
votes
3 answers

a permanent way of doing mysqli->set_charset()?

after setting all config file and runtime options for charset that i can find to utf-8, new mysqli connections made with php still has its charset set to latin1, which effectively means that i have to call $mysqli->set_charset('utf8') each time i…
Huang Tao
  • 2,254
  • 2
  • 26
  • 31
16
votes
1 answer

How to perform UPDATE with mysqli->prepare?

As I know there is a way to input data into a mysql database with mysqli, where you do not have to use mysql_real_escape_string. I mean like this: $stmt = $mysqli->prepare("INSERT INTO CountryLanguage VALUES (?, ?, ?, ?)"); …
phpheini
  • 163
  • 1
  • 1
  • 4
16
votes
3 answers

bind_result into an array PHP mysqli prepared statement

wondering how i could bind the results of a PHP prepared statement into an array and then how i could go about calling them. for example this query $q = $DBH->prepare("SELECT * FROM users WHERE username = ?"); $q->bind_param("s",…
mcbeav
  • 11,893
  • 19
  • 54
  • 84
16
votes
5 answers

Call to a member function execute() on boolean in

My html :
        Email : 
        Message : 
                
    
My rent.php file…
idkn
  • 422
  • 2
  • 7
  • 13
16
votes
2 answers

mysqli persistent connection

In short, is there some sort of mysqli_pconnect for high-usage PHP & MySQL servers, or do I need to stick with mysql unimproved? And if so, why did they remove it?
Aaron Yodaiken
  • 19,163
  • 32
  • 103
  • 184
16
votes
7 answers

What is difference between mysqli_fetch_array and MYSQLI_BOTH?

I know that mysqli_fetch_assoc, mysqli_fetch_array, mysqli_fetch However, is MYSQLI_BOTH equal to mysqli_fetch_array or are they in fact different?
user5755890
16
votes
5 answers

mysqli too many connections (HY000/1040) and (08004/1040) is there a difference?

I'm getting the following errors in my script: mysqli_connect(): (08004/1040): Too many connections mysqli_connect(): (HY000/1040): Too many connections What is the difference and how can I solve this problem?
ahmadMarafa
  • 1,316
  • 1
  • 13
  • 15
16
votes
7 answers

MySQLI binding params using call_user_func_array

Please see below my code. I am attempting to bind an array of paramenters to my prepared statement. I've been looking around on the web and can see I have to use call_user_func_array but cannot get it to work. The error I get is: "First argument is…
Columbo
  • 2,896
  • 7
  • 44
  • 54
16
votes
4 answers

new mysqli(): how to intercept an 'unable to connect' error?

I'm doing this (yes, I'm using wrong connection data, it's to force a connection error ) try { $connection = new mysqli('localhost', 'my_user', 'my_password', 'my_db') ; } catch (Exception $e ) { echo "Service unavailable"; exit…
realtebo
  • 23,922
  • 37
  • 112
  • 189
16
votes
4 answers

Using wildcards in prepared statement

I'm trying to run the following query, and I'm having trouble with the wildcard. function getStudents() { global $db; $users = array(); $query = $db->prepare("SELECT id, adminRights FROM users WHERE classes LIKE ? &&…
Michael Irwin
15
votes
5 answers

When should I use MySQLi instead of MySQL?

Can someone clarify for me what the advantages and disadvantages of using MySQLi instead of MySQL? Are there situations where I should not use MySQLi? Do I need to configure my server differently in order to use MySQLi? For instance, do I need to…
Semere Taézaz Sium
  • 4,036
  • 3
  • 21
  • 26
15
votes
3 answers

How do I ensure I caught all errors from MySQLi::multi_query?

The docs for multi_query say: Returns FALSE if the first statement failed. To retrieve subsequent errors from other statements you have to call mysqli_next_result() first. The docs for next_result say: Returns TRUE on success or FALSE on…
Billy ONeal
  • 104,103
  • 58
  • 317
  • 552
15
votes
1 answer

PHP + MySql + Stored Procedures, how do I get access an "out" value?

Documentation is severely lacking on anything to do with stored procedures in mysql with PHP. I currently have a stored procedure that I call via PHP, how can I get the value of an out parameter?
mmattax
  • 27,172
  • 41
  • 116
  • 149
15
votes
2 answers

Is there a way to get the default value of autocommit in MySQL?

I'm just wondering as per the title if this can be done? For example, if I was to do something like this: $this->db->autocommit(false); But then afterwards I wanted to set it back to it's default value; I can't just assume it's defaults to true and…
Brett
  • 19,449
  • 54
  • 157
  • 290
15
votes
7 answers

Empty string in not-null column in MySQL?

I used to use the standard mysql_connect(), mysql_query(), etc statements for doing MySQL stuff from PHP. Lately I've been switching over to using the wonderful MDB2 class. Along with it, I'm using prepared statements, so I don't have to worry about…
davr
  • 18,877
  • 17
  • 76
  • 99