Page 1 of 1
Array() HELP!
Posted: Wed Mar 04, 2009 11:32 am
by nishmgopal
Hi
Below is my code, what I am trying to do is display all the records in my table, but nothing is being printed!
please help
Code: Select all
$array= array();
$query = "SELECT * FROM Persons WHERE Name='Nish'";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
[color=#BFFF00]WHAT GOES HERE?[/color]
}
for($i=0;$i<=count($array);$i++) {
print "$array[i]<br>";
}
?>
Re: Array() HELP!
Posted: Wed Mar 04, 2009 1:53 pm
by mickeyunderscore
It's close to working. If you expect more than one row though, then you need to use a 3D array, otherwise you will end up with only the last row.
Code: Select all
<?php
$array = array();
$query = "SELECT * FROM Persons WHERE Name='Nish'";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
$array[] = $row;
}
for($i=0, $c=count($array);$i<$c;$i++) {
print "{$array[$i]['Name']}<br />";
}
?>
Re: Array() HELP!
Posted: Wed Mar 04, 2009 2:12 pm
by nishmgopal
hey thanks for that. What do i change in that code to display all the records?
Re: Array() HELP!
Posted: Thu Mar 05, 2009 1:50 am
by califdon
I'm not sure if you're trying to do something special, but if you're not, the code you posted seems to me to be a very awkward way to display all the records in a table. The simplest way is simply:
Code: Select all
mysql_connect('myhostname','mydbname','mypassword') or die(mysql_error());
mysql_select_db('mydbname') or die(mysql_error());
$sql="SELECT * FROM mytablename";
$resource=mysql_query($sql) or die(mysql_error());
while($row=mysql_fetch_assoc($resource)) {
extract($row);
foreach($row as $fld=>$value) {
echo "$fld : $value<br />";
}
echo "<br />";
}
You don't need to create another array. $row is an array that is built into the mysql_fetch_* function. By using mysql_fetch_assoc, instead of one of the other variations, and using extract(), you will get pairs of field names and values for each row.
Re: Array() HELP!
Posted: Thu Mar 05, 2009 5:33 am
by BomBas
@ Califdon - the foreach isn't necessary.
This code would be more effective:
Code: Select all
mysql_connect( 'myhostname','mydbname','mypassword' ) or die(mysql_error());
mysql_select_db( 'mydbname' ) or die(mysql_error());
$query = "SELECT * FROM mytablename";
$resource = mysql_query( $sql ) or die(mysql_error());
while( $row = mysql_fetch_object($resource) )
{
echo $row->name. ":". $row->description. "<br />";
}
Of course change columns name to your own.
Re: Array() HELP!
Posted: Thu Mar 05, 2009 12:18 pm
by califdon
BomBas wrote:@ Califdon - the foreach isn't necessary.
This code would be more effective:
Code: Select all
mysql_connect( 'myhostname','mydbname','mypassword' ) or die(mysql_error());
mysql_select_db( 'mydbname' ) or die(mysql_error());
$query = "SELECT * FROM mytablename";
$resource = mysql_query( $sql ) or die(mysql_error());
while( $row = mysql_fetch_object($resource) )
{
echo $row->name. ":". $row->description. "<br />";
}
Of course change columns name to your own.
Your code works IF you know the column names, of course. What I showed is a generic method that doesn't require that you know the column names, thus it can be used in all cases, merely substituting the table name.
Re: Array() HELP!
Posted: Thu Mar 05, 2009 12:35 pm
by nishmgopal
Thanks alot for your help guys! I managed to extract all my data, but now how can I display this data in a html table? do i have to define each row?
thank you in advance
Re: Array() HELP!
Posted: Thu Mar 05, 2009 1:33 pm
by califdon
nishmgopal wrote:Thanks alot for your help guys! I managed to extract all my data, but now how can I display this data in a html table? do i have to define each row?
thank you in advance
Yes.
Re: Array() HELP!
Posted: Thu Mar 05, 2009 2:08 pm
by BomBas
califdon wrote:BomBas wrote:@ Califdon - the foreach isn't necessary.
This code would be more effective:
Code: Select all
mysql_connect( 'myhostname','mydbname','mypassword' ) or die(mysql_error());
mysql_select_db( 'mydbname' ) or die(mysql_error());
$query = "SELECT * FROM mytablename";
$resource = mysql_query( $sql ) or die(mysql_error());
while( $row = mysql_fetch_object($resource) )
{
echo $row->name. ":". $row->description. "<br />";
}
Of course change columns name to your own.
Your code works IF you know the column names, of course. What I showed is a generic method that doesn't require that you know the column names, thus it can be used in all cases, merely substituting the table name.
Yeah you right.. I guess you're the smart boy here
