我正在尝试用
MySQLi编写
PHP脚本来查询数据库.
如果可以针对数据库检查用户输入,并且如果表’normal_verbs’的列’root’中的字符串在输入中,则返回来自列’conjugation’的结果,我希望它.
因此,如果用户输入类似于“foobar”,并且根列具有“foo”,我希望在’foobar’中看到’foo’并在该行中返回’conjugation’的值.
我似乎无法让查询像我想要的那样工作.我在下面使用的那个基本上只是一个占位符.我不完全理解为什么它不起作用.
我正在尝试的是这样的:
function db_connect() {
static $connection;
if(!isset($connection)) {
$connection = mysqli_connect('localhost','user','password','Verb_Bank');
}
if($connection === false) {
return mysqli_connect_error();
}
return $connection;
}
function db_query($query) {
$connection = db_connect();
$result = mysqli_query($connection,$query);
return $result;
}
function db_quote($value) {
$connection = db_connect();
return "'" . mysqli_real_escape_string($connection,$value) . "'";
}
$m= db_query("SELECT `conjugation` from normal_verbs where `root` in (" . $y . ")");
if($m === false) {
// Handle failure - log the error, notify administrator, etc.
} else {
// Fetch all the rows in an array
$rows = array();
while ($row = mysqli_fetch_assoc($m)) {
$rows[] = $row;
}
}
print_r ($rows);
它没有给我任何错误,所以我认为它连接到数据库.
编辑2:我错了.由于误解了MySQLi,我错过了一些明显的东西,并相应地编辑了代码.所以上面的代码确实有效,因为它连接到数据库并返回结果,但我仍然难以接受一个可行的SQL语句来做我想做的事情.