基本上 displayCharityList () 中的代码有效。我决定将该代码放在该函数中,将该函数放在一个类中,然后尝试通过稍后调用它来使其工作......
不知何故,它没有从数据库中提取数据。查询在函数内部有效,但在 __contruct 中无效
谁能告诉我我做错了什么?
谢谢
<?php
// Connection setup
class charity {
public $h;
function __construct () {
$c = new PDO('mysql:host=localhost;dbname=seafarer_v2','seafarer_user','supp0rt1243');
$h = $c->query('SELECT * FROM mnwg_charities')->fetchAll(PDO::FETCH_ASSOC);
$this->$h = $this->h;
$this->displayCharity = $this->displayCharityList();
}
Public function displayCharityList(){
echo "<table><tr><th>Name</th><th>Contact</th><th>Post code</th><th>Phone</th><th>Website</th><th>Email</th></tr>";
foreach ($this->h as $r){
echo "<tr><td>";
echo $r['Name'];
echo "</td>";
echo "<td>";
echo $r['Contact'];
echo "</td>";
echo "<td>";
echo $r['Postcode'];
echo "</td>";
echo "<td><div class=\"phone\">";
echo "<img src=\"http://m.intertrustgroup.com/images/icon_phone.png\" style=\"margin-right:.5em\">{$r['Phone']}</br>";
if($r['Fax']){ echo "<img src=\"http://www.sliksvn.com/gfx/icon_fax.gif\" style=\"margin-right:.5em\">{$r['Fax']}";}
echo "</div></td>";
echo "<td>";
echo "<a href=\"{$r['Website']}\" target=\"_blank\"> Visit Website </a>";
echo "</td>";
echo "<td>";
echo "<a href=\"mailto:{$r['Email']}\">Send Email</a>";
echo "</td></tr>";
}
echo "</table>";
}
}
?>
<?php
$hola = new charity();
echo $hola->displayCharity();
?>