2014-10-28 35 views

回答

0

使用PDO结果:

$query = $db->query("SELECT `prs_amtdb` FROM prs WHERE `prs_amtcrck` = 0"); 
$results = $query->fetchAll(); 

foreach($results as $result) { 
    echo $result; 
} 

http://php.net/manual/en/pdo.query.php

如果您有查询中使用的用户输入信息,则应始终使用prepared statements例如:

$query = $db->prepare("SELECT `prs_amtdb` FROM prs WHERE `prs_amtcrck` = :atmcrck"); 
$query->bindParam(':atmcrck', 0); // 0 will be the user input 
$query->execute();  
$results = $query->fetchAll(); 

foreach($results as $result) { 
    echo $result; 
} 

确保在PDO有一个数据库连接设置:

try { 
    $dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass); 
} catch (PDOException $e) { 
    die($e->getMessage()); 
} 

http://php.net/manual/en/pdo.connections.php

+1

我很欣赏没有必要对固定值使用参数化,但值得指出的是,如果零来自用户输入,参数化从安全角度来看是个好主意。 – halfer 2014-10-28 09:14:26

0

使用的mysqli

注:确保你绑定你的价值。 mysqli不会自动 保护您的查询 $ connection = mysqli_connect($ host,$ user,$ password,$ database);

$query="SELECT prs_amtdb FROM prs WHERE prs_amtcrck = 0"; 

$result= mysqli_query($connection, $query);//$connection is your database 

//connection 

//fetch the result 

while($row= mysqli_fetch_array($result)){ 
     echo $row['column_name'].'<br/>'; 
}