2012-09-04 16 views
5

के साथ एकाधिक पंक्तियों को लौटाना पिछले दो दिनों से या तो मैं अपने कार्यों को mysqli में परिवर्तित कर रहा हूं। मैंने एक समस्या में भाग लिया है। मेरे पास एक ऐसा फ़ंक्शन है जो डेटाबेस से एक पंक्ति युक्त एक सरणी देता है। हालांकि, मैं चाहता हूं कि सरणी में एक से अधिक पंक्तियां हों। साथ ही, मैं अलग-अलग पदों को कैसे प्रतिबिंबित कर पाऊंगा। यहां मेरा असफल प्रयास है जो केवल सरणी में एक पंक्ति प्रदर्शित करता है।MySqli और Arrays

$mysqli = new mysqli("localhost", "user", "password", "database"); 

function display_posts ($mysqli, $user_id) { 

    $fields = "`primary_id`, `poster_id`, `profile_user_id`, `post`"; 

    $user_id = (int)$user_id; 

    $query = "SELECT DISTINCT $fields FROM `posts` WHERE `profile_user_id` = $user_id 
    LIMIT 4";      

    if ($result = $mysqli->query($query)) { 

    $row = $result->fetch_assoc(); 

    return $row; 

    $result->free(); 

    $stmt->close(); 

}} 

यहां मैं डेटा प्रदर्शित करने की कोशिश कर रहा हूं।

$user_id = 1; 

$posts = display_posts($mysqli, $user_id); 

//Not sure what to do with $posts. A While loop perhaps to display each post? 

उत्तर

11

आप एक पाश का उपयोग करने के उन्हें एक बार में प्राप्त करने के लिए:

<?php 
function resultToArray($result) { 
    $rows = array(); 
    while($row = $result->fetch_assoc()) { 
     $rows[] = $row; 
    } 
    return $rows; 
} 

// Usage 
$query = 'SELECT DISTINCT $fields FROM `posts` WHERE `profile_user_id` = $user_id LIMIT 4'; 
$result = $mysqli->query($query); 
$rows = resultToArray($result); 
var_dump($rows); // Array of rows 
$result->free(); 
+0

हालांकि मैं एक उत्तर प्राप्त करने की उम्मीद कर रहा था कि कैसे ec प्रत्येक व्यक्तिगत मूल्य को बाहर निकालो, मैंने आगे बढ़कर खुद को समझ लिया। फिर भी मदद के लिए धन्यवाद। मैंने फ़ंक्शन 'परिणाम टॉअरे' निकालने का निर्णय लिया और इसे मेरे मूल कार्य में जोड़ दिया। – jason328

5

क्यों इस तरह सीधे का उपयोग नहीं:

$result = mysqli_fetch_all($mysqli->query($query), MYSQLI_ASSOC); 
+1

यह एक बेहतर जवाब होगा यदि आपने समझाया कि यह –

+1

@KateGregory क्यों काम करता है वास्तव में व्याख्या करने के लिए कुछ भी नहीं है। इस तथ्य के अलावा कि यह फ़ंक्शन केवल 5.3 से ही उपलब्ध है और केवल mysqlnd के अंतर्गत है। –

+0

व्याख्या करने के लिए बहुत कुछ नहीं है। थोड़ी देर के चक्र की बजाय, परिणामों के पूरे सेट को प्राप्त करने के लिए उपलब्ध mysqli विधियों का उपयोग करें। इसके लिए एक अतिरिक्त समारोह घोषित करने से भी बचें। और हाँ, यह> 5.3 ऊपर है। लेकिन जहां तक ​​मुझे पता है कि mysqli भी 5.3 और ऊपर है –

1

मुझे देर हो रही है, लेकिन मैं इस पर विश्वास जो आप प्राप्त करना चाहते थे:

$mysqli = new mysqli("localhost", "user", "password", "database"); 
$fields = "`primary_id`, `poster_id`, `profile_user_id`, `post`"; 

function display_posts() { 

    global $mysqli; 
    global $fields; 

    $query = "SELECT DISTINCT $fields FROM `posts` WHERE `profile_user_id` = $user_id LIMIT 4"; 

    $posts = $mysqli -> query($query) or die('Error: '.$mysqli -> error); 

    if ($posts -> num_rows > 0) { 

     while ($row = $posts -> fetch_assoc()) { 

     $value = $row['/*The table column here (You can repeat this line with a different variable e.g. $value 2, $value 3 etc and matching them with the respective table column)*/']; 

     echo $value./*Concatenate the other variables ($value 1 etc) here*/'<br />'; 

     } 

    }else { 

     echo 'No records found.'; 

    } 

} 

//Call the function 
display_posts();