mysqli_query()返回的不是对象 [英] mysqli_query() returns something that is NOT an Object

查看:53
本文介绍了mysqli_query()返回的不是对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎对此代码有问题,我试图使用mysqli_connect()和mysqli_query()查询数据库,但代码未返回mysqli对象,我也不知道为什么. 我制作了自己的代码来格式化查询,并且确定不会失败,因为我会每次检查: (即时通讯使用XAMPP,使用localhost进行测试),可能是安装的php损坏了吗? )

i seem to have a problem with this code, im trying to use mysqli_connect() and mysqli_query() to query the Database, but the code is NOT returning a mysqli Object and i dont know why. I Made my own code to format the Queries and im sure is not failing because im checking everytime: ( IM USING XAMPP, testing using localhost ), Could it be that the php installed is corrupt?? )

<?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;        
    }
}

?>

im使用此处的代码进行测试: mysqli_query()返回的不是false,但不是mysqli对象. 我也得到警告: 警告:mysqli_fetch_array()期望参数1为mysqli_result,在第19行的C:\ xampp \ htdocs \ Guardian2 \ login.php中给出的布尔值

im using the code here to test: The mysqli_query() returns something other than false, but its not a mysqli Object. I also get the warning: Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\Guardian2\login.php on line 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'];    
}

?>

推荐答案

替换:

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'分支中.

这篇关于mysqli_query()返回的不是对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆