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

Warning: mysqli_query(): Couldn't fetch mysqli

I have a problem where I can not retrieve the result from my MySQL database (via PHP). I use the same function in other places and it works flawlessly. However at this point i keep getting the "Warning: mysqli_query(): Couldn't fetch mysqli" error.…
Dennis
  • 3,044
  • 2
  • 33
  • 52
15
votes
5 answers

check if a variable is of type mysqli object?

how do i check if a variable is of a type mysqli object?
ajsie
  • 77,632
  • 106
  • 276
  • 381
15
votes
1 answer

Prepared statements and floats

I am playing with my own implementation of Google maps and I want to do it properly through prepared statements and mysqli object (not mysql as stated in the linked example) Also, I added "own registration" so I am afraid of SQL Injection. But, how…
Pavel Janicek
  • 14,128
  • 14
  • 53
  • 77
15
votes
4 answers

How to prevent duplicate usernames when people register?

I have been making a login/register system and I am drawing close to finishing my register portion of code. The only problem I am running into is how to make it so that users cannot register with duplicated usernames. I want it to work so that my…
Zippylicious
  • 185
  • 2
  • 5
  • 12
15
votes
7 answers

Connecting to old MySQL servers

I understand that PHP's mysql_* functions are deprecated and I should not be using them. However, we have several legacy MySQL 4.0 databases, which neither the mysqli_* nor PDO functions support connecting to. What is the best way to continue using…
jcsanyi
  • 8,133
  • 2
  • 29
  • 52
15
votes
2 answers

Checking if mysqli_query returned any values?

I have the following code and I would like to know if mysqli_query returned any rows and if so return an error. $result = mysqli_query($connection, "SELECT * FROM users"); if ($result == "") { echo "No records found"; } else { echo…
Peter Jewicz
  • 664
  • 1
  • 10
  • 27
15
votes
2 answers

get array of rows with mysqli result

I need to get all the rows from result object. I’m trying to build a new array that will hold all rows. Here is my code: $sql = new mysqli($config['host'],$config['user'],$config['pass'],$config['db_name']); if (mysqli_connect_errno()) { …
Yaniv Golan
  • 982
  • 5
  • 15
  • 28
15
votes
3 answers

phpmyadmin don't see new created databases

I create db via mysql: CREATE DATABASE `my_db` CHARACTER SET utf8 COLLATE utf8_general_ci; mysql>show databases - my_db is shown, then i'm login to phpmyadmin and don't see this db, only - information_schema, phpmyadmin, test p.s. ubuntu 12.04,…
user123321
  • 235
  • 1
  • 2
  • 7
15
votes
2 answers

After SP Insert, next page have an empty result until reloaded

I have a Stored Procedure (SP from now on) that inserts data to the database (SaveClient, see below). When the SP is done I redirect the PHP page to a different PHP page that lists the entries (FetchObjectList, see below). The list does not return…
Henrik Ammer
  • 1,889
  • 13
  • 26
15
votes
5 answers

PHP variables don't evaluate in the query

I am pretty new to PHP and MySQL and I just can't figure this one out. I have searched all around the forum but haven't found an answer I can make sense of. I originally was using mysql_fetch_assoc() but I could only search numbers and I received…
user1504463
  • 179
  • 1
  • 1
  • 4
14
votes
2 answers

How to resolve 'cannot pass parameter by reference' error in PHP?

Here's my code: $stmt = $conn->mysqli->prepare('INSERT INTO photos (CaseNo, ImageName, CaptureTime, UploadTime) VALUES (?,?,?,?)'); $stmt->bind_param('isss', $caseno, $index.'.'.$extension, date('Y-m-d H:i:s'), date('Y-m-d H:i:s')); I have tried…
Kumar Kush
  • 2,495
  • 11
  • 32
  • 42
14
votes
5 answers

Inefficient SQL Query

I'm building a simple web app at the moment that I'll one day open source. As it stands at the moment, the nav is generated on every page load (which will change to be cached one day) but for the moment, it's being made with the code below. Using…
PaulAdamDavis
  • 1,574
  • 3
  • 16
  • 19
14
votes
7 answers

result of prepared select statement as array

I would like to get the complete result of a prepared statement as an array (key/value pairs) in order to later use it in a str_replace() function. My table has three columns, an index and the fields "x1" and "x2". I used the following…
Johannes
  • 64,305
  • 18
  • 73
  • 130
14
votes
3 answers

How to use mysqli connection with SSL

I'm trying to make a secure connection with my database I wrote the following code:
NielsDePils
  • 241
  • 1
  • 2
  • 15
14
votes
1 answer

Correct way to use LIKE '%{$var}%' with prepared statements?

This does not work $sql = 'SELECT * FROM `users` WHERE username LIKE \'%{?}%\' '; Warning: mysqli_stmt::bind_param(): Number of variables doesn't match number of parameters in prepared statement in /home/rgero/public_html/php/searchadmins.php on…
Cârnăciov
  • 1,169
  • 1
  • 12
  • 24