MySQL says my 'Array' is not an array !? Please help

Alles, was PHP betrifft, kann hier besprochen werden.

MySQL says my 'Array' is not an array !? Please help

Postby sugafairie » 12. May 2019 14:06

I produced these 3 lines of code and expected it to have created an array with 4 columns and 16 rows

$conn = mysqli_connect('localhost','root','','sharediary';

$sql = "SELECT `tax_year`, `payment_number`, `company_name`, SUM(dividend_payable) FROM `shareholding` GROUP BY company_name, tax_year, payment_number WITH ROLLUP";

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


And indeed I can display the resuts in a browser using

while($row = mysqli_fetch_array($records))
echo "<tr>";
echo "<td>".$row['tax_year']."</td>";
echo "<td>".$row['payment_number']."</td>";
echo "<td>".$row['company_name']."</td>";
echo "<td>".$row['SUM(dividend_payable)']."</td>";
echo "</tr>";
Et Cetera

within an html table


But then I needed to do some tweaking of the layout but this relied on $record being an array,
but it appears that MySQL does not think it is an array !?!? Here is the story...


I googled around and found 4 methods to display the contents of an array
the first 3 viz. print_r, a user defined function based on print_r and vardump
told me that there are 4 columns and 16 rows which would be true,
but they did not display the contents of my supposed array.
When I tried a 4th method viz. a for loop thus,

for ($i=0;$i<count($records);$i++) {echo $records($i);}

But this time MySQL coughed up an error message ...

Warning: count(): Parameter must be an array or an object that implements Countable in C:\xampp\htdocs\showonscreen2.php on line 76

So I tried this as a kind of debugging tool...
if (!is_array($records)) {echo "No, I'm not an array";}
and it did indeed echo to screen the bad news "No, I'm not an array"
sugafairie
 
Posts: 14
Joined: 16. March 2019 11:51
XAMPP version: 3.2.2
Operating System: Windows 8.1

Re: MySQL says my 'Array' is not an array !? Please help

Postby Nobbie » 12. May 2019 17:37

sugafairie wrote:for ($i=0;$i<count($records);$i++) {echo $records($i);}

But this time MySQL coughed up an error message ...

Warning: count(): Parameter must be an array or an object that implements Countable in C:\xampp\htdocs\showonscreen2.php on line 76


a) its NOT MySQL coughing, its PHP. PHP tells you, that are not using an array where an array is expected.

b) and PHP is totally right, $records is NOT an array, its a so called "handle" and there is NO WAY to extract data from $records different than using the given functions like mysqli_fetch_array() and similar.

I am very sorry for you, but you really have a HUGE lack of basic PHP knowledge and this forum is NOT MEANT to teach PHP and/or MySQL. Therefore, this was my very last statement here. Take your time, read tutorials, read books, read documentation and learn PHP. You cannot learn it from one day to another. Good luck!
Nobbie
 
Posts: 10879
Joined: 09. March 2008 13:04


Return to PHP

Who is online

Users browsing this forum: No registered users and 2 guests

cron