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
10
votes
1 answer

Docker-compose mysql environment difference between PASSWORD and ROOT_PASSWORD

This is from my docker compose: db: image: mysql ports: - "3306:3306" environment: MYSQL_DATABASE: myDb MYSQL_USER: user MYSQL_PASSWORD: test MYSQL_ROOT_PASSWORD:…
helloworld123
  • 155
  • 2
  • 2
  • 8
10
votes
6 answers

Cannot figure out how to run a mysqli_multi_query and use the results from the last query

I've never used mysqli_multi_query before and it's boggling my brain, any examples I find on the net aren't helping me to figure out exactly what it is I want to do. Here is my code:
Harry Weinert
  • 141
  • 1
  • 1
  • 6
10
votes
2 answers

$mysqli->prepare returns false, but $mysqli->error is an empty string

In my code, I execute several valid SQL statements. But one of statement was invalid ($mysqli->prepare returns false), but no error code was returned ($mysqli->error returns false). Here an example of the code: $mysqli = new mysqli('host', 'user',…
David Gausmann
  • 1,570
  • 16
  • 20
10
votes
3 answers

Testing php / mysqli connection

I am looking for a way to test just the connection portion of a php / mysqli connection. I am migrating from a LAMP server build on Vista to the same on Ubuntu and am having fits getting mysqli to work. I know that all of the proper modules are…
Blind Fish
  • 998
  • 4
  • 14
  • 25
10
votes
7 answers

Warning: mysqli_real_connect(): (HY000/2002): No such file or directory in /private/tmp/wordpress/wp-includes/wp-db.php on line 1452

I'm trying to run PHPUnit to unittest a WordPress plugin, but the error in the title keeps showing up. I used WP-CLI to setup the unittests, but also WP-CLI throws a similar error when I try to run it. I use MAMP to run the database. I have setup…
SomeNorwegianGuy
  • 1,494
  • 4
  • 17
  • 43
10
votes
2 answers

MySQLi PHP: Check if SQL INSERT query was fully successful using MySQLi

I have this big function that gets a lot of different data and insert it into multiple tables.. Not all the data is always available so not all the SQL INSERT queries are successful. I need to check which SQL INSERT query was fully successful and…
Jonathan
  • 8,676
  • 20
  • 71
  • 101
10
votes
2 answers

Connection pooling vs persist connection mysqli

I've read that mysqli introduced connection pooling that was not available in mysql. http://php.net/manual/en/mysqli.quickstart.connections.php I've read this question that explains the difference between connection pooling and persist…
Dhruv Kapatel
  • 873
  • 3
  • 14
  • 27
10
votes
3 answers

MySQL falsely allowing duplicate entries when one of the fields involved is NULL

Using InnoDB/MySQLi, I have a simple table: mytable. The table has four fields: id (primary, auto_inc), field1, field2, field3. All of them are BIGINT and, except for id, can be NULL. I have added a unique constraint like so: ALTER TABLE mytable ADD…
a.real.human.being
  • 878
  • 2
  • 6
  • 17
10
votes
1 answer

CodeIgniter switching driver from mysql --> mysqli

I was reading this question: The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead And it got me thinking that I should make the change from mysql to mysqli. It is a one character change in CodeIgniter so it…
Chris Muench
  • 17,444
  • 70
  • 209
  • 362
10
votes
1 answer

Why does escaping SQL values require an open connection?

Just for clarity, can anyone explain why mysqli_real_escape_string has to read: $query = mysqli_real_escape_string($conn,"SELECT * FROM tbl"); And not just: $query = mysqli_real_escape_string("SELECT * FROM tbl"); Thanks for any help!
Richard Tinkler
  • 1,635
  • 3
  • 21
  • 41
10
votes
2 answers

Mysqli fetch_assoc() loop vs. Mysqli fetch_all() database load?

Strictly from MySQL's point of view (database performance, not PHP performance) what's the difference between a Mysqli fetch_assoc() loop vs. Mysqli fetch_all() when retrieving query results? Let's say for $result = $qdb->query("SELECT name, id FROM…
PJ Brunet
  • 3,615
  • 40
  • 37
10
votes
2 answers

How to fix Error while reading greeting packet?

I am trying to connect to server in NetBeans. I write the code as below: "; $conn=mysqli_connect("mydomain.com:2082", "phpmyadmin_user", "password","database_name"); echo $conn; ?> Running of…
BinaryMan
  • 269
  • 2
  • 6
  • 18
10
votes
2 answers

PHP PDO: Unable to connect, Invalid catalog name

I am trying to set up a new site on my hosting (Host route if it matters) but i keep getting this error when i try using PDO (first PDO site im trying): Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[3D000]: Invalid catalog…
Tristan Cunningham
  • 909
  • 2
  • 10
  • 24
10
votes
1 answer

Bind multiple parameters into mysqli query

Right now I need to use the following structure to cope with binding multiple parameters into a mysqli query: if ($words_total == 1) { $statement -> bind_param("s", $words[0]); } else if ($words_total == 2) { $statement -> bind_param("ss",…
Amy Neville
  • 10,067
  • 13
  • 58
  • 94
10
votes
3 answers

Is it possible to use store_result() and bind_result() with PHP PDO?

My question is fairly straightforward: Is it possible to use store_result() and bind_result() with PHP PDO? Here is some example code I came across: $stmt = $mysqli->prepare("SELECT id, username, password, salt FROM members WHERE email = ? LIMIT…
Mlagma
  • 1,240
  • 2
  • 21
  • 49