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
13
votes
6 answers

Get number of rows matched by UPDATE query with PHP mysqli

The older mysql extension has the CLIENT_FOUND_ROWS connection flag, but I couldn't find any equivalent for mysqli. I have an update query and want to know how many rows its where clause matched, not how many were actually changed (as indicated by…
Core Xii
  • 6,270
  • 4
  • 31
  • 42
13
votes
1 answer

Why does mysqli num_rows always return 0?

I've been having trouble getting the number of rows to return using mysqli. I just get 0 back every time even though there are definitely some results. if($stmt = $mysqli->prepare("SELECT id, title, visible, parent_id FROM content WHERE parent_id =…
ollie
  • 799
  • 2
  • 10
  • 24
13
votes
5 answers

sql UPDATES updates every column in the row that comes after the change column

I'm using an UPDATE query to make changes to my database. The updates I make to a particular cell in a row get inserted into the database. What's happening though is any blank date spaces that come after that cell get entered as 0000-00-00. For…
Tony
  • 298
  • 3
  • 17
13
votes
3 answers

mysqli_connect vs mysqli_real_connect

I read the doumentation But I don't understand the difference exactly. What difference does the connection object make in this? I didn't find any posts. I misunderstood it. Flags are not present in both. Why didn't they add flags as part of…
Gibbs
  • 21,904
  • 13
  • 74
  • 138
13
votes
1 answer

SSL operation failed with code 1: dh key too small

I am connecting to my database Google Cloud SQL via SSL. I use codeigniter 3.0 to do so, although the mysqli driver is a bit modified to allow this functionality. It's been working well for months. However it just started to return this…
markbratanov
  • 878
  • 3
  • 17
  • 39
13
votes
11 answers

Mysql No connection could be made because the target machine actively refused it

I know there are many people already asked this , but this people mostly forgot password of blocked by firewall which I have none of this situations . I am developing with php , and I need to connect to remote database to let all my team work on it…
BishoyM
  • 561
  • 1
  • 7
  • 16
13
votes
2 answers

What the different between MySQL Native Driver and MySQL Client Library

I want to know the different between MySQL Native Driver and MySQL Client Library and when to use both of them
Robert
  • 2,342
  • 2
  • 24
  • 41
13
votes
3 answers

"Property access is not allowed yet" warning when using prepared statement

I'm trying to make a log in system by using AES_ENCRYPT() to encode my password. But I have some warning from xdebug when trying to implement these codes: ... $key = 'd0gis=SUPER-cute'; $sql = "SELECT * FROM `users2` WHERE username = ? AND pwd =…
weeix
  • 1,359
  • 1
  • 14
  • 17
13
votes
3 answers

Why I am getting an error when using w3school tutorial?

I've been looking at this code for a while now and I can't see where the problem is. I have been reading the whole of StackOverflow and still can't see where my error is.…
Blue
  • 261
  • 1
  • 3
  • 14
13
votes
2 answers

Send html form data to sql database via php (using mysqli)

I want to send the data inputted into an html form to my sql database, i.e., create a new row attributing certain values to certain columns. I know there are similar questions, I read the answers but nothing seems to…
Alex
  • 1,416
  • 4
  • 16
  • 42
13
votes
2 answers

mysqli_real_escape_string, should I use it?

I want to eliminate sql injection, should I use mysqli_real_escape_string() or is it clear in mysqli? For example $nick = mysqli_real_escape_string($_POST['nick'])
dontHaveName
  • 1,899
  • 5
  • 30
  • 54
13
votes
1 answer

How do I put booleans into a mysql database with prepared statements?

According to the PHP manual, the four variable types for mysqli->bind_param are integer, double, string and blob. What is the best way to insert a boolean?
fdsa
  • 1,379
  • 1
  • 12
  • 27
13
votes
3 answers

Mysqli get_result alternative

I've just changed all my sql queries to prepared statements using mysqli. To speed this process up I created a function (called performQuery) which replaces mysql_query. It takes the query, the bindings (like "sdss") and the variables to pass in,…
Timm
  • 12,553
  • 4
  • 31
  • 43
12
votes
5 answers

Moving from mysql to mysqli or pdo?

Duplicate: mysqli or PDO - what are the pros and cons? I'm looking to move a website from mysql to either mysqli or pdo as primarily a learning tool, but also for performance increases if possible. I have read through…
Rob
  • 2,332
  • 7
  • 31
  • 35
12
votes
3 answers

warning:mysql_fetch_array() expects parameter 1 to be resource, object given

I am getting the above warning when I try to run this code: $mysqli=new mysqli("localhost", "***", "***","***") or die(mysql_error()); function checklogin($username, $password){ global $mysqli; …
Sam Gabriel
  • 327
  • 1
  • 6
  • 15