我對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']; ?> <i class='ui icon clock big orange'></i>Published at: <?php echo $data['published']; ?> 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循環?
謝謝。對不起,如果我的語言不好。
檢查方法searchResult()**沒有while循環** – RiggsFolly
是啊,什麼'while'循環?可能是'$ data'的那個' –
@RiggsFolly哦,對不起,在我分享的代碼之前,代碼與while不同,如果它不工作循環。 – filosofikode