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

Is it possible to combine mysqli prepared statement with multiple inserts?

I am well-versed in the old php mysql extension. I am working on my first script that uses the mysqli extension. I am going to be inserting a large number of rows into a table that are being generated dynamically. Is it possible to use a prepared…
Patrick
  • 3,142
  • 4
  • 31
  • 46
3
votes
1 answer

Is possible to enable MySQL Native Driver (mysqlnd)?

Possible Duplicate: Call to undefined method mysqli_stmt::get_result My web host has PHP 5.3.10 and I'm using mysqli, but some functions like get_result() doesn't work because it needs mysqlnd. I made some tests and appears that mysqlnd isn't…
Renato Dinhani
  • 35,057
  • 55
  • 139
  • 199
3
votes
1 answer

mysqli DELETE ahref not functioning

I have a Delete link listed beside all $rows, when I mouse over them they reflect the correct id for deletion, however, when I click DELETE I get redirected to phpfile.php?id=4, for example, and nothing is deleted, no errors are posted. while ($row…
V1GG3N
  • 309
  • 1
  • 6
  • 22
3
votes
0 answers

libmysql (c-api) slow compared to php-mysqli

I'm investigating why my C-test-program(libmysql) is almost twice as slow the same php-test(cli) program: I'm creating a prepared-statement and inserting 10 records. The php-cli-version(mysqlnd) is almost twice as fast as the C version using…
user914584
  • 571
  • 8
  • 15
3
votes
1 answer

Combine SELECT and UPDATE to avoid duplicate selects

i want to combine a SELECT and UPDATE query, to avoid duplicat select of rows. Here is my example code: private function getNewRCode() { $getrcodesql = "SELECT * FROM `{$this->mysqlprefix}codes` WHERE `used` = 0 LIMIT 1;"; $getrcodequery =…
Frederick Behrends
  • 3,075
  • 23
  • 47
3
votes
4 answers

Trying to clone an uncloneable object of class mysqli_result

the following error is thrown: Fatal error: Trying to clone an uncloneable object of class mysqli_result when I'm trying to use clone directly for a mysql query result: $result = mysqli_query($con, $query); $resultsClone = clone $result; is…
3emad
  • 238
  • 3
  • 10
3
votes
2 answers

Prepared Statement not returning anything

I know this particular query works, as I tested it with unprepared, procedural methods. Here it is: $name = 'introduction'; $mysqli = new mysqli('localhost', 'user', 'pass', 'db') or die('There was a problem connecting to the database.'); $stmt =…
Steve K
  • 387
  • 1
  • 8
  • 22
3
votes
4 answers

mysqli_query() always return true

This is my form:
This is my php…
Jayanga Kaushalya
  • 2,674
  • 5
  • 38
  • 58
3
votes
1 answer

Mysqli DELETE QUERY not working in PHP Script

Im using the following code to remove an entry from a table what i want to do is to check if any value was deleted from the table.If one value is deleted,the script should print success else false.This is what i have achieved till now.Please…
techno
  • 6,100
  • 16
  • 86
  • 192
3
votes
1 answer

Stored Procedure causing problems with mysqli when used with fetch_all

I have broken down this issue to it's essence but am still having problems. When I try and use fetch_all to get the results of a stored procedure, I get results returned to array as expected, but subsequent mysqli calls throw "command out of sync"…
christian
  • 2,279
  • 4
  • 31
  • 42
3
votes
4 answers

Cannot connect to mysql 5.0.8 using mysql_connect with password

I can connect to my mysql db using php without a password fine. However, when I add a password in phpmyadmin I can no longer connect. I consistently get the error of: "Could not connect: Access denied for user 'admin'@'localhost' (using password:…
user1330217
  • 243
  • 2
  • 4
  • 14
2
votes
1 answer

mysqli: anywhere to find errnos

I am working on trying to automatically recover from some mysql errors and am wondering if the error codes I get from $mysqli->errno are defined anywhere. I found a list at…
jpevarnek
  • 381
  • 1
  • 8
2
votes
2 answers

What MYSQL commands by definition do *not* return a result?

I'm writing a class in PHP for handling MySQLI queries, and when the query manages not to return a result set (or object) I want to issue a helpful error message. But, of course, not all queries are designed to return a result set (e.g. ALTER), so…
2
votes
2 answers

Function array not pulling back all rows from database

I currently have a table called category in my database category catId categoy News HTML PHP CSS Trying to pull back off rows from the database only brings back the 1 result using the following code private function navigation(){ $url…
2
votes
3 answers

Roleback CREATE TABLE using MySql Transactions

I'm writing a upgrader for a mysql database using PHP. The behavior of the upgrader should be as follows. If all the queries executed successfully the changes should be committed. If a sinngle query get faild eveything should be roled back to…
Thilanka
  • 1,733
  • 5
  • 21
  • 36
1 2 3
99
100