2009-06-14 19 views
8

मैं एक एसक्यूएल क्वेरी और mysqli तैयार बयान है:मैं एक MySQLi तैयार कथन के परिणामों को एक सहयोगी सरणी में कैसे डाल सकता हूं?

$sql = 'SELECT photographers.photographer_id, photographers.photographer_name 
    FROM photographers'; 

$stmt = $conn->stmt_init(); 
if ($stmt->prepare($sql)) { 
    $stmt->bind_result($photographer_id, $photographer_name); 
    $OK = $stmt->execute(); 
    $stmt->fetch(); 
} 

मैं कैसे स्टोर कर सकते हैं एक साहचर्य सरणी में परिणाम तो मैं पाश इसे बाद में और सभी डेटा एसक्यूएल स्ट्रिंग द्वारा वापस करने के लिए मिल सकता है?

उत्तर

30

निम्नलिखित का प्रयास करें:

$meta = $statement->result_metadata(); 

while ($field = $meta->fetch_field()) { 
    $params[] = &$row[$field->name]; 
} 

call_user_func_array(array($statement, 'bind_result'), $params);    
while ($statement->fetch()) { 
    foreach($row as $key => $val) { 
     $c[$key] = $val; 
    } 
    $hits[] = $c; 
} 
$statement->close(); 

सबसे पहले आप क्वेरी मेटाडाटा हो और कि से सभी क्षेत्रों आप गए लाए जाने के (आप इस मैन्युअल रूप से कर सकता है प्राप्त है, लेकिन इस कोड से निर्माण के बजाय सभी प्रश्नों के लिए काम करता है हाथ)। call_user_func_array() फ़ंक्शन उन पैरामीटरों पर आपके लिए mysqli_stmt::bind_result() फ़ंक्शन कॉल करता है।

उसके बाद यह प्रत्येक पंक्ति के माध्यम से चलने और प्रत्येक पंक्ति के लिए एक एसोसिएटिव सरणी बनाने और एक परिणाम में परिणामस्वरूप सभी परिणामों में जोड़ने का मामला है।

+0

+1। बहुत रचनात्मक।:) –

+0

यह मेरा कोड नहीं है, मुझे याद नहीं है कि मुझे यह कहां से मिला। Google में कोई संदेह नहीं होगा – Chris

+1

उत्तर दें: http://us2.php.net/manual/en/mysqli-stmt.bind-result.php – Chris

1

विचित्र रूप से पर्याप्त, आप नहीं कर सकते। Mysqli_stmt उदाहरण से mysqli_result ऑब्जेक्ट प्राप्त करने का कोई तरीका नहीं है। मैंने हमेशा यह एक बड़ा दोष माना है, और अनुमान लगाएगा कि यह एक प्रमुख कारण है कि Mysqli कभी भी किसी भी लोकप्रिय लोकप्रियता तक नहीं पहुंच पाया। इन दिनों पीडीओ द्वारा इसे बहुत अधिक पसंद किया गया है, जो आप प्रयास के साथ चाहते हैं।

संपादित करें: मेरा उत्तर केवल इसका मतलब है कि आप इसे डिफ़ॉल्ट रूप से नहीं कर सकते हैं। निश्चित रूप से आप इसे स्वयं लागू कर सकते हैं, जैसे कि क्रिस ने सुझाव दिया था। फिर भी, मुझे लगता है कि यदि संभव हो तो आपको पीडीओ का उपयोग करना चाहिए।

1

यदि आप पीडीओ एक्सटेंशन का उपयोग नहीं कर सकते हैं। या आपको तैयार डेटाबेस के साथ अपनी डेटाबेस कक्षा बनाने में परेशानी हो रही है। कैसे डालने अद्यतन के लिए उपयोग करते हैं, हटा सकते हैं और डालने के लिए:

$db = new database(); 
    $db->query = "INSERT INTO blabla (name,date,number) VALUES(?,?,?)"; 
    $db->params = array($name,$date,$number); 
    $db->type = 'ssi'; //s=string,i=integer 
    if($db->insert()) 
     echo 'success'; 

काम करता है डेटाबेस वर्ग

class database { 

     private $stmt; 
     private $mysqli; 
     private $query; 
     private $params = array(); 
     private $type; 

     public function __set($name, $value) { 
      switch ($name) { 
       case 'params': 
        $this->params = $value; 
        break; 
       case 'query': 
        $this->query = $value; 
        break; 
       case 'type': 
        $this->type = $value; 
        break; 
       default: 
        break; 
      } 
     } 

     public function __get($name) { 
      if ($name !== "mysqli" && $name !== "stmt") 
       return $this->$name; 
     } 

     public function __construct() { 
      $this->mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME, DB_PORT); 
      $this->stmt = $this->mysqli->stmt_init(); 
     } 

     private function close_con($bool) { 
      if ($bool) { 
       $this->stmt->free_result(); 
      } 
      $this->stmt->close(); 
      $this->mysqli->close(); 
     } 

     private function nofetch() { 
      $this->stmt->prepare($this->query); 
      $bind_names[] = $this->type; 
      for ($i = 0; $i < count($this->params); $i++) { 
       $bind_name = 'bind' . $i; 
       $$bind_name = $this->params[$i]; 
       $bind_names[] = &$$bind_name; 
      } 
      call_user_func_array(array($this->stmt, "bind_param"), $bind_names); 

      if ($this->stmt->execute()) { 

       $this->close_con(false); 
       return true; 
      } 
      $this->close_con(false); 
      return false; 
     } 

     public function insert() { 
      if ($this->nofetch()) { 
       return true; 
      } 
      return false; 
     } 

     public function update() { 
      if ($this->nofetch()) { 
       return true; 
      } 
      return false; 
     } 

     public function delete() { 
      if ($this->nofetch()) { 
       return true; 
      } 
      return false; 
     } 

     public function fetch() { 
      $result_out = array(); 
      $this->stmt->prepare($this->query); 
      $bind_names[] = $this->type; 
      if (count($this->params) > 0) { 
       for ($i = 0; $i < count($this->params); $i++) { 
        $bind_name = 'bind' . $i; 
        $$bind_name = $this->params[$i]; 
        $bind_names[] = &$$bind_name; 
       } 
       call_user_func_array(array($this->stmt, "bind_param"), $bind_names); 
      } 
      if ($this->stmt->execute()) { 
       $result = $this->stmt->result_metadata(); 
       $cols = $result->fetch_fields(); 
       foreach ($cols as $col) { 

        $name = str_replace("-", "_", $col->name); 

        $$name = null; 
        if ($name == null) 
         $name = 'name'; 
        $bindarray[$name] = &$$name; 
       } 

       call_user_func_array(array($this->stmt, 'bind_result'), $bindarray); 
       $this->stmt->store_result(); 
       $copy = create_function('$a', 'return $a;'); 
       while ($this->stmt->fetch()) { 
        $result_out[] = array_map($copy, $bindarray); 
       } 
      } 
      $this->close_con(true); 
      return $result_out; 
     } 

    } 

मुझे आशा है कि यह उपयोगी

है के लिए कुछ अलग

$array = array(); 
    $db = new database(); 
    $db->query = "SELECT * FROM blabla WHERE id=? and someother=?"; 
    $db->params = array($id,$other); 
    $db->type = 'is'; 
    $r = $db->fetch(); 
    //$r[0]['id'] for row 1 
    //$r[0]['name'] for row 1 
    //$r[1] .... For row 2 
    //$r[2] .... For row 3 
    //etc... 

लायें अब

5

अद्यतन: चूंकि PHP 5.3.0 आप एक mysqli_result ऑब्जेक्ट प्राप्त कर सकते हैं जो fetch_array विधि प्रदान करता है।

$sql = 'SELECT photographers.photographer_id, photographers.photographer_name 
    FROM photographers'; 
$data = null; 

$stmt = $conn->stmt_init(); 
if ($stmt->prepare($sql)) { 
    $stmt->bind_result($photographer_id, $photographer_name); 
    $OK = $stmt->execute(); 
    $result = $stmt->get_result(); 
    $data = $result->fetch_array(); 
} 

प्रलेखन: http://php.net/manual/en/mysqli-stmt.get-result.php

+3

ध्यान दें कि, जैसा कि दस्तावेज़ कहते हैं, यह केवल तभी काम करता है जब आपके पास MySQL देशी ड्राइवर स्थापित और काम कर रहा हो, जो आपके इंस्टॉलेशन में हो। – PaulJ

1

मैं आदेश MySQLi mysqlnd बिना विवरण तैयार से डेटा प्राप्त करने के लिए एक समाधान खोजने के लिए इस चर्चा में आए। मैं एक आसान तरीके से MySQLi के साथ तैयार बयानों को संभालने के लिए एक कक्षा विकसित कर रहा हूं। कृपया, तैयार कथन लिखने और इसके परिणाम प्राप्त करने के लिए कोड पर नज़र डालें, या बस इसका उपयोग करें (कोड के टुकड़े के अंत में उपयोग का एक उदाहरण देखें)।

class DbUtils { 

    private $host; 
    private $user; 
    private $pass; 
    private $database; 
    private $connection; 

    public function __construct($host, $user, $pass, $database) { 

     $this->host = $host; 
     $this->user = $user; 
     $this->pass = $pass; 
     $this->database = $database; 
     $this->connection = new mysqli($host, $user, $pass, $database); 

    } 

    public function query(Array $params) { 

     $args = array(); 

     // 0. Correct the input function parameters 
     if (array_key_exists("query", $params)) { 
      $args["query"] = $params["query"]; 
     } else { 
      throw new Exception("Parameter not found: 'query'."); 
     } 
     if (array_key_exists("types", $params)) { 
      $args["types"] = $params["types"]; 
     } else { 
      $args["types"] = ''; 
     } 
     if (array_key_exists("input", $params)) { 
      $args["input"] = $params["input"]; 
     } else { 
      $args["input"] = array(); 
     } 

     // 1. Check the connection: 
     if ($this->connection->connect_errno) { 
      echo "Connection to MySQL failed: [" . $this->connection->connect_errno . "]: " . $this->connection->connect_error . "<br/>"; 
     } 

     // 2. Prepare the sentence: 
     if (!($stmt = $this->connection->prepare($args["query"]))) { 
      echo "Prepared statement failed: [" . $stmt->errno . "]: " . $stmt->error . "<br/>"; 
     } 

     // 3. Bind the input parameters: 
     if ((0 != sizeof($args["input"])) && !(call_user_method_array("bind_param", $stmt, array_merge(array($args["types"]), $args["input"])))) { 
      echo "Binding parameters failed: [" . $stmt->errno . "]: " . $stmt->error . "<br/>"; 
     } 

     // 4. Execute the sentence 
     if (!($stmt->execute())) { 
      echo "Sentence execution failed: [" . $stmt->errno . "]: " . $stmt->error . "<br/>"; 
     } 

     // 5. Bind the results: 
     $data = array(); 
     $meta = $stmt->result_metadata(); 
     $row = array(); 
     while($field = $meta->fetch_field()) { 
      $argos[] = &$row[$field->name]; 
     } 
     call_user_method_array('bind_result', $stmt, $argos); 

     // 6. Collect the results: 
     while ($stmt->fetch()) { 
      foreach($argos as $key => $val) { 
       $dataItem[$key] = $val; 
      } 
      $data[] = $dataItem; 
     } 

     // 7. Close the sentence: 
     $stmt->close(); 

     // 8. Return interesting data properly ordered: 
     return $data; 
    } 

} 

// 1. Instantiate it: 
$dbUtils = new DbUtils(
    "127.0.0.1", 
    "user", 
    "password", 
    "database" 
); 

// 2. Query prepared statements like this: 
$users = $dbUtils->query(array(
    "query" => "SELECT * FROM user WHERE name LIKE ? AND pass LIKE ?;", 
    "input" => array('%', '%'), 
    "types" => 'ss' 
)); 

// 3. Enjoy securely CRUD Ops! 
0

एक साधारण जो वास्तव में आश्चर्यजनक रूप से काम करता है। मुझे पता है कि यह प्रक्रियात्मक है, लेकिन अभी भी:

$query = "SELECT * FROM foo WHERE bar = ?;"; 

$stmt = mysqli_prepare($dbc, $query); 

mysqli_stmt_bind_param($stmt, "s", $bar); 

mysqli_stmt_execute($stmt); 

$result = mysqli_stmt_get_result($stmt); 

return mysqli_fetch_assoc($result); 
संबंधित मुद्दे