-1

空白=空白のテーブルからselect*のクラスでクエリを呼び出すたびに、クラスの最後の結果の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

そして、大まかに言って、ページへのリンク:ページリンク

私はここで他のことを修正してきました、そして技術的にはそれは機能します。私が入力したユーザー名と一致するものがデータベースにある場合は応答がありますが、一致するものがない場合は、何らかの理由でまったく応答しません。

具体的には...クラスの最後から2番目の関数の一番下にあります。

$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 に答える