我对 PHP 很陌生,但我是一个经验丰富的 Java 程序员。
我知道我想用我的代码实现什么,即
while(result.Hasnext()){ array.add(result.next)}
现在我有以下php代码:
$myArr = array();
$query = 'SELECT message from shoutbox';
$result = $this->db->query($query);
我如何将我得到的所有数据添加到 php 中的数组中?我知道这很简单,但我一直在到处寻找例子,只是没有找到一个。
我完整的数据库课程
<?php
class database{
private $server = "db9.meebox.net";
private $usedb = "marcrasm_website";
private $user = "******";
private $pass = "******";
private $db;
public function __construct() {
$this->db = new mysqli($this->server, $this->user, $this->pass, $this->usedb);
if ($this->db->connect_errno) {
printf('Error: Connect failed: %s', $mysqli->connect_error);
exit();
}
}
public function sendMessage($message){
$currentTime = $_SERVER['REQUEST_TIME'];
$date = date('Y/m/d H:i:s');
$query = "INSERT INTO shoutbox VALUES ('NULL','$message','$date')";
$this->db->query($query);
}
public function loadMesssages(){
$query = 'SELECT message from shoutbox ORDER BY created DESC';
$result = $this->db->query($query);
if (mysql_num_rows($result) == 0){
//no results
}else{
// returned at least one result.
while($row = mysql_fetch_assoc($result)){
$myArr [] = $row;
}
}
return $myArr;
}
}
?>