dongshendie8849 2018-10-23 20:49
浏览 411
已采纳

SQL返回错误的行数

I have an SQL script that is supposed to count the number of rows in my table. The script worked fine, and was able to see that I had 25 rows in each of 8 tables. The problem is, when I imported a csv into one of the tables to add more values and then ran the script, it still said I had 25 rows in each table. I then removed ALL of the rows in another table, and the result still came back 25.

I don't have the values manually set anywhere in my script, this is the entire script that is returning all values of 25 despite no matter what I do to the table. I have no idea how to debug this unless it's a problem with phpMyAdmin/MAMP?

<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbName = "database";

$var1 = 't1c';
$var2 = 't1c';
$var3 = 't1c';
$var4 = 't1c';
$var5 = 't2c';
$var6 = 't2c';
$var7 = 't2c';
$var8 = 't2c';

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
error_reporting(E_ALL);
ini_set('display_errors', 1);


$conn = new mysqli ($servername, $username, $password, $dbName);

if(!$conn){
    die("Connection failed. ". mysqli_connect_error());
}

$sql = "SELECT ";
$sql .= "(SELECT COUNT(*) FROM table1) AS $var1, ";
$sql .= "(SELECT COUNT(*) FROM table2) AS $var2, ";
$sql .= "(SELECT COUNT(*) FROM table3) AS $var3, ";
$sql .= "(SELECT COUNT(*) FROM table4) AS $var4, ";
$sql .= "(SELECT COUNT(*) FROM table5) AS $var5, ";
$sql .= "(SELECT COUNT(*) FROM table6) AS $var6, ";
$sql .= "(SELECT COUNT(*) FROM table7) AS $var7, ";
$sql .= "(SELECT COUNT(*) FROM table8) AS $var8; ";

$result = mysqli_query($conn ,$sql);

if(mysqli_num_rows($result) > 0) {

    while($row = mysqli_fetch_assoc($result)){

        echo "var1:" . $row[$var1] . "|var2:" . $row[$var2] . "|var3:" . 
$row[$var3] . "|var4:" . $row[$var4] . "|var5:". $row[$var5] . "|var6:". 
$row[$var6] . "|var7:". $row[$var7] . "|var8:". $row[$var8] . ";";

    }
}
?>

Again, var 1-8 returned the correct data when first running the script, but now the value remains the same each time I run the script. I've tried turning the server off and back on. Any ideas?

I noticed that on phpMyAdmin, it DISPLAYS 25 rows at a time, although I manually can set it to show more rows. Could that have anything to do with why it's returning 25 rows through SQL? I would think not, and it's just a coincidence that I manually had 25 values originally but I'm completely stumped.

  • 写回答

1条回答 默认 最新

  • dongshengyin0147 2018-10-23 20:54
    关注

    You're repeating the same aliases in your variables. $var1 through $var4 are all t1c, and $var5 through $var8 are all t2c. Since array indexes are unique, there's only one $row['t1c'] and $row['t2c'], so you're just counting the rows in table4 and table8. $row[$var1] is the same as $row[$var2] since $var1 == $var2.

    Use different values for all the variables and it should work as you expect.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 spring后端vue前端
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题