V Posted July 21, 2010 Share Posted July 21, 2010 I making a function that queries DB table items and I would like to also show the total rows if a variable totalRows is set or "on". This is what I came up with.. //above is the $connection variable for DB connection function anyQuery($connection, $table, $column, $totalRows) { $sql = "SELECT * FROM $table ORDER BY post_date DESC"; $result = $connection->query($sql) or die(mysqli_error($connection)); //find out how many records were retrieved $numRows = $result->num_rows; while ($row = $result->fetch_assoc()) { echo strtoupper("<li>{$row["$column"]}</li>"); } if($totalRows == "on") { echo $totalRows; } } It echoes the table items fine but echoes "on" instead of the rows number. I tried the function like this anyQuery($connection, "posts", "post_title", "on"); I even tried setting the variable to "on" before calling the function and still no go. Does someone have a better idea? :-\ Quote Link to comment Share on other sites More sharing options...
Mchl Posted July 21, 2010 Share Posted July 21, 2010 $numRows = $result->num_rows; ... echo $totalRows; Quote Link to comment Share on other sites More sharing options...
gwolgamott Posted July 21, 2010 Share Posted July 21, 2010 Tried something like this yet? $count_my_row = 0; while ($row = $result->fetch_assoc()) { $count_my_row = $count_my_row + 1; echo strtoupper("<li>{$row["$column"]}</li>"); } if($totalRows == "on") { echo $count_my_row; } } Quote Link to comment Share on other sites More sharing options...
V Posted July 21, 2010 Author Share Posted July 21, 2010 Mchl I'm sorry, I was re-writing the code before posting. I changed numRows to totalRows but I still get no total row number gwolgamott, thanks for the suggestion! I use a mysql num_rows function to get the same result. It works fine when I remove the "if" statement from the function. Quote Link to comment Share on other sites More sharing options...
gwolgamott Posted July 21, 2010 Share Posted July 21, 2010 Oh I see, you were trying to echo numRows and not totalRows... You meant this: while ($row = $result->fetch_assoc()) { echo strtoupper("<li>{$row["$column"]}</li>"); } if($totalRows == "on") { echo $numlRows; //that explains why you were gettting "on" all the time then, but not why the if doesn't work } } And that if statement doesn't work at all? I've a suspicion that totalRows is not getting set correctly. //echo the totalRow here just to be sure it is being sent to the function with something you believe it should be function anyQuery($connection, $table, $column, $totalRows) { echo $totalRows; Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.