我似乎有這個代碼的問題,即時嘗試使用mysqli_connect()和mysqli_query()查詢數據庫,但代碼不返回一個mysqli對象和我不知道爲什麼。 我做了我自己的代碼來格式化查詢,我確定沒有失敗,因爲我每次檢查: (即時通訊使用XAMPP,使用本地主機進行測試),難道是因爲安裝的PHP已損壞? )mysqli_query()返回的東西不是一個對象
<?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,在C定的boolean:\ XAMPP \ htdocs中\ Guardian2 \ login.php中第19行
<?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'];
}
?>
'var_dump($ mysqli_object)'會告訴你它是什麼。這可能是一個布爾值FALSE來表示任何你餵食它的失敗。 –
$ mysqli_object包含bool(true)o.O –
但我不知道爲什麼?它不應該返回真正的SELECT查詢 –