我在 iclude 中有此代码用于连接到数据库我在此文件中也有 mysql 连接功能,但我决定将 mysql 更改为 pdo -include.php:
try{
$conn = new PDO('mysql:host=localhost;dbname=databasename', 'username', 'password');
return $conn;
}
catch(PDOException $e)
{
echo 'can not connect to database';
exit();
}
我在 index.php 中有 mysql 查询,但我想将此代码更改为 pdo
<?php
$sql=mysql_query("SELECT id,col1 FROM tblname ORDER BY col4 DESC, col3 DESC, tzade DESC LIMIT 18");
if(mysql_num_rows($sql)) {
$j=0;
while($result = mysql_fetch_object($sql)) {
$j++;
if($result->id == $site_id){
?>
<a class="normal_link" href="l.php?lid=<?php echo $result->id; ?>&title=<?php echo title($result->col1);?>">
<div id="jadv">
<div><?php echo $j; ?></div>
<div><?php echo $result->col1; ?></div>
</div>
</a>
<?php
}
else{
?>
<a class="normal_link" href="l.php?lid=<?php echo $result->id; ?>&title=<?php echo title($result->col1);?>">
<div id="jadva">
<div id="jad"><?php echo $j; ?></div>
<div id="jad"><?php echo $result->col1; ?></div>
</div>
</a>
<?php
}
}
}
?>
如何将此代码中的mysql查询更改为pdo?