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
12
votes
8 answers

Call to a member function prepare() on a non-object PHP Help

I am trying to write a PHP function. It is very simple. It is just a prepared statement that queries the database, but I can not get this to work. I keep recieving the error Call to a member function prepare() on a non-object. here is the code: $DBH…
mcbeav
  • 11,893
  • 19
  • 54
  • 84
12
votes
3 answers

mysqli_multi_query doesn't work reliable with mysql conditional comment queries

I am having an issue with mysql conditional comment queries where errors are reported with no syntax error. It works in the case that at least one of the queries actually executes with a conditional. I am using php 5.6.24 and mysql…
Chris Muench
  • 17,444
  • 70
  • 209
  • 362
12
votes
4 answers

Call to undefined function mysql_query() with Login

When i execute my PHP code below i get a Fatal error and i'm not sure how to resolve it. Thank you for your help The Error PHP Fatal error: Uncaught Error: Call to undefined function mysql_query() in /Applications/MAMP/htdocs/lprapp/config.php:23 …
user6771006
12
votes
1 answer

Cannot Find mysqli Class in PHP7 Installation on Windows

I downloaded and installed php7 from: http://windows.php.net/qa/#php-7.0-ts-VC14-x64 The php codes are running fine. I am not able to connect database using 'mysqli' Class. It says Class 'mysqli' not found I've uncommented…
gskhanal
  • 591
  • 1
  • 3
  • 12
12
votes
2 answers

Sphinx How can i keep connection active even if no activity for longer time?

I was doing bulk inserts in the RealTime Index using PHP and by Disabling AUTOCOMIT , e.g. // sphinx connection $sphinxql = mysqli_connect($sphinxql_host.':'.$sphinxql_port,'',''); //do some other time consuming work //sphinx start…
user1642018
12
votes
2 answers

mysqli_multi_query - Commands out of sync; you can't run this command now

I have several query strings which I want to execute at once using "mysqli_multi_query". This works. When I insert a query again to check each item in joined tables using "mysqli_query" it doesn't return any result nor any error from PHP. When I run…
Edison
  • 343
  • 2
  • 11
12
votes
3 answers

what is $stmt in PHP

What exactly is $stmt and what is it's purpose? what does it stand for.. I'm following a tutorial that is using prepared statements and looked up stmt in the manual: http://php.net/manual/en/class.mysqli-stmt.php and see that it is a class that…
Zach Smith
  • 8,458
  • 13
  • 59
  • 133
12
votes
4 answers

Is there any way to print the actual query that mysqli->execute() makes?

I have a complex query that gets executed like this: if ($stmt = $dbi->prepare($pt_query)) { $stmt->bind_param('ssssssssi', $snome,$scognome,$ssocieta,$svia,$slocalita,$sprovincia,$scap,$stelefono,$sfax,$uid); $stmt->execute(); …
0plus1
  • 4,475
  • 12
  • 47
  • 89
12
votes
4 answers

Inserting a row to a table with auto_increment column

I'm working on a table that has 4 columns and the first one is an auto incrementing integer called id. If I'm going to insert into this table using mysqli prepared statements I keep having trouble inserting a query that works. Using phpMyAdmin It…
fdfdsfsdfsdfds
  • 121
  • 1
  • 1
  • 3
12
votes
1 answer

PHP singleton database connection pattern

I've been working on a small project using PHP and MySQL. I've read a lot around about best practices on managing a connection and so on. I've also implemented (following some posts found around) a singleton class to manage MySQL…
LucasM
  • 421
  • 2
  • 7
  • 23
12
votes
2 answers

When should I close a database connection in PHP?

I'm nothing like a php developer but I do have to use it and I'm not really aware on how PHP handle memory allocation during session. I'm working on an application that ask for an HTTP authentication, once you're logged in you can manipulate data…
Kiwy
  • 340
  • 2
  • 10
  • 43
12
votes
3 answers

Creating a globally accessible MySQLi object

I have multiple classes that use static methods. These functions connect to the database using $mysqli = new mysqli(DB_SERVER, DB_USER, DB_PASS, DB_NAME); where the constants DB_SERVER, DB_USER, DB_PASS, DB_NAME are database variables defined in a…
Blacksmith
  • 712
  • 8
  • 21
12
votes
4 answers

Warning: mysqli_connect(): Unknown MySQL server host

I have some trouble connecting to a mysql server. I can connect using the linux terminal, so I know that my host, port, user, and password works. I cannot, however, connect using PHP. PHP Version: 5.2.17 Server version: 5.5.27-log MySQL Community…
DoubleTrouble
  • 902
  • 2
  • 7
  • 19
12
votes
1 answer

PHP mysqli - return an associative array from a prepared statement

I'm trying to use mysqli to prepare a statement in order to safely pass in variable values to the query. All of that is working for me, but the problem I'm running into is getting the result in an associative array. Here's my structure so…
Chris Schmitz
  • 20,160
  • 30
  • 81
  • 137
12
votes
6 answers

Should I use mysqli_real_escape string() or mysql_real_escape_string() for form data?

Possible Duplicate: mysql_escape_string VS mysql_real_escape_string I need to get company_name (given by user through a form) entered into my mysql database. When I use $company = mysqli_real_escape_string($_POST['company_name']) I get an…
user1629766
  • 157
  • 1
  • 1
  • 6