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

How can I view the contents of a prepared statement?

I'm working on learning to use prepared statements with mysqli in PHP and usually, if I'm having a problem with a query I just echo it to the screen to see what it looks like as a first step. How can I do that with a prepared statement? I'd like to…
Stomped
  • 2,010
  • 6
  • 21
  • 25
27
votes
2 answers

how to bind multiple parameters to MySQLi query

I have a mysql query, but I can't bind param for it SELECT users.email,users.handle,userprofile.mobile FROM users,userprofile WHERE users.email =? OR users.handle =? OR userprofile.mobile=? I've tried below line $query = "SELECT…
AF.P
  • 403
  • 1
  • 5
  • 8
27
votes
3 answers

mysqli bind_param() expected to be a reference, value given

Can't figure out, whats causing error Parameter 3 to mysqli_stmt::bind_param() expected to be a reference, value given in... PDO $query = "INSERT INTO test (id,row1,row2,row3) VALUES (?,?,?,?)"; $params = array(1,"2","3","4"); $param_type =…
woopata
  • 875
  • 5
  • 17
  • 29
27
votes
6 answers

utf 8 - PHP and MySQLi UTF8

my table char set is utf8 and it's collation is utf8.now i have this code: $mysqli = new mysqli("localhost", "root", "", "Amoozeshgah"); if (mysqli_connect_errno()) { printf("Connect failed: %s\n",…
Mahdi_Nine
  • 14,205
  • 26
  • 82
  • 117
26
votes
2 answers

mysqli last insert id

I would like to associate the image with firstname, lastname...how can I retrieve the last rowand use it to insert to the other table? I tried $image = $mysqli->insert_id; then binding but it doesn't work. Can someone help me out? $image =…
user2926655
  • 309
  • 2
  • 4
  • 7
26
votes
2 answers

$stmt->close() vs $stmt->free_result()

I am trying to clarify the difference between $stmt->close() and $stmt->free_result() when finalizing a prepared mysqli statement. So far I use: $mysqli = new mysqli(host,user,password,database); $stmt = $mysqli->prepare(sql…
Nicholaos Renessis
  • 432
  • 1
  • 4
  • 8
26
votes
4 answers

Why are PHP's mysql_ functions deprecated?

Playing Devil's Advocate a little here as I stopped using these functions a while ago, but the question is genuine and probably matters to a lot of SO users. We all know that using mysql_ functions in the wrong way can be very dangerous, it can…
Glitch Desire
  • 14,632
  • 7
  • 43
  • 55
25
votes
1 answer

How to use MySQLi inside a namespace

MySQLi works fine inside a class with no namespace and outside a class. I recently started using namespace and now I have stumbled on a code much like the following: namespace Project; class ProjectClass{ public static function…
Asko Nõmm
  • 265
  • 1
  • 3
  • 4
25
votes
1 answer

Should we ever check for mysqli_connect() errors manually?

The PHP manual for mysqli_connect() suggests checking for the return value and displaying the error messages on the screen. $link = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db"); if (!$link) { echo "Error: Unable to connect to…
Dharman
  • 30,962
  • 25
  • 85
  • 135
25
votes
8 answers

mysqli_connect(): (HY000/2002): No connection could be made because the target machine actively refused it

I know there are many questions like this, but i didn't find any solution in it. Things i tried:- checked firewall restarted my PC and Apache server restarted MYSQL checked my code Tried everything i know and found on internet here's my…
Sami
  • 466
  • 1
  • 5
  • 12
25
votes
5 answers

mysql_field_name to the new mysqli

I have a way to get the name of the columns of a table. It works fine but now I want to update to the new mysqli ? (I tried the mysqli_fetch_field but I don't know how to apply to this case and I am not sure if it is the wright option) How to do the…
Nrc
  • 9,577
  • 17
  • 67
  • 114
25
votes
2 answers

Mysqli Prepare Statement - Returning False, but Why?

I have a function that generates a prepared INSERT statement based on an associative array of column names and values to be inserted into that column and a table name (a simple string): function insert ($param, $table) { $sqlString = "INSERT…
Andrew E.
25
votes
4 answers

How to fetch all in assoc array from a prepared statement?

I'm trying this code: if ($result = $this->mysqli->prepare("SELECT * FROM `mytable` WHERE `rows1`=?")) { $result->bind_param("i",$id); $result->execute(); while ($data = $result->fetch_assoc()) { …
AquaVita
  • 293
  • 4
  • 6
  • 10
24
votes
4 answers

Warning: mysqli_error() expects exactly 1 parameter, 0 given error

I get the following error Warning: mysqli_error() expects exactly 1 parameter, 0 given The problem is with this line of the code: $query = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error()); The whole code…
Aasim Azam
  • 263
  • 1
  • 4
  • 9
24
votes
1 answer

Is mysql_real_escape_string() necessary when using prepared statements?

For this query, is necessary to use mysql_real_escape_string? Any improvement or the query is fine ? $consulta = $_REQUEST["term"]."%"; ($sql = $db->prepare('select location from location_job where location like ?')); $sql->bind_param('s',…
anvd
  • 3,997
  • 19
  • 65
  • 126