我在通过 PDO(",,,") 存储数据时没有遇到问题,但在使用以下代码段接收/输出数据时遇到错误。
public static function getList( $numRows=1000000, $order="publicationDate DESC" ) {
$conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
$sql = "SELECT SQL_CALC_FOUND_ROWS *, UNIX_TIMESTAMP(publicationDate) AS publicationDate FROM articles
ORDER BY " . mysql_escape_string($order) . " LIMIT :numRows";
$st = $conn->prepare( $sql );
$st->bindValue( ":numRows", $numRows, PDO::PARAM_INT );
$st->execute();
$list = array();
while ( $row = $st->fetch() ) {
$article = new Article( $row );
$list[] = $article;
}
// Now get the total number of articles that matched the criteria
$sql = "SELECT FOUND_ROWS() AS totalRows";
$totalRows = $conn->query( $sql )->fetch();
$conn = null;
return ( array ( "results" => $list, "totalRows" => $totalRows[0] ));
}
我在使用 mysql_escape_string()
时收到以下错误 -
Deprecated: mysql_escape_string(): This function is deprecated; use mysql_real_escape_string() instead. in C:\document.php on line 41
当我将其更改为 mysql_real_escape_string()
时,我收到以下错误 -
Deprecated: mysql_real_escape_string(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in C:\document.php on line 41
Warning: mysql_real_escape_string(): Access denied for user ''@'localhost' (using password: NO) in C:\document.php on line 41
Warning: mysql_real_escape_string(): A link to the server could not be established in C:\document.php on line 41
最佳答案
你正在使用 PDO,所以不要使用任何属于过时的 mysql_query
库的部分,包括 mysql_real_escape_string
。
您不能随便注入(inject)任意ORDER BY
条件,在这里您必须非常小心。如果它是您要排序的列,请通过已知良好列的白名单运行它并插入该文本仅。您不希望人们提供自己的任意列进行排序,尤其是当他们不在数据库中时。
请记住,转义列名使用的方法与转义 MySQL 数据字符串的方法完全不同。
关于php - 我将如何解析这段 PHP 以允许通过 PHP 从 MySQL 接收/输出数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31274386/