mysqli::query

mysqli_query

(PHP 5, PHP 7, PHP 8)

mysqli::query -- mysqli_queryPerforms a query on the database

Beschreibung

Objektorientierter Stil

public mysqli::query(string $query, int $result_mode = MYSQLI_STORE_RESULT): mysqli_result|bool

Prozeduraler Stil

mysqli_query(mysqli $mysql, string $query, int $result_mode = MYSQLI_STORE_RESULT): mysqli_result|bool

Performs a query against the database.

For non-DML queries (not INSERT, UPDATE or DELETE), this function is similar to calling mysqli_real_query() followed by either mysqli_use_result() or mysqli_store_result().

Hinweis:

In the case where you pass a statement to mysqli_query() that is longer than max_allowed_packet of the server, the returned error codes are different depending on whether you are using MySQL Native Driver (mysqlnd) or MySQL Client Library (libmysqlclient). The behavior is as follows:

  • mysqlnd on Linux returns an error code of 1153. The error message means got a packet bigger than max_allowed_packet bytes.

  • mysqlnd on Windows returns an error code 2006. This error message means server has gone away.

  • libmysqlclient on all platforms returns an error code 2006. This error message means server has gone away.

Parameter-Liste

mysql

Nur bei prozeduralem Aufruf: Ein von mysqli_connect() oder mysqli_init() zurückgegebenes mysqli-Objekt.

query

The query string.

Warnung

Sicherheitswarnung: SQL-Injection

Wenn die Abfrage irgendwelche Eingabevariablen enthält, sollten stattdessen parametrisierte Prepared Statements verwendet werden. Alternativ dazu müssen die Daten korrekt formatiert sein und alle Strings müssen mit der Funktion mysqli_real_escape_string() maskiert werden.

result_mode

The result mode can be one of 3 constants indicating how the result will be returned from the MySQL server.

MYSQLI_STORE_RESULT (default) - returns a mysqli_result object with buffered result set.

MYSQLI_USE_RESULT - returns a mysqli_result object with unbuffered result set. As long as there are pending records waiting to be fetched, the connection line will be busy and all subsequent calls will return error Commands out of sync. To avoid the error all records must be fetched from the server or the result set must be discarded by calling mysqli_free_result().

MYSQLI_ASYNC (available with mysqlnd) - the query is performed asynchronously and no result set is immediately returned. mysqli_poll() is then used to get results from such queries. Used in combination with either MYSQLI_STORE_RESULT or MYSQLI_USE_RESULT constant.

Rückgabewerte

Returns false on failure. For successful queries which produce a result set, such as SELECT, SHOW, DESCRIBE or EXPLAIN, mysqli_query() will return a mysqli_result object. For other successful queries, mysqli_query() will return true.

Beispiele

Beispiel #1 mysqli::query() example

Objektorientierter Stil

<?php

mysqli_report
(MYSQLI_REPORT_ERROR MYSQLI_REPORT_STRICT);
$mysqli = new mysqli("localhost""my_user""my_password""world");

/* Create table doesn't return a resultset */
$mysqli->query("CREATE TEMPORARY TABLE myCity LIKE City");
printf("Table myCity successfully created.\n");

/* Select queries return a resultset */
$result $mysqli->query("SELECT Name FROM City LIMIT 10");
printf("Select returned %d rows.\n"$result->num_rows);

/* If we have to retrieve large amount of data we use MYSQLI_USE_RESULT */
$result $mysqli->query("SELECT * FROM City"MYSQLI_USE_RESULT);

/* Note, that we can't execute any functions which interact with the
    server until all records have been fully retrieved or the result
    set was closed. All calls will return an 'out of sync' error */
$mysqli->query("SET @a:='this will not work'");

Prozeduraler Stil

<?php

mysqli_report
(MYSQLI_REPORT_ERROR MYSQLI_REPORT_STRICT);
$link mysqli_connect("localhost""my_user""my_password""world");

/* Create table doesn't return a resultset */
mysqli_query($link"CREATE TEMPORARY TABLE myCity LIKE City");
printf("Table myCity successfully created.\n");

/* Select queries return a resultset */
$result mysqli_query($link"SELECT Name FROM City LIMIT 10");
printf("Select returned %d rows.\n"mysqli_num_rows($result));

/* If we have to retrieve large amount of data we use MYSQLI_USE_RESULT */
$result mysqli_query($link"SELECT * FROM City"MYSQLI_USE_RESULT);

/* Note, that we can't execute any functions which interact with the
    server until all records have been fully retrieved or the result
    set was closed. All calls will return an 'out of sync' error */
mysqli_query($link"SET @a:='this will not work'");

Die oben gezeigten Beispiele erzeugen eine ähnliche Ausgabe wie:

Table myCity successfully created.
Select returned 10 rows.

Fatal error: Uncaught mysqli_sql_exception: Commands out of sync; you can't run this command now in...

Siehe auch

Hier Kannst Du einen Kommentar verfassen


Bitte gib mindestens 10 Zeichen ein.
Wird geladen... Bitte warte.
* Pflichtangabe
Es sind noch keine Kommentare vorhanden.

Neuigkeiten für PHP-Entwickler: Laravel 11 Veröffentlichung

Am 12. März 2024 wurde die lang erwartete Version 11 des Laravel-Frameworks veröffentlicht, die eine Reihe von spannenden Neuerungen und Verbesserungen für die PHP-Entwicklungsgemeinschaft mit sich bringt. ...

Mike94

Autor : Mike94
Kategorie: PHP Magazin

Technisches SEO bleibt relevant

Technisches SEO – Was ist das überhaupt? Technisches SEO bezieht sich auf die Optimierung der technischen Aspekte deiner Webseite. Das Ziel ist klar! ...

admin

Autor : admin
Kategorie: SEO & Online-Marketing

Was ist neu in der PHP 8.2.10

PHP 8.2.10 ist eine der neuesten Versionen von PHP, die eine Reihe von Verbesserungen und neuen Funktionen mit sich bringt. In diesem Artikel werden wir einige der herausragenden Neuerungen und Verbesserungen dieser Version diskutieren. ...

admin

Autor : admin
Kategorie: Software-Updates

Tutorial veröffentlichen

Tutorial veröffentlichen

Teile Dein Wissen mit anderen Entwicklern weltweit

Du bist Profi in deinem Bereich und möchtest dein Wissen teilen, dann melde dich jetzt an und teile es mit unserer PHP-Community

mehr erfahren

Tutorial veröffentlichen

Seltsames Verhalten von execute() oder Fehler meinerseits

Hallo liebe Community, ich habe ein kleines Problem und vielleicht kann mir ja jemand helfen, würde ich mich sehr drüber freuen. Unten steht e ...

Geschrieben von garibaldiwz am 22.03.2024 13:03:12
Forum: SQL / Datenbanken
Google reCAPTCHA in Kontaktformular einbinden

Überprüfen Sie den E-Mail-Versand: Stellen Sie sicher, dass die E-Mail-Funktion mail() ordnungsgemäß funktioniert und dass keine Fehler beim V ...

Geschrieben von Gast am 18.03.2024 04:54:16
Forum: PHP Developer Forum
`count.php`

Hallo cober93327, und Danke fuer deine Antwort! :-) Naja, so einen "Besucherzähler" auf der Webseite anzuzeigen ist schon eher etwas, das man a ...

Geschrieben von kekse1 am 17.03.2024 15:56:38
Forum: Projekthilfe
`count.php`

Es gibt dazu natuerlich auch eine recht ausfuehrliche Dokumentation in meinem GitHub-Repository Es würde meiner Ansicht nach enorm helfen, wenn D ...

Geschrieben von cober93327 am 14.03.2024 15:49:28
Forum: Projekthilfe