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
14
votes
3 answers

Try / catch in mysqli

I'm following an OOP mysqli course. When connecting to the database, they use the following script: $db = new mysqli("host", "user", "password", "database"); if ($db->connect_error){ $error = $db->connect_error; echo("Not connected: " .…
cesarcarlos
  • 1,271
  • 1
  • 13
  • 33
14
votes
2 answers

What is the type of NULL in mysqli bind_param?

I am trying to bind params to a INSERT INTO MySQLi prepared statement if that variable exists, otherwise insert null.Then i know type variable i corresponding variable has type integer d corresponding variable has type double s …
JackSun
  • 1,418
  • 3
  • 15
  • 19
14
votes
3 answers

unbuffered query with MySQLi?

Are MySQLi queries unbuffered? If not, is there a way to do an unbuffered query, as with the non-MySQLi mysql_unbuffered_query()?
ceejayoz
  • 176,543
  • 40
  • 303
  • 368
14
votes
2 answers

Warning: mysqli_query() expects at least 2 parameters, 1 given. What?

I made a PHP page that is supposed to select two names from a database and displays them. It just says: Warning: mysqli_query() expects at least 2 parameters, 1 given in /home/tdoylex1/public_html/dorkhub/index.php on line 4 Warning: mysqli_query()…
user2840637
  • 213
  • 1
  • 2
  • 7
14
votes
2 answers

What does bind_param accomplish?

I'm learning about avoiding SQL injections and I'm a bit confused. When using bind_param, I don't understand the purpose. On the manual page, I found this example: $stmt = mysqli_prepare($link, "INSERT INTO CountryLanguage VALUES (?, ?, ?,…
EveyPortman
  • 394
  • 2
  • 4
  • 17
14
votes
2 answers

What's the difference between the mysqli functions bind_result, store_result and fetch?

I'm running into problems knowing when and what to call after mysqli_stmt_execute How do you know when to call mysqli_stmt_bind_result mysqli_stmt_store_result mysqli_stmt_fetch
Celeritas
  • 14,489
  • 36
  • 113
  • 194
14
votes
1 answer

Handle decimal numbers in mysqli

I have to put the price of some items inside a mysql table. When creating the table I'm using DECIMAL(10,2) as I don't need more than 2 digits after the comma (for example: 123,45 would be accepted as an input but 123,456 would be rounded to 123,45…
Saturnix
  • 10,130
  • 17
  • 64
  • 120
14
votes
1 answer

Fatal error: Cannot use object of type mysqli_result

I'm about to open my website when I noticed that one of my mods gives me this error: Fatal error: Cannot use object of type mysqli_result as array in /var/www/vbsubscribetouser.php on line 303 I've went to line 303 and this is what I…
14
votes
2 answers

Should mysqli update queries return a result?

I'm updating my PHP code from mysql to mysqli but I can't seem to find the answer to this question: Do mysqli update queries return a result? With mysql, I could do $result = mysql_query("UPDATE `data` SET `field1` = 1 WHERE `key` = '$mykey'); and…
Swiftheart
  • 443
  • 1
  • 5
  • 17
14
votes
6 answers

PHP mysqli reconnect problem

I am having trouble using the mysqli class in PHP and I haven't been able to find the answer anywhere. In my script a class creates a mysqli connection that it uses throughout it's functions. Afterward, this script forks. The connection is used by…
Evan Carothers
  • 143
  • 1
  • 1
  • 5
14
votes
1 answer

Looping through results in mysqli

I am new to mysqli and having a problem looping through results with mysqli. Unfortunately, I am only getting a single result. When I put the query into phpMyAdmin, it comes up with three results. I believe the relevant code is here and that I am…
radleybobins
  • 863
  • 5
  • 10
  • 23
14
votes
3 answers

PHP to EasyPHP MySQL server 1 second connection delay

I'm asking this here because I think it applies to more than just EasyPHP itself. I currently use EasyPHP as my WAMP development server so I can create web applications. The problem is, when I get the mysqli object to connect to the database it…
Sam
  • 1,564
  • 4
  • 23
  • 37
13
votes
5 answers

mysqli_real_escape_string() expects exactly 2 parameters, 1 given

if (phpversion() >= '4.3.0'){ $string = mysqli_real_escape_string($string); }else{ $string = mysqli_escape_string($string); } All the documentation for mysqli_real_escape_string seems to indicate this is a valid bit of code, but I don't…
user880789
  • 171
  • 1
  • 1
  • 9
13
votes
3 answers

How do you use IN clauses with mysqli prepared statements

I’m moving some old code over to the new msqli interface using prepared statements, I’m having trouble with SQL statements containing the IN clause. I would just normally do this: $ids = '123,535,345,567,878' $sql = "SELECT * FROM table WHERE id IN…
user93836
13
votes
5 answers

Fatal error: Uncaught exception 'mysqli_sql_exception' with message 'No index used in query/prepared statement'

When I run the following code, I get the error saying Fatal error: Uncaught exception 'mysqli_sql_exception' with message 'No index used in query/prepared statement' $mysql = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME) or die('There…
Hrishikesh Choudhari
  • 11,617
  • 18
  • 61
  • 74