我有一个方法,需要多少项目&从限制开始并返回值
public function comment_recent_comments_block($numbers = 10, $start_from = 1) {
$sql = " SELECT comment.comment_id, comment.subject,
content.content_id, content.content_type_id
FROM comment
LEFT JOIN content ON content.content_id = comment.reference_id
WHERE comment.reference_table = 'content'
AND comment.subject IS NOT NULL AND comment.subject !='' ";
$sql .=" ORDER BY comment.weightage DESC, comment.creation_date DESC ";
$sql .= " LIMIT :lmt_start_from , :lmt_no_of_records ";
......其余代码...... }
我希望PHP考虑10& 1作为默认值但不起作用。我的sql抛出错误,显示'NULL'值的错误。 现在,如果我明确检查参数并设置值,如
$numbers = empty($numbers) ? 10 : $numbers;
$start_from = empty($start_from) ? 1 : $start_from;
在方法中然后它可以工作。
可以解释一下我在第一种方法中缺少的东西。为什么PHP不考虑默认值...我尝试输入字符串'10'& '1',但那也不起作用
由于
---------------------------------------完整代码------ ---------------------------
public function findBySql($sql, $value_a = '') {
global $dbc;
$stmt = $dbc->connection->prepare(" $sql ");
if (!empty($value_a)) {
foreach ($value_a as $key => $value) {
if (!empty($value)) {
if ($key == 'lmt_no_of_records' || $key == 'lmt_start_from') {
$stmt->bindValue(":$key", $value, PDO::PARAM_INT);
} else {
$stmt->bindValue(":$key", $value);
}
} else {
$stmt->bindValue(":$key", NULL);
}
}
}
try {
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_CLASS);
return $result;
} catch (PDOException $e) {
echo "Record couldnt be fetched !!: " . $e->getMessage();
return 0;
}
}
public function comment_recent_comments_block($numbers = 10, $start_from = 1) {
$numbers = empty($numbers) ? 10 : $numbers;
$start_from = empty($start_from) ? 1 : $start_from;
$sql = " SELECT comment.comment_id, comment.subject,
content.content_id, content.content_type_id
FROM comment
LEFT JOIN content ON content.content_id = comment.reference_id
WHERE comment.reference_table = 'content'
AND comment.subject IS NOT NULL AND comment.subject !='' ";
$sql .=" ORDER BY comment.weightage DESC, comment.creation_date DESC ";
$sql .= " LIMIT :lmt_start_from , :lmt_no_of_records ";
$value_a = ['lmt_no_of_records' => $numbers, 'lmt_start_from' => $start_from ];
$result = $this->findBySql($sql, $value_a);
$comment_string = '';
if (count($result) > 0) {
$comment_string .= '<ul class="documentation_list comment">';
foreach ($result as $records) {
$comment_string .= '<li class="comment_subject">';
$comment_string .= '<a href="' . HOME_URL . 'content.php?mode=2&' . 'content_id=' . $records->content_id .
'&content_type_id=' . $records->content_type_id . '#comment_id_' . $records->comment_id . '">';
$comment_string .= $records->subject . '</a>';
$comment_string .= '</li>';
}
$comment_string .= '</ul>';
}
return $comment_string;
}
现在这个工作但如果我删除下面两个语句 $ numbers =空($ numbers)? 10:$ number; $ start_from = empty($ start_from)? 1:$ start_from; 它不会工作
答案 0 :(得分:0)
您发布的代码中没有任何特定的PDO,我认为问题与此无关(但可能)。(
有可能)
请尝试类似
<?php
class Foo {
public function comment_recent_comments_block($numbers = 10, $start_from = 1) {
// put the following three lines of code at the beginning of your method comment_recent_comments_block
var_export(debug_backtrace(0, 1));
var_export( array($numbers, $start_from) );
die;
}
}
$foo = new Foo;
$foo->comment_recent_comments_block();
我的输出是
array (
0 =>
array (
'file' => 'something.php',
'line' => 11,
'function' => 'comment_recent_comments_block',
'class' => 'Foo',
'type' => '->',
'args' =>
array (
),
),
)array (
0 => 10,
1 => 1,
)
表示该方法实际上是在没有参数的情况下调用的,并且相应的默认值已分配给$ numbers和$ start_from。
你的输出是什么?