I am having table with two columns Symbol and weight. I am using this code
$table=mysql_fetch_array($result);
echo count($table);
echo "<pre>";
print_r($table);
echo "</pre>";
Out of the Above Code is
4
Array
(
[0] => x1
[symbol] => x1
[1] => 0.50
[weight] => 0.50
)
My table contains 4 entries. But it is showing only one. i want to see all the entries, can you tell how to access them manually. I know i can access them using.
while($table=mysql_fetch_array($result)
{
echo "<br/>".$table['symbol']."\t".$table['weight'];
}
But I want to do this manually. Because later i want to perform some manual operation on this array.
Content of my Table is
Symbol | Weight
X1 | .50
X2 | .25
X3 | .20
X4 | .10
By manually i mean, i will fetch the last two record without traversing the whole array(i.e with the help of while) then add the weight of these last two records.
For Example : Here my table have 4 entries and last two records are X3 and X4 with weight .20 and .10. I will replace these two records with the new single records that is X33 with weight .30. and my new table will look like
Symbol | Weight
X1 | .50
X2 | .25
X33| .30
You have two options, throw in a WHERE
clause or do a loop until you get all the data and store it in your own array.
while($table=mysql_fetch_array($result)
{
$data[]=$table;
}
With your array, you can then access it manually however you would like.
The best option is to use mysqli
or PDO
since mysql
is deprecated and will be removed in the future. I know mysqli result object has the fetch_all
function to get an array of objects.