我似乎对这段代码有疑问,我试图使用 mysqli_connect() 和 mysqli_query() 来查询数据库,但代码没有返回 mysqli 对象,我不知道为什么。我制作了自己的代码来格式化查询,并且我确定没有失败,因为我每次都在检查:(我正在使用 XAMPP,使用 localhost 进行测试),难道是安装的 php 已损坏?)
<?php
//error_reporting(~E_ALL);
require 'errorReporting.php';
$mysqli_host = 'localhost';
$mysqli_user = 'root';
$mysqli_pass = NULL;
$mysqli_name = 'gsistel';
$mysqli_link = mysqli_connect($mysqli_host, $mysqli_user, $mysqli_pass, $mysqli_name) or die('SQL ERROR COULD NOT CONNECT TO DB');
mysqli_select_db($mysqli_link, $mysqli_name) or die('SQL ERROR COULD NOT SELECT DB');
function fQuery($table, $operation, $selectionColumns, $iouValues, $whereColumns, $whereValues, $orderBy, $orderClause) {
$operation = strtoupper($operation);
switch($operation) {
case 'SELECT':
global $mysqli_link;
$mysqli_query = 'SELECT ';
$selection = '';
for($i = 0; $i < count($selectionColumns); $i++) {
if(count($selectionColumns) == 1) {
$selection = $selectionColumns[0];
}else if($i == count($selectionColumns)-1) {
$selection = $selection . $selectionColumns[$i];
}else {
$selection = $selection . $selectionColumns[$i] . ', ';
}
}
$mysqli_query = $mysqli_query . $selection . ' FROM ' . $table . ' ';
$whereSelection = '';
if($whereColumns != null && (count($whereColumns == count($whereValues)))) {
$mysqli_query = $mysqli_query . ' WHERE ';
for($i = 0; $i < count($whereColumns); $i++) {
if(count($whereColumns) == 1) {
$whereSelection = $whereColumns[0] . '=\'' . $whereValues[0] . '\'';
}else if($i == 0){
$whereSelection = $whereSelection . $whereColumns[$i] . '=\'' . $whereValues[$i] . '\'';
}else {
$whereSelection = $whereSelection . ' AND ' . $whereColumns[$i] . '=\'' . $whereValues[$i] . '\'';
}
}
$mysqli_query = $mysqli_query . $whereSelection . ' ';
}
if($orderBy != null) {
$mysqli_query = $mysqli_query . ' ORDER BY ' . $orderClause;
}
return $mysqli_result = mysqli_query($mysqli_link, $mysqli_query) or die(reportFriendlyError('SQL SELECT', 'ERROR: SQL QUERY FAILED<br>' . $mysqli_query));
break;
case 'INSERT':
$mysqli_query = 'INSERT INTO ' . $table . ' VALUES (';
for($i = 0; $i < count($iouValues); $i++) {
if($i == count($iouValues)-1) {
if(strtoupper($iouValues[$i]) == 'NULL') {
$mysqli_query = $mysqli_query . $iouValues[$i] . ')';
}else {
$mysqli_query = $mysqli_query . '\'' . $iouValues[$i] . '\'' . ')';
}
}else {
if(strtoupper($iouValues[$i]) == 'NULL') {
$mysqli_query = $mysqli_query . $iouValues[$i] . ', ';
}else {
$mysqli_query = $mysqli_query . '\'' . $iouValues[$i] . '\'' . ', ';
}
}
}
return $mysqli_query;
//TODO: Executa query
break;
case 'UPDATE':
$mysqli_query = 'UPDATE ' . $table . ' SET ';
for($i = 0; $i < count($selectionColumns); $i++) {
if($i == count($selectionColumns)-1) {
$mysqli_query = $mysqli_query . $selectionColumns[$i] . '=\'' . $iouValues[$i] . '\'';
}else {
$mysqli_query = $mysqli_query . $selectionColumns[$i] . '=\'' . $iouValues[$i] . '\', ';
}
}
if($whereColumns != null && (count($whereColumns) == count($whereValues))) {
$mysqli_query = $mysqli_query . ' WHERE ';
for($i = 0; $i < count($whereColumns); $i++) {
if($i == count($whereColumns)-1) {
$mysqli_query = $mysqli_query . $whereColumns[$i] . '=\'' . $whereValues[$i] . '\'';
}else {
$mysqli_query = $mysqli_query . $whereColumns[$i] . '=\'' . $whereValues[$i] . '\', ';
}
}
}
return $mysqli_query;
//TODO: Execute query
break;
case 'DELETE':
$mysqli_query = 'DELETE FROM ' . $table . ' WHERE ';
for($i = 0; $i < count($whereColumns); $i++) {
if($i == count($whereColumns)-1) {
$mysqli_query = $mysqli_query . $whereColumns[$i] . '=\'' . $whereValues[$i] . '\'';
}else {
$mysqli_query = $mysqli_query . $whereColumns[$i] . '=\'' . $whereValues[$i] . '\', ';
}
}
return $mysqli_query;
//TODO: execute query
break;
default:
return false;
break;
}
}
?>
我使用此处的代码进行测试: mysqli_query() 返回的不是 false,但它不是 mysqli 对象。我也收到警告:警告:mysqli_fetch_array() 期望参数 1 为 mysqli_result,布尔值在第 19 行的 C:\xampp\htdocs\Guardian2\login.php 中给出
<?php
require 'connect.php';
$mysqli_object = fQuery('users', 'SELECT', array('username'), null, array('username') , array('admin'), null, null);
if($mysqli_object === false) {
echo 'FAIL';
}
if(is_object($mysqli_object)) {
echo 'IS OBJECT';
}else {
echo 'IS NOT OBJECT';
}
$user = '';
while($row = mysqli_fetch_array($mysqli_object)) {
echo $user = $row['username'];
}
?>