我似乎有这个代码的问题,我试图使用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'];
}
?>
答案 0 :(得分:2)
替换:
return $mysqli_result = mysqli_query($mysqli_link, $mysqli_query) or die(reportFriendlyError('SQL SELECT', 'ERROR: SQL QUERY FAILED<br>' . $mysqli_query));
使用:
$mysqli_result = mysqli_query($mysqli_link, $mysqli_query) or die(reportFriendlyError('SQL SELECT', 'ERROR: SQL QUERY FAILED<br>' . $mysqli_query));
return $mysqli_result;
在case 'SELECT'
分支。