2015-12-30 63 views
0

我對PHP中的MySQL有一些疑問。函數中如何返回mysql_fetch_array

這是我的配置代碼:

<?php 

class Core{ 

    public $connect; 

public function __construct(){ 
    $host = 'localhost'; 
    $user = 'root'; 
    $password = ''; 
    $database = 'fix'; 
    $this->connect = new mysqli($host,$user,$password,$database); 
    if(!$this->connect) echo mysql_error(); 
    } 

    public function siteTitle(){ 
    $title = "title"; 
    $tagline = "tagline"; 
    return $title. " - ".$tagline; 
    } 

    public function load($file,$data){ 
     extract($data); 
     require "$file.php"; 
    } 

    public function searchResult(){ 
     $query = $this->connect->query("SELECT * FROM `location`"); 
     if($query->num_rows > 0){ 
      $columns = $query->fetch_assoc(); 
      return $columns;  
     } 
    } 



    public function locationStatus($id){ 
     $location = $this->searchResult(); 
     $locationId = $location['id']; 
     if($locationId == 1){ 
      echo "Available <i class='ui icon circle green'></i>"; 
     }elseif($locationId == 2){ 
      echo "Under Counstruction <i class='ui icon circle orange'></i>"; 
     }else{ 
      echo "Unavailable <i class='ui icon circle red'></i>"; 
     } 

    } 


} 


?> 

我將顯示來自信息搜索結果(結果)函數searchResult.php文件:

 <div style='border:1px solid #ccc; padding:3px;'> 
     <img src="asset/images/<?php echo "$data[image].jpg"; ?>" class='ui image medium' style='padding-right:10px;float:left' /> 
     <h2 style='font-family:nexa;color:rgb(36,162,217);line-height:0;'><?php echo $data['title']; ?></h2> 
     <h4><span style='color:orange;'>Location Address: </span><?php echo $data['subtitle']; ?></h4> 
     <p><?php echo $data['description']; ?></p> 
     <br /> 
     <p> 
     <i class='ui icon marker big orange'></i>Checkin: <?php echo $data['checkin']; ?> &nbsp;&nbsp;&nbsp;<i class='ui icon clock big orange'></i>Published at: <?php echo $data['published']; ?>&nbsp;&nbsp;&nbsp; Status: <?php echo $this->locationStatus($data['id']); ?> 
     </p> 
     <a style='' href='location/<?php echo $data['slug']; ?>' class='ui button blue'>READ MORE</a> 

</div> 

而且searchResult.php,我叫它用的index.php代碼:

<?php echo $this->load('templates/searchResult', $this->searchResult()); ?> 

我的問題是:

爲什麼我的sql查詢的結果不是循環?雖然,我正在循環它與一個while循環?

謝謝。對不起,如果我的語言不好。

+0

檢查方法searchResult()**沒有while循環** – RiggsFolly

+0

是啊,什麼'while'循環?可能是'$ data'的那個' –

+0

@RiggsFolly哦,對不起,在我分享的代碼之前,代碼與while不同,如果它不工作循環。 – filosofikode

回答

1

如果你想這樣調用

<?php 
    echo $this->load('templates/searchResult', $this->searchResult()); 
?> 

你將不得不把循環的searchResult()方法,使這一個調用將返回所有行的視圖。

public function searchResult(){ 
    $query = $this->connect->query("SELECT * FROM `location`"); 

    $rows = array(); 

    if($query->num_rows > 0){ 
     while ($row = $query->fetch_assoc()) { 
      $rows[] = $row; 
     } 
    } 
    return $rows;  
} 
在你看來,循環

然後在$data

+0

好的,它的作品!謝謝 – filosofikode

+0

作爲'while'的替代方法,[fetch_all](http://php.net/manual/en/mysqli-result.fetch-all.php)可以與'resulttype'設置爲'MYSQLI_ASSOC'一起使用,像這樣:'$ rows = $ query-> fetch_all(MYSQLI_ASSOC);'。 – FirstOne

0
public function searchResult(){ 
    $query = $this->connect->query("SELECT * FROM `location`"); 
    if($query->num_rows > 0){ 
     while ($columns = $query->fetch_assoc()) 
     { 
      $data[] = $columns; 
     } 
    } 
    return $data; 
} 

您需要在信息搜索結果的功能while循環。

+0

好的,謝謝你的回答:D – filosofikode