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.
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.