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
18
votes
5 answers

list all tables in a database with MySQLi

I have looked around and still can't find how to list all my tables in a database. is it possible with MySQLi? Thanks.
Run
  • 54,938
  • 169
  • 450
  • 748
18
votes
2 answers

What is the difference between get_result() and store_result() in php?

This is my code. I am checking if a user exists or not in a login/registration system: public function userExist($email){ $stmt = $this->conn->prepare("select email from users where email= ?"); $stmt->bind_param("s", $email); …
Surya Sekhar Mondal
  • 179
  • 1
  • 1
  • 13
18
votes
5 answers

CREATE TABLE IF NOT EXISTS fails with table already exists

I have the following code: $db_host = 'localhost'; $db_port = '3306'; $db_username = 'root'; $db_password = 'root'; $db_primaryDatabase = 'dsl_ams'; // Connect to the database, using the predefined database variables in…
Samuel Stiles
  • 2,118
  • 5
  • 22
  • 27
18
votes
4 answers

Best way to INSERT many values in mysqli?

I'm looking for a SQL-injection-secure technique to insert a lot of rows (ca. 2000) at once with PHP and MySQLi. I have an array with all the values that have to be include. Currently I'm doing that:
Roman Holzner
  • 5,738
  • 2
  • 21
  • 32
18
votes
3 answers

PHP and MySQLi - Cannot pass parameter 2 by reference in

I am trying to make a function which will check update and insert some data but I am having an issue in the first step where the $stmt->bind_param is saying that is not passing parameters by reference or something like that. I have attached below…
Sadi Qevani
  • 189
  • 1
  • 1
  • 3
18
votes
2 answers

Why use mysqli_close()?

Is there any reason why I should close the connection after a query or at the end of the script? What are the benefits of doing/no doing do?
bear
  • 11,364
  • 26
  • 77
  • 129
18
votes
4 answers

mysqli query results to show all rows

I have the following code: include $_SERVER['DOCUMENT_ROOT'].'/include/conn.php'; $query = "SELECT title FROM news_event"; $result = $mysqli->query($query); $row = $result->fetch_array(MYSQLI_BOTH); $row_cnt =…
Tom
  • 12,776
  • 48
  • 145
  • 240
18
votes
5 answers

How can I query using a foreign key in MySQL?

Right now I have a small database with two tables that look something like this: users table ==================== id name status_id 1 Bobby 3 2 James 2 and statuses table ============= id value 1 …
Josh Mountain
  • 1,888
  • 10
  • 34
  • 51
18
votes
3 answers

PHP: mysql v mysqli v pdo

I've been reading some questions regarding using mysqli versus pdo to use mysql in php. I've seen questions such as mysqli or PDO - what are the pros and cons? or Moving from mysql to mysqli or pdo?, which both deal with mysqli v pdo exclusively.…
Vlad
  • 211
  • 1
  • 2
  • 9
17
votes
5 answers

mysqli_query() expects at least 2 parameters, 1 given

This mysqli_query command results in the error below mysqli_query("INSERT INTO `counter`.`hits` (`page_hits`) VALUES (1)"); Warning: mysqli_query() expects at least 2 parameters, 1 given in What does this error message mean, and how can it be…
Joe Winfield
  • 809
  • 4
  • 11
  • 18
17
votes
2 answers

Why doesn't MySQLi library natively support named parameters?

Proper MySQLi parameterized query syntax from http://php.net/manual/en/mysqli.quickstart.prepared-statements.php: $stmt = $mysqli->prepare("INSERT INTO test(id) VALUES (?)"); $stmt->bind_param("i", $id); But never something like: $stmt =…
Dennis
  • 7,907
  • 11
  • 65
  • 115
17
votes
3 answers

Is mysqli_real_escape_string safe?

I´m new in PHP and I´ve realised that my database connection, using a php form (with user and pass text inputs) was totally unsafe: This was working, but was unsafe:
Rosamunda
  • 14,620
  • 10
  • 40
  • 70
17
votes
1 answer

PHP prepared statements and transactions in a loop

The classic transactions in a loop code: $mysqli->query("START TRANSACTION"); foreach ($pdata as $key => $value) { $sql = "INSERT INTO temp (`fund_id`) VALUES (" . $value . ")"; $result =…
user2723490
  • 2,010
  • 4
  • 27
  • 37
17
votes
2 answers

Fatal error: Call to undefined function mysqli_result()

Can someone please tell me why this doesnt work, when I tried to switch my old sql to sqli: $query = "SELECT * FROM `product_category`"; $result = mysql_query($query, $connect) or die("could not perform query: " . mysql_error()); $num_rows =…
Cesarg2199
  • 569
  • 1
  • 6
  • 18
17
votes
3 answers

mysqli_stmt::bind_param(): Number of elements in type definition string doesn't match number of bind variables

So I have this massive headache inducing query that I need to perform involving 65 form inputs needing to be injected into a database using mysqli prepared statements. The issue I'm running into is that it says the # of variables I am attempting to…
Samuel Stiles
  • 2,118
  • 5
  • 22
  • 27