转换成的mysqli - mysql_result - 预计参数2是整数
我已阅读所有关于转换到mysqli的问题,但不能把这段代码转换:转换成的mysqli - mysql_result - 预计参数2是整数
// Select how many news should be displayed
if ($u_rank >= 1) {
$sql = mysqli_query($connect_db, "SELECT news_d FROM users_data WHERE uid = '$id'");
$news_d = mysql_result($sql, news_d);
} else {
$news_d = 10;
};
如果我自定义功能尝试:
function mysqli_result($res, $row, $field=0) {
$res->data_seek($row);
$datarow = $res->fetch_array();
return $datarow[$field];
}
并尝试在我的代码使用方法:
$news_d = mysqli_result($sql, news_d);
我得到这个错误: 警告:mysqli_data_seek()预计参数2是整数,字符串给出
帮助,将不胜感激,
最佳,MISKO
如果你真正的代码是一样的,你在这个问题发布再有就是在该行一个明显的问题之一:
$news_d = mysqli_result($sql, news_d);
news_d
不$news_d
。它看起来像一个常量(因为它不以$
开头)并且它被解释为常量。由于没有名为news_d
的常量,解释器会将其转换为字符串。它是一样的:
$news_d = mysqli_result($sql, 'news_d');
的行为在constants PHP文档页解释:
If you use an undefined constant, PHP assumes that you mean the name of the constant itself, just as if you called it as a string (
CONSTANT
vs."CONSTANT"
). An error of levelE_NOTICE
will be issued when this happens. See also the manual entry on why$foo[bar]
is wrong (unless you firstdefine()
bar as a constant).
由于mysqli_result()
函数传递其第二个参数mysqli::data_seek()
($res->data_seek($row);
),它必须是一个数字,而不是一个字符串。不幸的是,您发布的代码的目的根本不明确。只有你知道你应该把什么号码作为第一个参数传递给mysqli_result()
以及为什么。
你传递news_d
这是无效的。这应该是一个整数。尝试通过0
代替,就像这样:
$news_d = mysqli_result($sql, 0);
Grrr ...整天搜索:( – MiskoPeterka
)您也可以传递'$ news_d' - 但是我看不到您所生成的代码块上面引用的内容:( – steadweb
你犯了一个错字,忘记了'news_d'变量前面的'$'。 – Quentin