如下所示,我在获取输出时犯了错误??
假设在这些表中是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
*/
?>
发布于 2018-07-28 04:03:17
正如您的问题的注释中所述,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;
发布于 2018-07-28 03:56:12
<?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',
);
foreach($g_module as $table_name){
$count = mysql_num_rows("SELECT * FROM $table_name");
echo "<br/>".$count;
}
?>
我认为你应该试着这样做。
https://stackoverflow.com/questions/51564153
复制相似问题