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
49
votes
4 answers

How to check if a row exists in MySQL? (i.e. check if username or email exists in MySQL)

I need help checking if a row exists in the database. In my case, that row contains an email address. I am getting the result: email no longer exists publisher@example.com This is the code I'm currently using: if (count($_POST)) { $email =…
user2882684
  • 539
  • 1
  • 5
  • 10
49
votes
12 answers

MySQLi equivalent of mysql_result()?

I'm porting some old PHP code from mysql to MySQLi, and I've ran into a minor snag. Is there no equivalent to the old mysql_result() function? I know mysql_result() is slower than the other functions when you're working with more than 1 row, but a…
DOOManiac
  • 6,066
  • 8
  • 44
  • 67
49
votes
1 answer

mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in

I'm have some trouble checking if a Facebook User_id already exists in my database (if it doesn't it should then accept the user as a new one and else just load the canvas application). I ran it on my hosting server and there was no problem, but on…
Mats de Swart
  • 538
  • 1
  • 4
  • 10
48
votes
4 answers

mysqli::query(): Couldn't fetch mysqli

Warning: mysqli::query(): Couldn't fetch mysqli in C:\Program Files (x86)\EasyPHP-DevServer-13.1VC9\data\localweb\my portable files\class_EventCalendar.php on line 43 The following is my connection file:
codingManiac
  • 1,654
  • 4
  • 29
  • 42
46
votes
4 answers

MySQLi count(*) always returns 1

I'm trying to count the number of rows in a table and thought that this was the correct way to do that: $result = $db->query("SELECT COUNT(*) FROM `table`;"); $count = $result->num_rows; But counts always returns (int)1. If I use the same query in…
Lode
  • 2,160
  • 1
  • 20
  • 25
45
votes
6 answers

Single result from database using mysqli

I am trying to use mySQLi for the first time. I have done it in the case of loop. Loop results are showing but I am stuck when I try to show a single record. Here is loop code that is working.
Hiroshi Rana
  • 978
  • 2
  • 9
  • 18
45
votes
6 answers

How to start and end transaction in mysqli?

As far as I understood transaction starts once we call $mysqli->autocommit(FALSE); statement and ends after calling $mysqli->commit(); command like in the example below. autocommit(FALSE); $mysqli->query('UPDATE…
Bakhtiyor
  • 7,198
  • 15
  • 55
  • 77
42
votes
1 answer

How to insert into MySQL using mysqli

I am just learning about databases and I want to be able to store user inputs. What would be a basic example on how to get form data and save it to a database using PHP? Also making the form secure from SQL attacks.
bammab
  • 2,543
  • 7
  • 25
  • 28
42
votes
4 answers

MySQLi query results: When do I close, free, or both?

I have some questions about using MySQLi queries, and related memory management. Suppose I have something like this: $db = new mysqli($dbhost, $un, $ps, $dbname); $query = "SELECT field1, field2 FROM table1 "; $results = $db->query($query); while…
Carvell Fenton
  • 2,341
  • 6
  • 23
  • 30
42
votes
3 answers

new mysqli vs mysqli_connect

What is difference between the new mysqli and mysqli_connect? I know that executing a query is different; for example: mysqli->query() and mysqli_query() Why are there two different types, what is the need for the difference?
FosAvance
  • 2,363
  • 9
  • 36
  • 52
40
votes
3 answers

mysqli_store_result() vs. mysqli_use_result()

The question What is the difference between mysqli::store_result() and mysqli::use_result()? The story Vague documentation The documentation on PHP.net seems very vague about the difference between the two. The mysqli::use_result()-page does not…
Avaq
  • 3,009
  • 1
  • 21
  • 25
40
votes
4 answers

mysqli or die, does it have to die?

If I use a bit of code like this: $update_result = mysqli_query( $link , $sql_update_login ) or die ('Unable to execute query. '. mysqli_error($link)); Does it have to die or can you put a different query afterwards? Like a predetermined function…
Maelish
  • 1,600
  • 4
  • 18
  • 25
39
votes
4 answers

How to call a MySQL stored procedure from within PHP code?

I have stored procedure that I created in MySQL and want PHP to call that stored procedure. What is the best way to do this? -MySQL client version: 4.1.11 -MySQL Server version: 5.0.45 Here is my stored procedure: DELIMITER $$ DROP FUNCTION IF…
Pheap
  • 2,349
  • 4
  • 18
  • 13
39
votes
6 answers

PHP PDO and MySQLi

I just finished an introduction course in PHP, and throughout the stackoverflow forum people have recommended that I switch to PDO, prepared statements or MYSQLi, I briefly checked the manual but most of it went over my head. I've been using…
grasshopper
  • 1,381
  • 4
  • 19
  • 36
38
votes
6 answers

How to convert mysqli result to JSON?

I have a mysqli query which I need to format as JSON for a mobile application. I have managed to produce an XML document for the query results, however I am looking for something more lightweight. (See below for my current XML code) $mysql = new…
ab24
  • 389
  • 1
  • 3
  • 10