使用PDO替换mysql_connect-格式正确吗? [英] Using PDO to replace mysql_connect - formatting correctly?
问题描述
这是我当前的页面:
<?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;?>
我想将其转换为与PDO兼容的代码,因为它已在我的php.ini中启用
I want to convert this to code that works with PDO, since it is enabled in my php.ini
我打算(针对该项目和所有未来项目)逐步淘汰PDO,以使其与PDO一起使用.
How would I get PDO to work with this, as I'm intending on (for this project and all future ones) phasing out use of the older mysql_connect.
我在 Zend开发人员区域,尽管我可以对基于Dwoo的项目进行平均处理,但此模板不不使用模板引擎-它是纯PHP语法,不使用模板,仅使用各种include()
和require,并在需要时加上echo()
.
I had a look at how to do it at the Zend Developer Zone and although I can do it at an average level for Dwoo-based projects, this template does not use a templating engine - it is pure PHP-based syntax, no templates used, only various include()
and require, plus echo()
where needed.
感谢您的帮助!
推荐答案
这是您的解决方案.
<?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;?>
这篇关于使用PDO替换mysql_connect-格式正确吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!