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
24
votes
2 answers

Where are php's extensions .so files located?

I opened some some ini files like mysqli.ini , mysql.ini , pdo_mysql.ini. Inside those files there is an .so extension added for those files. I want to know where these .so files are stored. Inside mysqli.ini file ; configuration for php MySQL…
Dinesh Gowda
  • 1,044
  • 3
  • 13
  • 29
24
votes
11 answers

Warning: mysqli_real_escape_string() expects exactly 2 parameters, 1 given... what I do wrong?

I try make php login but I get this error: Warning: mysqli_real_escape_string() expects exactly 2 parameters, 1 given, what I do wrong? register.php
SuperTroll
  • 447
  • 1
  • 4
  • 15
24
votes
3 answers

Fatal error: Call to undefined method mysqli_result::fetch_all()

I have problems with PHP in Ubuntu 10.04. When I try use mysqli_result::fetch_all this error appears: Call to undefined method mysqli_result::fetch_all() However, it works in Windows XP. The Code: $result = $this->dbh->query('SELECT [...] '); return…
Stokres
  • 685
  • 2
  • 7
  • 12
23
votes
11 answers

Getting error mysqli::real_connect(): (HY000/2002): No such file or directory when I try to access my project on live server

I had uploaded my website to server and then I tried to access, but the code igniter returns me that error that I don't find any answer. Why is this happening? My config database is set like: $db['default'] = array( 'dsn' => '', 'hostname' =>…
William
  • 467
  • 1
  • 6
  • 19
23
votes
1 answer

Advantages Of MySQLi over MySQL

What are the advantages of using MySQLi over MySQL?
Ben Shelock
  • 20,154
  • 26
  • 92
  • 125
23
votes
2 answers

mysqli: can it prepare multiple queries in one statement?

I would like to know if i can prepare one mysqli statement that executes multiple queries: mysqli->prepare(query1 ...1,2,3 param...; query2...4,5 param...); or mysqli->prepare(insert into ...1,2,3 param...; insert into...4,5 param...); and after…
Donovant
  • 3,091
  • 8
  • 40
  • 68
22
votes
7 answers

SELECT * FROM in MySQLi

My site is rather extensive, and I just recently made the switch to PHP5 (call me a late bloomer). All of my MySQL query's before were built as such: "SELECT * FROM tablename WHERE field1 = 'value' && field2 = 'value2'"; This made it very easy,…
johnnietheblack
  • 13,050
  • 28
  • 95
  • 133
22
votes
5 answers

PHP MySQL over SSL. Peer certificate did not match

I'm trying to use Google Cloud SQL over SSL from GCE(Google Compute Engine) instance. My problem is that I cannot connect to Cloud SQL instance over SSL. mysql command works normally. I can connect to Cloud SQL instance with certification…
zono
  • 8,366
  • 21
  • 75
  • 113
22
votes
4 answers

Mysqli fetch_assoc vs fetch_array

When I'm returning one row from a table, to gather the results I usually use e.g.: $info = $result->fetch_assoc(); What is the difference between that and: $info = $result->fetch_array(); Is there a reason to use one over the other when…
StudioTime
  • 22,603
  • 38
  • 120
  • 207
22
votes
4 answers

Prepared Statements - Number of Rows

I'm just trying to figure out how to determine the number of rows and then make that number display in the HTML. My prepared statement looks like this: if($stmt = $mysqli -> prepare("SELECT field1, field2, field3 FROM table WHERE id= ?ORDER BY id…
Kevin
  • 1,685
  • 7
  • 28
  • 55
22
votes
7 answers

Bind Param with array of parameters

I have a function that does this: function registerUser($firstName, $lastName, $address, $postcode, $email, $password) { $params = array($firstName, $lastName, $address, $postcode, $email, $password); $result = $this->db->bind("INSERT INTO…
David G
  • 6,803
  • 4
  • 28
  • 51
22
votes
4 answers

Why is mysqli_insert_id() always returning 0?

I have the following code. The mysqli_insert_id() (in this case "$last_row"), which is supposed to return the last row of the table, is always returning 0. Why is it so?
Mumbo Jumbo
  • 360
  • 1
  • 2
  • 12
21
votes
2 answers

mysqli_free_result necessary?

I was looking through my code and read that it was recommened to use mysqli_free_result when your result object is not needed anymore. But after seeing that each query is outputed to the $result variable repeatedly throughout the script, I am…
JoJo
  • 221
  • 1
  • 2
  • 5
21
votes
2 answers

mysqli_select_db() expects parameter 1 to be mysqli, string given

I am new to Mysqli_* and I am getting these errors: Warning: mysqli_select_db() expects parameter 1 to be mysqli, string given in D:\Hosting\9864230\html\includes\connection.php on line 11 Warning: mysqli_error() expects exactly 1 parameter, 0…
user1551909
  • 461
  • 1
  • 4
  • 9
21
votes
2 answers

Moving from mysql connection to mysqli

I have a function I used to use with my scripts. openDBConn() and closeDBConn() These both called the mysql_connect() function. Used as seen below openDBConn(); $car_model = getCarModel($car_id); $car_color =…
Jms Bnd
  • 1,213
  • 3
  • 13
  • 18