2008-10-01 21 views
22

मैं धीरे-धीरे mysql_ से PDO कार्यों में से सभी को धीरे-धीरे ले जा रहा हूं और मैंने अपनी पहली ईंट की दीवार को मारा है। मुझे नहीं पता कि पैरामीटर के साथ परिणामों के माध्यम से लूप कैसे करें। मैं निम्नलिखित के साथ ठीक हूँ:PHP में पीडीओ के माध्यम से मैं एक MySQL क्वेरी के माध्यम से कैसे लूप करूं?

foreach ($database->query("SELECT * FROM widgets WHERE something='something else'") as $results) 
{ 
    echo $results["widget_name"]; 
} 

जाहिर है 'कुछ और' गतिशील हो जाएगा:

foreach ($database->query("SELECT * FROM widgets") as $results) 
{ 
    echo $results["widget_name"]; 
} 

लेकिन अगर मैं इस तरह कुछ करना चाहता हूँ।

उत्तर

58

यहां डीबी से कनेक्ट करने के लिए पीडीओ का उपयोग करने के लिए एक उदाहरण है, इसे PHP त्रुटियों के बजाय अपवादों को फेंकने के लिए कहें (आपके डीबगिंग में मदद मिलेगी), और क्वेरी में गतिशील मानों को प्रतिस्थापित करने के बजाय पैरामीटरयुक्त कथन का उपयोग करना (अत्यधिक अनुशंसित):

// Wrap a PDOStatement to iterate through all result rows. Uses a 
// local cache to allow rewinding. 
class PDOStatementIterator implements Iterator 
{ 
    public 
     $stmt, 
     $cache, 
     $next; 

    public function __construct($stmt) 
    { 
     $this->cache = array(); 
     $this->stmt = $stmt; 
    } 

    public function rewind() 
    { 
     reset($this->cache); 
     $this->next(); 
    } 

    public function valid() 
    { 
     return (FALSE !== $this->next); 
    } 

    public function current() 
    { 
     return $this->next[1]; 
    } 

    public function key() 
    { 
     return $this->next[0]; 
    } 

    public function next() 
    { 
     // Try to get the next element in our data cache. 
     $this->next = each($this->cache); 

     // Past the end of the data cache 
     if (FALSE === $this->next) 
     { 
      // Fetch the next row of data 
      $row = $this->stmt->fetch(PDO::FETCH_ASSOC); 

      // Fetch successful 
      if ($row) 
      { 
       // Add row to data cache 
       $this->cache[] = $row; 
      } 

      $this->next = each($this->cache); 
     } 
    } 

}

तो यू करने के लिए:

// $attrs is optional, this demonstrates using persistent connections, 
// the equivalent of mysql_pconnect 
$attrs = array(PDO::ATTR_PERSISTENT => true); 

// connect to PDO 
$pdo = new PDO("mysql:host=localhost;dbname=test", "user", "password", $attrs); 

// the following tells PDO we want it to throw Exceptions for every error. 
// this is far more useful than the default mode of throwing php errors 
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

// prepare the statement. the place holders allow PDO to handle substituting 
// the values, which also prevents SQL injection 
$stmt = $pdo->prepare("SELECT * FROM product WHERE productTypeId=:productTypeId AND brand=:brand"); 

// bind the parameters 
$stmt->bindValue(":productTypeId", 6); 
$stmt->bindValue(":brand", "Slurm"); 

// initialise an array for the results 
$products = array(); 
if ($stmt->execute()) { 
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { 
     $products[] = $row; 
    } 
} 

// set PDO to null in order to close the connection 
$pdo = null; 
5

PHP documentation है अनुसार कहते हैं आप निम्न कार्य करना करने के लिए सक्षम होना चाहिए:

$sql = "SELECT * FROM widgets WHERE something='something else'"; 
foreach ($database->query($sql) as $results) 
{ 
    echo $results["widget_name"]; 
} 

मैं कोई विशेषज्ञ हूँ, लेकिन यह काम करना चाहिए।

+3

यह 'कुछ और' से बचने का ख्याल नहीं रखता जो गतिशील हो सकता है। Shabbyrobe द्वारा सचित्र के रूप में तैयार प्रश्न उत्तर है। – DGM

+5

@ डीजीबी, मूल प्रश्न के उदाहरण से डेरिल की 'कुछ और'। प्रश्न में गतिशील रूप से संयोजन प्रश्नों के साथ कुछ लेना देना नहीं है, इसे किसी प्रश्न के परिणामों पर पुनरावृत्ति के साथ करना है। जिसने सही ढंग से उत्तर दिया। हालांकि मैं मानता हूं कि शबबीरोबे ने * बेहतर * जवाब दिया था। –

+0

yup, यह निश्चित रूप से सही है, और छोटा है। – andyk

3

आप foreach वाक्य रचना चाहते हैं, तो आप निम्नलिखित वर्ग का उपयोग कर सकते se:

foreach(new PDOStatementIterator($stmt) as $col => $val) 
{ 
    ... 
} 
संबंधित मुद्दे