【发布时间】:2016-01-17 05:48:03
【问题描述】:
我有一个脚本来管理我的表并包含获取列表或通过 id 获取某些数据的功能。 现在我的一个函数看起来像这样
//Returns a Series Object matching the given series id
public static function getById( $WHERE, $id ){
$conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
$sql = "SELECT * FROM series $WHERE $id";
$st = $conn->prepare( $sql );
$st->execute();
$row = $st->fetch();
$conn = null;
if( $row ) return new Series( $row );
}
但我希望它是这样的
//Returns a Series Object matching the given series id
public static function getById( $statement ){
$conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
$sql = "SELECT * FROM series $statement";
$st = $conn->prepare( $sql );
$st->execute();
$row = $st->fetch();
$conn = null;
if( $row ) return new Series( $row );
}
所以不必这样做
$series = Series::getById( ( string ) "WHERE id=", (int) $_POST['seriesId'] );
我能做到
$series = Series::getById( ( string ) "WHERE id=$_POST['seriesId']" );
【问题讨论】:
-
你应该使用准备好的语句。
标签: php mysql function variables