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

Updating from MYSQL to MYSQLI

So as MYSQL is deprecated and eveyone keeps telling me to update, I thought it was about time I did. But as I'm not used to mysqli_*, it seems alien to me. And it's not a simple edit when I have a whole site coded in Mysql. So I'm wondering: How…
dave
  • 1,009
  • 5
  • 15
  • 26
12
votes
3 answers

Possible to use multiple/nested MySQLi statements?

Is it possible to have a MySQLi prepared statement within the fetch() call of a previous statement? If not, what's the best way around it? Example code: if($stmt = $link->prepare("SELECT item FROM data WHERE id = ?")) { $stmt->bind_param("i",…
Gilean
  • 14,708
  • 10
  • 45
  • 52
12
votes
2 answers

mysqli/mysql query inside function not working

I'm tryin to build some functions for a website of mine and some of them consist in fetching data from the mysql database. When I test the code outside of the function it seems to work properly. So here it is, The first…
inrob
  • 4,969
  • 11
  • 38
  • 51
12
votes
3 answers

Which one is faster and lighter - mysqli & PDO

I am using MySQL with PHP5. I got to know that ancient mysql_* functions are no longer maintained and community has begun the deprecation process. So I decided to move away from mysql_*. The question is where to? I am looking for lighter, simpler…
Alfred
  • 21,058
  • 61
  • 167
  • 249
12
votes
4 answers

mysqli bind_param giving error: (1210) Incorrect arguments to mysql_stmt_execute

I have this exact same code working great on another server: $mysqli_Cxn = new mysqli($SQL_HOST,$SQL_USER,$SQL_PASS,$SQL_DB); if($mysqli_Cxn->connect_errno){ echo 'Unable to connect!!'; exit(); } $userID=12345; $userFirstName =…
Charley P.
  • 184
  • 1
  • 13
11
votes
3 answers

My PHP array of references is "magically" becoming an array of values... why?

I'm creating a wrapper function around mysqli so that my application needn't be overly complicated with database-handling code. Part of that is a bit of code to parameterize the SQL calls using mysqli::bind_param(). bind_param(), as you may know,…
Rick Koshi
  • 945
  • 1
  • 11
  • 21
11
votes
3 answers

mysqli, OOP vs Procedural

I just saw this for the first time. I had no idea you can instantiate a mysqli class by doing something like new mysqli( $host, $username, $password, $db ); This is brand new to me...Every tutorial I have seen online or in books when connecting…
kdub
  • 749
  • 2
  • 8
  • 19
11
votes
1 answer

How do you connect to MySQL using PHP's mysqli when using sha256_password (access denied)

I'm using MySQL 5.7.24 and PHP 7.2.10. MySQL is configured with SSL. I have a user my_user with password abc123. I have tried configuring with both authentication plugins: ALTER USER 'my_user'@'192.168.192.150' IDENTIFIED WITH sha256_password BY…
Nick Williams
  • 2,864
  • 5
  • 29
  • 43
11
votes
3 answers

bind_param problem with binding boolean values

I have a problem binding booleans using mysqli_stmt::bind_param in PHP5. The SQL query is the following: insert into `nvp_notes` (subject,messageid,receivedate,read) values (?,?,?,?) Where 'read' is a tinyint, either 0 or 1, as I've had issues with…
ian
11
votes
1 answer

Create mysql trigger via PHP?

I'm executing the following in PHP5.3: $sql = " CREATE TRIGGER `_photo_fulltext_insert` AFTER INSERT ON `photo` FOR EACH ROW INSERT INTO `_photo_fulltext` SELECT * FROM `photo` WHERE `id`=NEW.`id`; CREATE TRIGGER `_photo_fulltext_delete` AFTER…
brianjcohen
  • 965
  • 2
  • 10
  • 14
11
votes
2 answers

Unable to install php-mysqli on PHP7

I have installed PHP 7, mysql5.7, Apache2.2, CentOS6. And I'm installing CodeIgniter3.0.6. When I use database connection, error occured and said A PHP Error was encountered Severity: Core Warning Message: PHP Startup: Unable to load dynamic…
ryochanuedasan
  • 528
  • 1
  • 6
  • 11
11
votes
1 answer

If I use a static method in PHP to create a connection to a database will I end up with one or many connections?

I am looking at creating an object that is called upon to pass data to the data store. My implementation uses MySQLi, but I want to allow other developers to use whatever data store they want. I was thinking that a static method might be the best…
11
votes
3 answers

PHP stmt prepare fails but there are no errors

I am trying to prepare a mysqli query, but it fails silently without giving any error. $db_hostname = "test.com"; $db_database = "dbname"; $db_username = "db_user"; $db_password = "password"; $db = new…
Typel
  • 1,109
  • 1
  • 11
  • 34
11
votes
5 answers

MySQLI Prepared Statement: num_rows & fetch_assoc

Below is some poorly written and heavily misunderstood PHP code with no error checking. To be honest, I'm struggling a little getting my head around the maze of PHP->MySQLi functions! Could someone please provide an example of how one would use…
Arbiter
  • 486
  • 1
  • 8
  • 21
11
votes
3 answers

Can't pass mysqli connection in session in php

I'm trying to pass a session with mysqli connection for multiple queries on the site, but when I try to do a query it outputs a warning "Couldn't fetch mysqli" $_SESSION['db']=new mysqli($host,$username,$password,$db); Is it impossible to pass a…
Gal
  • 23,122
  • 32
  • 97
  • 118