-1

每次我用我的班级为 select * from table where blank=blank 调用查询时,它总是在 var_dump 上出现“NULL”,以获得班级结束时的结果。我仍然坚持这一点,不知道它为什么这样做,但它肯定没有发送任何响应,因为我什么也没得到。

mysqli.class.php

<?php
class DATABASE
{
//set up variables only for this class
private $db_host;
private $db_user;
private $db_pass;
private $db_name;
private $connection;
private $paramaters = array();
private $results = array();
private $numrows;

//call connection on call of class
public function __construct($db_host, $db_user, $db_pass, $db_name)
{
    $this->host = $db_host;
    $this->user = $db_user;
    $this->pass = $db_pass;
    $this->name = $db_name;
    $this->mysqli = new mysqli($this->host, $this->user, $this->pass, $this->name) or die('There was a problem connecting to the database! Error #: '. $this->mysqli->connect_errno);
}
//close mysqli connection on class close
public function __destruct()
{
    $this->mysqli->close();
}
//query
public function select($fields, $table, $where, $whereVal, $type, $orderByVal, $ASDESC, $limitVal, $sets, $setVal)
{
    switch($type)
    {
        case "regular":
            if ($where == null)
            {
                $queryPre = "SELECT " . $fields . " FROM " . $table;
                $querySuff = "";
            } else {
                $queryPre = "SELECT " . $fields . " FROM " . $table;
                $querySuff = " WHERE " . $where . " = ?";
            }
            break;
        case "orderByLimit":
            $queryPre = "SELECT " . $fields . " FROM " . $table;
            $querySuff = " ORDER BY " . $orderByVal . " " . $ASDESC . " LIMIT " . $limitVal;
            break;
        case "update":
            if ($where == null)
            {
                $queryPre = "UPDATE " . $table;
                //need for loop for multiple sets, check for is_array and do multiple if so.
                $querySuff = " SET " . $sets . " = " . $setVal;
            } else {
                $queryPre = "UPDATE " . $table;
                //need for loop for multiple sets, check for is_array and do multiple if so.
                $querySuff = " SET " . $sets . " = " . $setVal . " WHERE " . $where . " = ?";
            }
            break;
        case "insert":
            if ($sets == null)
            {
                $queryPre = "INSERT INTO " . $table;
                $querySuff = " VALUES(" . setVal . ")";
            } else {
                $queryPre = "INSERT INTO " . $table . " (" . $sets . ")";
                $querySuff = " VALUES(" . setVal . ")";
            }
        case "delete":
            if ($where == null)
            {
                $queryPre = "DELETE FROM " . $table;
                $querySuff = "";
            } else {
                $queryPre = "DELETE FROM " . $table;
                $querySuff = " WHERE " . $where . " = ?";
            }
    }
    //$sql = $queryPre . "" . $querySuff; 
    //var_dump($sql);
    //exit;
    $stmt = $this->mysqli->prepare($queryPre . "" . $querySuff) or die('There was a problem preparing the Query! Error#: '. $this->mysqli->errno); 
    if ($whereVal == null)
    {
        $stmt = $this->bindVars($stmt,$setVal);
    } else {
        $stmt = $this->bindVars($stmt,$whereVal);
    }
    $stmt->execute();
    $meta = $stmt->result_metadata();

    while ($field = $meta->fetch_field())
    {
        $parameters[] = &$row[$field->name];
    }
    call_user_func_array(array($stmt, 'bind_result'), $parameters);

    while ($stmt->fetch()) 
    {
        $x = array();
        foreach($row as $key => $val)
        {
            $x[$key] = $val;
        }
        $results[] = $x;
    }
    $stmt->close();
    //var_dump($results);
    if ($results == "" || $results == NULL)
    {
        return null;
    } else {
        return $results;
    }
}
private function bindVars($stmt,$params) 
{
    if ($params != null) 
    {
        $types = '';
        //initial sting with types
        if (is_array($params))
        {
            foreach($params as $param)
            {
                //for each element, determine type and add
                if(is_int($param))
                {
                    $types .= 'i'; //integer
                } elseif (is_float($param)) 
                {
                    $types .= 'd'; //double
                } elseif (is_string($param))
                {
                    $types .= 's'; //string
                } else {
                    $types .= 'b'; //blob and unknown
                }
            }
        } else {
            if (is_int($params))
            {
                $types = 'i';
            } elseif (is_float($params))
            {
                $types = 'd';
            } elseif (is_string($params))
            {
                $types = 's';
            } else {
                $types = 'b';
            }               
        }
        $bind_names[] = $types;
        if (is_array($params))
        {
            //go through incoming params and added em to array
            for ($i=0; $i<count($params);$i++)
            {
                //give them an arbitrary name
                $bind_name = 'bind' . $i;
                //add the parameter to the variable variable
                $$bind_name = $params[$i];
                //now associate the variable as an element in an array
                $bind_names[] = &$$bind_name;               
            }
        } else {
            $int0 = 0;
            $bind_name = 'bind' . $int0;
            $$bind_name = $params;
            $bind_names[] = &$$bind_name;
        }
        call_user_func_array(array($stmt,'bind_param'),$bind_names);
    }
    return $stmt; //return the bound statement
}
}
?>

调用和检查字段的示例 - process_availability.php:

<?php
//require necessary files
require('../config/dbconfig.php');
include('../classes/mysqli.class.php');
//initiate connection
$mysqli = new DATABASE($db_host,$db_user,$db_pass,$db_name);
//take type of check
$checktype = $_POST['type'];

//check the user name
if ($checktype == "username") {
//change post to variable
$username = $_POST['username'];
//check if user name is empty
if ($username == "") {
    $validuser = array("empty", "false");
    echo implode(',', $validuser);
    exit;
}
//if user name is more characters than 30
if (strlen($username) > 30) {
    $validuser = array("max", "false");
    echo implode(',', $validuser);
    exit;
}
//search for same user name in database
$resultsU = $mysqli->select('*','users','username',$username,'regular',null,null,null,null,null);
//var_dump($resultsU);
if (is_array($resultsU))
{
var_dump($resultsU);
    foreach($resultsU as $rowU)
    {
        //return results
        if($rowU['username'] == "" || $rowU['username'] == NULL)
        {
            //user name is blank
            $validuser = array("yes", "true");
            echo implode(',', $validuser);
            exit;
        }
        else {
            //username is not blank, so it's taken
            $validuser = array("no", "false");
            echo implode(',', $validuser);
            exit;
        }
    }
}
}

并且只是为了展示我实际上对这些信息所做的事情,这是 java 的一部分(主要只处理用户名,还有更多的电子邮件,不包括在内): fiddle

而且,粗略地说,指向页面的链接:页面链接

我一直在这里修复其他东西,并且在技术上它有效。如果数据库中有与我输入的用户名匹配的内容,我会收到响应,但如果不匹配,由于某种原因它根本没有响应.....

具体来说......在类中倒数第二个函数的底部:

$stmt->close(); 
//var_dump($results); 
if ($results == "" || $results == NULL) 
{ 
    return null; 
} else { 
    return $results; 
}
4

1 回答 1

1

当您没有向客户端返回任何结果时,您需要向客户端表明这是您所做的,并且上面显示的代码在这种情况下只是不输出任何内容。虽然很容易在客户端正确处理这个空响应,但更好的解决方案是执行以下操作之一:

  • 如果您需要结果中的数据,json_encode()则在将结果发送回客户端之前。这意味着如果没有结果,您将返回一个空数组,但它仍然是有效的 JSON,而且至关重要的是,您可以使用result.length.
  • 如果您实际上不需要结果数据,而您只需要确定是否有任何结果,则可以简单地返回 1 或 0。这种布尔响应需要最少的带宽和最少的处理,最好的事情是您需要做的就是在客户端将其评估为布尔值 - 即if (result) { /* do stuff */ }
于 2012-08-07T22:57:57.740 回答