我被许多人钉在十字架上,因为他们仍在使用旧的方法' mysql_query()
我也正在启动我的网站,这花了我4个月的时间才完成,我希望能够获得最大的安全保障。
所以我觉得现在可能是摆脱旧习惯并开始使用新方法的最佳时机。
我已经扫描了一些教程,但我不得不承认它与mysql_query()相比似乎相当(非常)复杂
最好的学习方法可能是通过示例,所以我希望有人能够将以下示例更改为PDO和MYSQLI语句,以便我可以看到两者之间的区别。如果可以包含简短的解释,将非常感谢...
您还建议最容易学习的是什么? Mysqli或PDO
$sql="select * from Multiple_Picks where event_id = '$matchId' and
pick='$winner' and
abs(score-$winScore) = (select min(abs(score-$winScore)) from Multiple_Picks
where pick = '$winner' );";
$result = mysql_query($sql) or trigger_error(mysql_error()." in ".$sql);
while($row = mysql_fetch_array($result)){
//if(isset($row['member_nr'])){
$winingMember = $row['member_nr'];
$event = $row['event_id'];
$pick = $row['pick'];
$score = $row['score'];
$tournament=$row['tournament'];
$round=$row['round'];
}
感谢您阅读
答案 0 :(得分:1)
这有助于你
使用mysqli:
$con = mysqli_connect("localhost",$username,$password,$dbname);
$fetch=mysqli_query($con,'select * from Multiple_Picks where event_id = '$matchId' and
pick='$winner' and
abs(score-$winScore) = (select min(abs(score-$winScore)) from Multiple_Picks
where pick = '$winner' );');
while( $row=mysqli_fetch_assoc($fetch))
{
$winingMember = $row['member_nr'];
$event = $row['event_id'];
$pick = $row['pick'];
$score = $row['score'];
$tournament=$row['tournament'];
$round=$row['round'];
}
使用PDO
<?php
/*** mysql hostname ***/
$hostname = 'localhost';
/*** mysql username ***/
$username = 'username';
/*** mysql password ***/
$password = 'password';
try {
$dbh = new PDO("mysql:host=$hostname;dbname=animals", $username, $password);
/*** echo a message saying we have connected ***/
echo 'Connected to database<br />';
/*** The SQL SELECT statement ***/
$sql = "select * from Multiple_Picks where event_id = '$matchId' and
pick='$winner' and
abs(score-$winScore) = (select min(abs(score-$winScore)) from Multiple_Picks
where pick = '$winner' );";
/*** fetch into an PDOStatement object ***/
$stmt = $dbh->query($sql);
/*** echo number of columns ***/
$result = $stmt->fetch(PDO::FETCH_ASSOC);
/*** loop over the object directly ***/
foreach($result as $key=>$val)
{
echo $key.' - '.$val.'<br />';
}
/*** close the database connection ***/
$dbh = null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>