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

MySQLi not found dockerized php

I'm trying to dockerize my website. I've got Nginx and PHP up and running and it's working find except I can't connect to a db. When the page is loaded I get the error: Fatal error: Uncaught Error: Class 'MySQLi' not found in /private/conn.php:8…
Tom
  • 9,725
  • 3
  • 31
  • 48
37
votes
4 answers

mysqli - Do I really need to do $result->close(); & $mysqli->close();?

Just started using mysqli. If I'm working with small data sets on small websites (traffic-wise), do I really need to use these all the time? $result->close(); $mysqli->close(); Also, for someone doing custom PHP and MySQL work without a framework,…
James
  • 379
  • 1
  • 4
  • 4
37
votes
1 answer

bind_param Number of variables doesn't match number of parameters in prepared statement

Here is a snippet from my code: $stmt = $mysqli->prepare("SELECT DISTINCT model FROM vehicle_types WHERE year = ? AND make = '?' ORDER by model"); $stmt->bind_param('is', $year, $make); $stmt->execute(); When I echo out the values for $year…
TMorgan
  • 655
  • 1
  • 7
  • 13
36
votes
5 answers

mysqli::mysqli(): (HY000/2002): Can't connect to local MySQL server through socket 'MySQL' (2)

I get this error when I try to connect to the mysql database using php mysqli class. Using following code: $db = new MySQLi("localhost","kamil","*****"); password is * for security. I have created user kamil with all privileges on external ip…
Dharman
  • 30,962
  • 25
  • 85
  • 135
35
votes
2 answers

PHP and MySQLi close()

I am new to MySQL and PHP and am attempting to make my own CMS to help make managing my websites easier. Can someone explain mysqli's close() function? Is it necessary? What exactly does it do? I heard that after PHP runs its script that it closes…
Theopile
  • 868
  • 3
  • 14
  • 30
35
votes
3 answers

Can I blindly replace all mysql_ functions with mysqli_?

I have used mysql_query() throughout my project; but I've just learned that mysql_ was deprecated as of PHP 5.5, has been removed in PHP 7. So, I would like to know if I can replace all mysql_ functions with mysqli_ in my project blindly? For…
Midnight Engineer
  • 473
  • 1
  • 5
  • 9
35
votes
8 answers

Single Value Mysqli

I am trying to write a function that will check for a single value in the db using mysqli without having to place it in an array. What else can I do besides what I am already doing here? function getval($query){ $mysqli = new mysqli(); …
Refiking
  • 641
  • 3
  • 10
  • 18
34
votes
8 answers

Use an array in a mysqli prepared statement: `WHERE .. IN(..)` query

Imagine we have a query: SELECT * FROM somewhere WHERE `id` IN(1,5,18,25) ORDER BY `name`; and an array of IDs to fetch: $ids = array(1,5,18,25) With prepared statements it's adviced to prepare one statement and call it multiple times: $stmt =…
kolypto
  • 31,774
  • 17
  • 105
  • 99
34
votes
7 answers

How can I bind an array of strings with a mysqli prepared statement?

I need to bind an array of values to WHERE IN(?) clause. How can I do that? This works: $mysqli = new mysqli("localhost", "root", "root", "db"); if(!$mysqli || $mysqli->connect_errno) { return; } $query_str = "SELECT name FROM table WHERE city…
Mark
  • 2,666
  • 3
  • 25
  • 29
34
votes
4 answers

difference between mysqli_query and mysqli_real_query

What is the difference between mysqli::query and mysqli::real_query? OR What is the difference between mysqli_query and mysqli_real_query?
itsazzad
  • 6,868
  • 7
  • 69
  • 89
33
votes
1 answer

How to prepare statement for update query?

I have a mysqli query with the following code: $db_usag->query("UPDATE Applicant SET phone_number ='$phone_number', street_name='$street_name', city='$city', county='$county', zip_code='$zip_code', day_date='$day_date', month_date='$month_date', …
Michael
  • 6,377
  • 14
  • 59
  • 91
33
votes
3 answers

How do I migrate my site from mysql to mysqli?

Is there anything I have to do on the database end or is it just the php that changes? Can I just substitute mysqli functions for mysql functions? Anything else I should know?
dac
  • 811
  • 3
  • 12
  • 24
33
votes
5 answers

Object of class mysqli_result could not be converted to string

I am getting the error: Object of class mysqli_result could not be converted to string This is my code: $result = mysqli_query($con, "SELECT classtype FROM learn_users WHERE username='abcde'"); echo "my result My…
Chinmay Chandak
  • 455
  • 1
  • 8
  • 14
33
votes
8 answers

How to Check Whether mysqli connection is open before closing it

I am going to use mysqli_close($connection) to close the $connection. But Before Closing I need to ensure that the concerned connection is open. I tried if($connection) { mysqli_close($connection); } But it is not working. Any Solution?
Munib
  • 3,533
  • 9
  • 29
  • 37
32
votes
4 answers

Fatal error: Call to a member function fetch_assoc() on a non-object

I'm trying to execute a few queries to get a page of information about some images. I've written a function function get_recent_highs($view_deleted_images=false) { $lower = $this->database->conn->real_escape_string($this->page_size *…
nickles
  • 639
  • 1
  • 8
  • 13