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

When and why to use mysqli_fetch_row, mysqli_fetch_object, mysqli_fetch_assoc, mysqli_fetch_array

I understand in some way the differences between mysqli_fetch_row, mysqli_fetch_object, mysqli_fetch_assoc and mysqli_fetch_array. My question is if they are so similar (if they are really almost the same as many topics say) which should we use? Why…
Liam James
  • 414
  • 1
  • 6
  • 15
11
votes
3 answers

Mysqli multiple row insert, simple multi insert query

How do I insert this query with mysqli?... INSERT INTO table (field1, field2, field3) VALUES ('value', 'value', 'value'), ('value', 'value', 'value'), ('value', 'value', 'value'); Normally in mysql this query is straight forward and will insert 3…
user1675155
  • 111
  • 1
  • 1
  • 4
11
votes
1 answer

Differences in mysqli_fetch functions

This post gives four ways of retrieving the result of a MySQL query: mysqli_fetch_array — Fetch a result row as an associative, a numeric array, or both $row = mysqli_fetch_array($result); echo $row[0]; // or echo…
1''
  • 26,823
  • 32
  • 143
  • 200
11
votes
3 answers

How to run the bind_param() statement in PHP?

I'm trying to make the following code work but I can't reach the execute() line. $mysqli = $this->ConnectLowPrivileges(); echo 'Connected
'; $stmt = $mysqli->prepare("SELECT `name`, `lastname` FROM `tblStudents` WHERE `idStudent`=?"); echo…
Lucio
  • 4,753
  • 3
  • 48
  • 77
11
votes
3 answers

Strict Standards: mysqli_next_result() error with mysqli_multi_query

I have tried using multi_query but I keep getting a strict Standards message popping up. $querystring = "INSERT INTO responses VALUES('1', '2', '3', '4'); INSERT INTO responses VALUES('1', '2', '3', '4')"; if (mysqli_multi_query($db,…
Phil
  • 309
  • 1
  • 3
  • 9
11
votes
1 answer

PHP MySQLi Asynchronous Queries with

I am trying to use asynchronous queries via PHP MySQLi. The following code has been simplified, the original is code is too verbose to list here because of class dependencies and all that. Also please assume the reference to the connection…
ObiHill
  • 11,448
  • 20
  • 86
  • 135
11
votes
3 answers

Having trouble executing a SELECT query in a prepared statement

Ive followed a bunch of different examples regarding using a SELECT in a prepared statement, but nothing is returned. EDIT I have changed my code a bit to look like this: $date1 = 2012-01-01; $date2 = 2012-01-31; $sql_con = new mysqli('db',…
Glenncito
  • 902
  • 1
  • 10
  • 23
10
votes
7 answers

How can I put the results of a MySQLi prepared statement into an associative array?

I have a sql query and a mysqli prepared statement: $sql = 'SELECT photographers.photographer_id, photographers.photographer_name FROM photographers'; $stmt = $conn->stmt_init(); if ($stmt->prepare($sql)) { …
zeckdude
  • 15,877
  • 43
  • 139
  • 187
10
votes
2 answers

How to insert the record with MySQL NOW() function in MySQLi with bind_param?

I need to execute this mysql query in MySQLi PDO with bind parametr in PHP: mysql_query("INSERT INTO `posts` (post_name,publish_date) VALUES ($post_name,NOW()) ") I use the script like this, but it doesn't insert publish_date correctly.…
JohnUS
  • 1,001
  • 4
  • 11
  • 13
10
votes
1 answer

Why affected_rows always returns -1?

I seem to have problem getting affected_rows when I INSERT and SELECT, it just returns -1 for some reason? I'm using a database class which I use all the time for my projects which uses MYSQLI prepare statements to avoid SQL injections. Does anyone…
John
  • 387
  • 2
  • 5
  • 17
10
votes
5 answers

Use one bind_param() with variable number of input vars

I try to use variable binding like this: $stmt = $mysqli->prepare("UPDATE mytable SET myvar1=?, myvar2=... WHERE id = ?")) { $stmt->bind_param("ss...", $_POST['myvar1'], $_POST['myvar2']...); but some of the $_POST['...'] might be empty so I don't…
Elena
10
votes
5 answers

Trying to get property of non-object MySQLi result

Got a bit of PHP code I'm struggling with - had a search around Google etc. and tried everything mentioned, but for some reason I'm having trouble solving it. The problem is: I have some code that is querying a database for the presence of a…
Rick Hanshaw
  • 141
  • 1
  • 1
  • 4
10
votes
1 answer

parameters in MySQLi

I'm using PHP with MySQLi, and I'm in a situation where I have queries like SELECT $fields FROM $table WHERE $this=$that AND $this2=$that2 So far I've written some code that splices up an array that I give it, for example: $search =…
dcgross
10
votes
1 answer

MySQLi Prepared Statements and Transactions

Is there a way to do transactions with prepared statements? I mean can I use the following example with $mysqli->autocommit(FALSE); and $mysqli->commit( ); and $mysqli->rollback( ); //Preparing the statment $insert_stmt=$mysqli->prepare("INSERT…
Yerel
  • 103
  • 1
  • 5
10
votes
2 answers

MySQLi: query VS prepare

There is something I don't quite understand it at all which is prepare and query in mysqli. This one is using mysqli::query to process the query and it has been known of lacking security: public function fetch_assoc($query) { $result =…
Run
  • 54,938
  • 169
  • 450
  • 748