PHP Velho Oeste 2024

mysqli::real_query

mysqli_real_query

(PHP 5, PHP 7, PHP 8)

mysqli::real_query -- mysqli_real_querySQL クエリを実行する

説明

オブジェクト指向型

public mysqli::real_query(string $query): bool

手続き型

mysqli_real_query(mysqli $mysql, string $query): bool

データベースに対して単一のクエリを実行します。 その結果を取得したり保存したりするには、関数 mysqli_store_result() あるいは mysqli_use_result() を使用します。

警告

セキュリティ上の注意: SQLインジェクション

クエリに入力値を含める場合は、プリペアドステートメント を使うべきです。使わない場合、データを適切にフォーマットし、全ての文字列は mysqli_real_escape_string() を使ってエスケープしなければいけません。

指定したクエリが結果を返すかどうかを調べるには、 mysqli_field_count() を参照ください。

パラメータ

link

手続き型のみ: mysqli_connect() あるいは mysqli_init() が返す mysqliオブジェクト。

query

クエリを表す文字列。

戻り値

成功した場合に true を、失敗した場合に false を返します。

エラー / 例外

mysqli のエラー報告 (MYSQLI_REPORT_ERROR) が有効になっており、かつ要求された操作が失敗した場合は、警告が発生します。さらに、エラー報告のモードが MYSQLI_REPORT_STRICT に設定されていた場合は、mysqli_sql_exception が代わりにスローされます。

参考

add a note add a note

User Contributed Notes 3 notes

up
1
TIL
5 years ago
Please note that this example is vulnerable to injection and uses plain-stored passwords...
up
0
jay at cloudulus dot media
5 years ago
Well, we don't know if $password is in plain text because it was never defined in the example.

New passwords should be stored using

<?php
$crypt_pass
= password_hash($password, PASSWORD_DEFAULT);
?>

Where PASSWORD_DEFAULT = BCRYPT algorithm

Then to compare a user entered password to a database stored password, we use:

<?php
$valid
= password_verify($password, $crypt_pass);
?>

Which returns true or false.

As for being vulnerable to injection, this is true. To avoid this, use sprintf() or vsprintf() to format the query and inject the values using mysqli::real_escape_string, like so:

<?php
$vals
= [$db->real_escape_string($username), $db->real_escape_string($password)];
$query = vsprintf("Select * From users Where username='%s' And password='%s'", $vals);
$result = $db->real_query($query);
?>
up
-58
Tinker
7 years ago
Straightforward function - simply connect to the database and execute a query, similar to this function bellow.

<?php

function check_password($username, $password) {
// Create connection
$db = new mysqli('localhost','database_user','database_pass','database_name');

// Check for errors
if($db->connect_errno){
echo
$db->connect_error;
}

// Execute query
$result = $db->real_query("Select * From users Where username='$username' And password='$password'");

// Always check for errors
if($db->connect_errno){
echo
$db->connect_error;
}

return
$result == true;
}
?>

Very easy.

Replace database_user, database_pass and database_name with the proper names, and the text inside real_query with your actual query.

Don't forget the quotes on either side (except for numbers, there you can omit them) otherwise it won't work. Hope that helps someone.
To Top