转换为 mysql_connect 到 PDO
Posted
技术标签:
【中文标题】转换为 mysql_connect 到 PDO【英文标题】:Convert to mysql_connect to PDO 【发布时间】:2017-01-26 19:15:27 【问题描述】:您好,我可以使用此代码,但我需要将代码转换为 PDO。 我尝试了很多变化,但没有奏效。 我有 $conn 作为连接。 提前致谢。
$projectrfpid = $_GET['projectrfpid'];
$allRecords = mysql_query('SELECT * FROM projectscostbreakdown WHERE projectscostbreakdown_projectid='.$projectrfpid.' AND projectscostbreakdown_deleted=1 ORDER BY projectscostbreakdown_areaname, projectscostbreakdown_order ASC');
if(is_resource($allRecords))
while($row = mysql_fetch_assoc($allRecords))
?>
<tr id="<?php echo $row['projectscostbreakdown_id']; ?>" >
<td ><img src="../test/images/icon-drag.png" style="cursor:pointer;"/></td>
<td><b><?php echo $row['projectscostbreakdown_areaname']; ?>:</b> <?php echo $row['projectscostbreakdown_itemname']; ?><br /><?php echo nl2br($row['projectscostbreakdown_itemdescription']); ?></td>
</tr>
<?php
?>
【问题讨论】:
您应该向我们展示您的尝试以及您遇到的问题。 *** 不是一个要求别人为你工作的网站。 抱歉,我试了很多次,一旦失效就删除了代码。所以我无法记录我尝试过的内容。很快就会更新。谢谢。 【参考方案1】:I am writing a general format.
Fill up the below fields:
$dbhost = "";
$dbname = "";
$dbuser = "";
$dbpass = "";
//make the connection
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);
$sql="SELECT * FROM projectscostbreakdown WHERE projectscostbreakdown_projectid=:id AND projectscostbreakdown_deleted=1 ORDER BY projectscostbreakdown_areaname, projectscostbreakdown_order ASC";
//always use parameterized queries
$query=$conn->prepare("$sql");
//Bind parameter values using PDO's bindValue method
$query->bindValue(':id', $projectrfpid);
$query->execute();
$results = $query->fetchAll(PDO::FETCH_ASSOC);
Look up the PDO section in the php manual to learn more.
【讨论】:
【参考方案2】:做这个改变..
$con = mysqli_connect(DB_SERVER,DB_USER,DB_PASSWORD,DB_NAME);
$sql ="SELECT * FROM projectscostbreakdown WHERE projectscostbreakdown_projectid='.$projectrfpid.' AND projectscostbreakdown_deleted=1 ORDER BY projectscostbreakdown_areaname, projectscostbreakdown_order ASC";
$allRecords = mysqli_query($con, $sql);
while ($row = mysqli_fetch_array($allRecords , MYSQLI_ASSOC))
?>
<tr id="<?php echo $row['projectscostbreakdown_id']; ?>" >
<td ><img src="../test/images/icon-drag.png" style="cursor:pointer;"/></td>
<td><b><?php echo $row['projectscostbreakdown_areaname']; ?>:</b> <?php echo $row['projectscostbreakdown_itemname']; ?><br /><?php echo nl2br($row['projectscostbreakdown_itemdescription']); ?></td>
</tr>
<?php
【讨论】:
以上是关于转换为 mysql_connect 到 PDO的主要内容,如果未能解决你的问题,请参考以下文章