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
2
votes
1 answer

Absurd MySQL behavior over same data set but different threads

I'm connecting to MySQL db from two different PHP instances. The first thread adds a new row, passes the 'id' of new row to the second thread via a queue. Sometimes the second thread is unable to find the new row data, even though theoretically it…
Neo
  • 13,179
  • 18
  • 55
  • 80
2
votes
3 answers

What are the memory implications of passing a MySQLi result to a function in PHP?

Dear gods of Stackoverflow Let's say I have a MySQL query that selects a large dataset: $query = "SELECT col_1, col_2, ..., col_99 FROM big_table"; And I get a MySQLi result like so: $result = $db->query($query); But then instead of dealing with…
Kosta Kontos
  • 4,152
  • 7
  • 25
  • 28
2
votes
2 answers

filter_input and mysqli_real_escape_string for integers

I'm developing a simple PHP database application for internal use but would like to code it to best practices. Some of my pages are receiving integer values from GET requests and I'm just wondering how much validation and sanitation is really…
Kevin Morse
  • 244
  • 2
  • 13
2
votes
1 answer

What does fetch in php do?

I don't understand the concept of the fetch function. I am doing a tutorial from 'PHP Solutions' book and i am using MySQL Improved to update something in the database. Here is the code: if (isset($_GET['article']) && !$_POST) { $sql =…
zeckdude
  • 15,877
  • 43
  • 139
  • 187
2
votes
1 answer

Is it worth using MYSQLI_CLIENT_COMPRESS when the db is on another machine?

I have two machines on amazon ec2, one with a drupal installation and another one with the mysql database. I wonder if it's worth to patch drupal to activate mysql client compression. Are there any caveats?
gpilotino
  • 13,055
  • 9
  • 48
  • 61
2
votes
0 answers

PHP + MySql + Stored procedure + cursor: don't got any row in result

I have problem with a stored procedure where i use a cursor, so i made a test code to spot the problem but its still a mystery for me, here is the test codes: Stored proc. body named 'Test1': SELECT 'ok1'; Stored proc. body named 'Test2': DECLARE…
abe
  • 33
  • 3
2
votes
2 answers

Require_once doesn't seem to affect all functions?

Everything was working fine in my little project, until I decided to clean up a little bit and moved database-related php-files to their own folder. Then things went strange. I am trying to use two functions here: function getEntries () { …
hannu40k
  • 512
  • 1
  • 8
  • 20
2
votes
2 answers

Storing IP Addresses for Users

I'd like to store all IP's used by a user, for every user. So, I could figure out where they are coming from, if they switch a lot. If all of a sudden they are using a new one, etc. I know to store it as a number in MySql so it is smaller data, but…
Alan1993
  • 21
  • 1
2
votes
1 answer

Can PHP mysql and mysqli module connections to the same database be used side-by-side safely?

Say one were looking at transitioning a codebase from using the basic PHP mysql_* functions to a mysqli implementation. Can connections to the same database be used side-by-side in both interfaces, or is there some way in which they can interfere…
chaos
  • 122,029
  • 33
  • 303
  • 309
2
votes
1 answer

mysqli returning bits and ints as string

I have a project made in flex, using PHP data services to access an SQL server database, and I need to convert it to MySQL, I have changed all my PHP services from sqlsrv to mysqli.Something like this: $this->connection =…
Joaolvcm
  • 1,983
  • 3
  • 20
  • 24
2
votes
1 answer

which one to use mysqli_fetch_assoc OR mysqli_fetch_array from performance point of view?

Our company is developing a big project in which all the things matter for us. So is there any benifit to use fetch_assoc() or is it just a shorcut for usability purposes?
Alireza
  • 6,497
  • 13
  • 59
  • 132
2
votes
2 answers

How to set PDO default host?

I'm trying to convert some PHP code from Mysqli to PDO, and I need to be able to set a default host for PDO MySQL connections (outside of my application code). With Mysqli, I could do this with the php.ini setting: mysqli.default_host =…
Andrew Ensley
  • 11,611
  • 16
  • 61
  • 73
2
votes
1 answer

How to check if correct values are being passed in mysqli

I'm trying to count the rows returned from a query in mysqli, I know I supplied the correct user id and password but it keeps on returning 0 for the number of rows returned. I even echoed out the user id and the hashed password that I'm binding to…
Wern Ancheta
  • 22,397
  • 38
  • 100
  • 139
2
votes
1 answer

PHP - copying classes by clone - is this bugged or im doing it wrong?

Please explain me why that code: class kategoria { public $IdKat; public $NazwaKat; public $OpisKat; } class dbClass { private $link; private $user = USER; private $pass =…
Kamil
  • 13,363
  • 24
  • 88
  • 183
2
votes
2 answers

php Return FALSE on singleton factory method if data base connection can't be created

I have a class extending mysqli. It follows a singleton pattern so I have a static method to retrieve the shared instance of the class. After calling self::$instance = new self I suppose self::$instance should be FALSE or NULL if __construct could…
David Casillas
  • 1,801
  • 1
  • 29
  • 57
1 2 3
99
100