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

Fatal error: Call to undefined function mysqli_connect()

For 2 days now I'm trying to solve this, but unfortunately no result. Let me tell you my story about the problem. I've bulid an application on a site, and the application deals with the reviews. But, I'm trying to put it on another site, and I…
Emi
  • 1,165
  • 4
  • 15
  • 26
80
votes
12 answers

How to change mysql to mysqli?

Based on this code below I use for regular mysql, how could I convert it to use mysqli? Is it as simple as changing mysql_query($sql); to mysqli_query($sql);?
JasonDavis
  • 48,204
  • 100
  • 318
  • 537
79
votes
7 answers

Warning: mysqli_connect(): (HY000/2002): No such file or directory

I'm trying to install vanilla forums on my Mac, and for this I just created a database and a user from the MySQL command line: mysql> CREATE DATABASE vanilla; Query OK, 1 row affected (0.00 sec) mysql> create user 'vanilla_user3'@'localhost'…
kramer65
  • 50,427
  • 120
  • 308
  • 488
74
votes
2 answers

PHP error: "Cannot pass parameter 2 by reference"

I just need help on this PHP error which I do not quite understand: Fatal error: Cannot pass parameter 2 by reference in /web/stud/openup/inactivatesession.php on line 13
user1723760
  • 1,157
  • 1
  • 9
  • 18
69
votes
19 answers

phpMyAdmin ERROR: mysqli_real_connect(): (HY000/1045): Access denied for user 'pma'@'localhost' (using password: NO)

I keep getting the following errors with mysql connection through XAMPP and I don't know what to do: That's the code in the config.inc.php
E. AMARAL
  • 949
  • 1
  • 10
  • 20
64
votes
5 answers

Using Mysqli bind_param with date and time columns?

How do you insert data into a MySQL date or time column using PHP mysqli and bind_param?
Keith Lyall
  • 1,165
  • 2
  • 9
  • 8
62
votes
3 answers

Using Prepared Statement, how I return the id of the inserted row?

I want retrieve the id of a inserted row in the database, but I don't know how to do this. I tried to return using the SQL clause RETURNING id, but not works. How I can return the id after the insertion of a row?
Renato Dinhani
  • 35,057
  • 55
  • 139
  • 199
59
votes
2 answers

How can I with mysqli make a query with LIKE and get all results?

This is my code but it dosn't work: $param = "%{$_POST['user']}%"; $stmt = $db->prepare("SELECT id,Username FROM users WHERE Username LIKE ?"); $stmt->bind_param("s",…
user2493164
  • 1,321
  • 3
  • 11
  • 15
58
votes
15 answers

CodeIgniter: Unable to connect to your database server using the provided settings Error Message

I have been using CI just fine using the MySQL driver. I want to use the MySQL driver instead, but as soon as I change it (just add the ‘i’ at the end of MySQL, and added the port number) I get the following error message A Database Error…
Onema
  • 7,331
  • 12
  • 66
  • 102
56
votes
6 answers

$stmt->execute() : How to know if db insert was successful?

With the following piece of code, how do i know that anything was inserted in to the db? if ($stmt = $connection->prepare("insert into table (blah) values (?)")) { $stmt->bind_param("s", $blah); $stmt->execute(); $stmt->close(); …
cosmicsafari
  • 3,949
  • 11
  • 37
  • 56
56
votes
10 answers

Which is fastest in PHP- MySQL or MySQLi?

I'd like to know if anyone has any first-hand experience with this dichotomy. A few blogs say the mysql extension is faster than mysqli. Is this true? And I'm only asking about speed. I know mysqli has features that are not present in the older…
David
  • 7,487
  • 6
  • 32
  • 25
55
votes
16 answers

How to install MySQLi on MacOS

I cannot find instructions about installing MySQLi on a Mac. Just to be clear, MySQL is up to date and I am running PHP 5. How do I install it? Where do I even get it from? Thanks for your help. I'll be giving an up vote and a check mark to whoever…
Jack Humphries
  • 13,056
  • 14
  • 84
  • 125
53
votes
6 answers

How to echo a MySQLi prepared statement?

I'm playing around with MySQLi at the moment, trying to figure out how it all works. In my current projects I always like to echo out a query string while coding, just to make sure that everything is correct, and to quickly debug my code. But... how…
Alec
  • 9,000
  • 9
  • 39
  • 43
52
votes
11 answers

mysqli fetch_all() not a valid function?

Thanks to the answers I have figured out that I am unable to use fetch_all() because i am using PHP 5.2.17 - fetch_assoc with while loop worked. The function I am using fetch_all is coming back with this error: Fatal error: Call to undefined…
Phil
  • 10,948
  • 17
  • 69
  • 101
51
votes
1 answer

PHP MySQLI Prevent SQL Injection

I've build a website that will be going live soon and just have a couple questions about preventing SQL injection, I understand how to use mysqli_real_escape_string but I'm just wondering if I have to use that on all variables that I'm getting for…
user2201765
  • 1,017
  • 6
  • 18
  • 21