我在哪里犯了一个错误以获得我在下面写的输出?
假设在这些表中有 0 行,所以任何地方都应该是 0 作为输出。
<?php
$g_module =
array(
'm_b_broadcast_live',
'm_b_browsing_live',
'm_e_askfm_likes_live',
'm_e_facebook_followers_live',
'm_e_facebook_group_joins_live',
);
for ($i = 0; $i <= 5; $i++) {
$modules_names = "g_module[$i]";
$modules_from = '$'.$modules_names;
$modules_rows = '$g_module_row_'.$i;
$$modules_rows = mysql_num_rows("SELECT * FROM $$modules_from");
}
echo $g_module_row_1;
echo '</br>';
echo $g_module_row_2;
echo '</br>';
echo $g_module_row_3;
echo '</br>';
echo $g_module_row_4;
echo '</br>';
echo $g_module_row_5;
/* output should be:
0
0
0
0
0
*/
?>
正如您的问题的评论中所述,mysql_num_rows()
将查询结果作为参数,而不是查询字符串。
此外,您应该使用mysqli
! 看看这里的文档。
您可以简化的另一件事是逃避美元符号,这样您就不必$modules_names
再次连接一个。
更正后的代码如下所示:
<?php
$g_module = [
'm_b_broadcast_live',
'm_b_browsing_live',
'm_e_askfm_likes_live',
'm_e_facebook_followers_live',
'm_e_facebook_group_joins_live',
];
// you have to create a connection to your database server first
// of course you will have to swap out my placeholders for the actual credentials
$con=mysqli_connect('mysql_server_address_here','username_here','password_here','database_name_here');
for($i=0;$i<=5;$i++){
$modules_from = "\$g_module[$i]";
$modules_rows = "\$g_module_row_$i";
$q=mysqli_query("SELECT * FROM $$modules_from");
//use mysqli_num_rows instead
$$modules_rows = mysqli_num_rows($q);
}
// close your connection after you are finished
mysqli_close($con);
echo $g_module_row_1;
echo '</br>';
echo $g_module_row_2;
echo '</br>';
echo $g_module_row_3;
echo '</br>';
echo $g_module_row_4;
echo '</br>';
echo $g_module_row_5;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句