Funkce PHP mysqli query().
Příklad - Objektově orientovaný styl
Proveďte dotaz na databázi:
<?php
$mysqli = new mysqli("localhost","my_user","my_password","my_db");
// Check connection
if ($mysqli -> connect_errno) {
echo "Failed to connect to MySQL: " . $mysqli -> connect_error;
exit();
}
// Perform query
if ($result = $mysqli -> query("SELECT * FROM Persons"))
{
echo "Returned rows are: " . $result -> num_rows;
// Free result set
$result -> free_result();
}
$mysqli -> close();
?>
Podívejte se na příklad procedurálního stylu dole.
Definice a použití
Funkce query() / mysqli_query() provádí dotaz na databázi.
Syntax
Objektově orientovaný styl:
$mysqli -> query(query,
resultmode)
Styl postupu:
mysqli_query(connection, query, resultmode)
Hodnoty parametrů
Parameter | Description |
---|---|
connection | Required. Specifies the MySQL connection to use |
query | Required. Specifies the SQL query string |
resultmode |
Optional. A constant. Can be one of the following:
|
Technické údaje
Návratová hodnota: | Pro úspěšné dotazy SELECT, SHOW, DESCRIBE nebo EXPLAIN vrátí objekt mysqli_result. U ostatních úspěšných dotazů vrátí hodnotu TRUE. NEPRAVDA při selhání |
---|---|
Verze PHP: | 5+ |
PHP Changelog: | PHP 5.3.0 přidalo možnost asynchronních dotazů |
Příklad - Procedurální styl
Proveďte dotaz na databázi:
<?php
$con = mysqli_connect("localhost","my_user","my_password","my_db");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
exit();
}
// Perform query
if ($result = mysqli_query($con, "SELECT * FROM Persons"))
{
echo "Returned rows are: " . mysqli_num_rows($result);
// Free result set
mysqli_free_result($result);
}
mysqli_close($con);
?>
❮ Reference PHP MySQLi