rad*_*k86 6 php mysql pdo echo
这是我当前的页面:
<?php
mysql_connect('localhost', 'root', 'mypass') or die (mysql_error());
mysql_select_db('radio1') or die (mysql_error());
$result = mysql_query("SELECT *, TIME_FORMAT(airtime, '%H:%i') `airtime`
from presenters");
//Table starting tag and header cells
while($row = mysql_fetch_array($result)) {
?>
<?php foreach($rows as $row):?>
<dl class="standard">
<dt><a href="<?=$row=['link'] ?>" title="<?=$row=['presenter'] ?>"><?=$row['airtime'] . " - " .$row['presenter']?></a></dt>
<dd class="itemimg"><a href="<?=$row=['link'] ?>" title="<?=$row=['presenter'] ?>"><img src="<?=$row['image']; ?>" width="100" height="75" alt="<?=$row=['presenter'] ?>" title="<?=$row=['presenter'] ?>" /></a></dd>
<dd class="itemdesc">
<?=$row['showinfo']; ?>
</dd>
<dd class="itemlink">
<a href="<?=$row=['link'] ?>" title="Find out more..."><span> </span>
<?=$row['more']; ?></a>
</dd>
</dl>
<?php endforeach;?>
Run Code Online (Sandbox Code Playgroud)
我想将此转换为适用于PDO的代码,因为它在我的php.ini中启用
我如何让PDO使用它,因为我打算(对于这个项目和所有未来的项目)逐步淘汰使用旧的mysql_connect.
我在Zend Developer Zone看了一下如何做到这一点,虽然我可以在基于Dwoo的项目的平均水平上做到这一点,但这个模板不使用模板引擎 - 它是基于PHP的纯语法,没有使用模板,只有各种各样include(),需要,加上echo()需要的地方.
任何帮助表示赞赏!
这是您的解决方案。
<?php
$hostname = "localhost";
$username = "root";
$password = "mypass";
$dbname = 'radio1';
$dbh =null;
try {
$dbh = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password);
}
catch(PDOException $e)
{
echo $e->getMessage();
}
$result = $dbh->query("SELECT *, TIME_FORMAT(airtime, '%H:%i') `airtime` from presenters");
//Table starting tag and header cells
while($row = $result->fetch ()) {
?>
<?php foreach($rows as $row):?>
<dl class="standard">
<dt><a href="<?=$row=['link'] ?>" title="<?=$row=['presenter'] ?>"><?=$row['airtime'] . " - " .$row['presenter']?></a></dt>
<dd class="itemimg"><a href="<?=$row=['link'] ?>" title="<?=$row=['presenter'] ?>"><img src="<?=$row['image']; ?>" width="100" height="75" alt="<?=$row=['presenter'] ?>" title="<?=$row=['presenter'] ?>" /></a></dd>
<dd class="itemdesc">
<?=$row['showinfo']; ?>
</dd>
<dd class="itemlink">
<a href="<?=$row=['link'] ?>" title="Find out more..."><span>
</span>
<?=$row['more']; ?></a>
</dd>
</dl>
<?php endforeach;?>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8929 次 |
| 最近记录: |