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.

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

报告相同问题?

悬赏问题

  • ¥15 maixpy训练模型,模型训练好了以后,开发板通电会报错,不知道是什么问题
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容