1
I want to read lines from an SQL and use them to create objects and finally put these objects in an array. But at the end my methods (from the Object class) do not print the object name.
The code to read only one line works, so the my problem certainly is in the array.
<?php
require_once 'Object.php';
require("connect.inc");
connect_db() or die ("Error.");
$result=mysql_query("SELECT * FROM object") or die ("Error");
$count = 0;
$c[] = array();
while ($line=mysql_fetch_array($result) && $count < 6) {
$count ++;
$c[$count] = new Object($line);
}
print ($c[1]->getName()); // Example
?>
Hi Bernardo, can you translate the question to English?
– Sergio
$Count should be $cont or vice versa. And snap the brackets from the $c statement.
– bfavaretto
sorry, actually this was a transcription error, both are equal
– Bernardo Araujo
The error is in if. It is
$cont < 6
, when, I believe, I should be$count < 6
– user28595
sorry, as I translated the code to post, I ended up missing this variable at the time of transcription, but all "Count" are equal in the code
– Bernardo Araujo
What is the use of this counter? When there are no more lines, the loop will be finished, there is no need for counter.
– user28595
I’m not familiar with PHP, I thought I needed one like in Java. Alias, this inside the vector, but it is used pq I want only the first 6 rows of the table.
– Bernardo Araujo
What is this Object class? You created it?
– user28595