10/10/2018, 10:30
Convert Result sql to array
mình đang viết một hàm có chức năng show dữ liệu trong database kết quả sau khi thực hiện nó là một object (sau khi thực hiện hàm mysql_query) mình muốn chuyển kết quả này sang array bạn nào có cách chuyển sang hog zậy. thank
[=========> Bổ sung bài viết <=========]
Kết quả của mình muốn có dạng như zầy: $arr = array('0'=>array(....),'1'=>array(....),....)
[=========> Bổ sung bài viết <=========]
Kết quả của mình muốn có dạng như zầy: $arr = array('0'=>array(....),'1'=>array(....),....)
Bài liên quan





"truy show" là gì ?
tại sao kết quả đưa về lại là object , khi mục tiêu của bác mún lại là array
Cho cái class tham khảo:
<?php class sql_query { var $DBHOST;//Host for connecting to var $DBUSER;//Mysql Username var $DBPASS;//Mysql Password var $DBNAME;//Your require Database function set_vars($host="localhost", $username="", $password="", $db="") { $this->DBHOST = $host; $this->DBUSER = $username; $this->DBPASS = $password; $this->DBNAME = $db; } function query($query) { // connecting, selecting database $sql_link = mysql_connect("$this->DBHOST","$this->DBUSER","$this->DBPASS") or print "Could not connect to database"; // select the database mysql_select_db("$this->DBNAME") or print "Could not select database"; // make the query $result = mysql_query($query); if (!$result) { return mysql_errno(); } else { $this->result_raw = $result; } } function tableindex() { while ($fields = mysql_fetch_field($this->result_raw)) { $colunas[] = $fields->name; } $this->columns = $colunas; } function tablemount() { while ($row = mysql_fetch_object($this->result_raw)) { $return[] = $row; } $this->result=$return; } } ?><? if ($server != "" && $user != "" && $pass != "" && $query != "" && $database != "") { $table = new sql_query; $table->set_vars("$server","$user","$pass","$database"); $table->query("$query"); $table->tableindex(); $table->tablemount(); print "<table border=1><tr>\n"; foreach ($table->columns as $coluna) { print "<td><b>$coluna</b></td>"; } print "</tr>"; foreach ($table->result as $row) { print "<tr>\n"; foreach($table->columns as $column) { $field_result = $row->$column; print "<td>$field_result  </td>"; } print "</tr>\n"; } print "</table>" ; } ?>còn bình thường bạn muốn chuyển từ object -> array ? 2 cái này đâu tương đồng đâu mà chuyển ?
nếu vấn đề bài toán là ở chỗ phải filter dữ liệu từ nhiều table , bạn có thể lọc từng table , trả về array , và in dữ liệu ra . hoặc dùng 1 query kết hợp nhiều table để trả dữ liệu về array .
ko hiểu được vì sao phải cứ đưa nó về object , trong khi mục tiêu là array ?