Array() HELP!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nishmgopal
Forum Contributor
Posts: 101
Joined: Tue Mar 03, 2009 9:38 am

Array() HELP!

Post 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>";
}
 
 
?>
mickeyunderscore
Forum Contributor
Posts: 129
Joined: Sat Jan 31, 2009 9:00 am
Location: UK

Re: Array() HELP!

Post 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 />";
}
?>
nishmgopal
Forum Contributor
Posts: 101
Joined: Tue Mar 03, 2009 9:38 am

Re: Array() HELP!

Post by nishmgopal »

hey thanks for that. What do i change in that code to display all the records?
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Array() HELP!

Post 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.
BomBas
Forum Commoner
Posts: 41
Joined: Wed Mar 04, 2009 1:04 pm

Re: Array() HELP!

Post 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.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Array() HELP!

Post 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.
nishmgopal
Forum Contributor
Posts: 101
Joined: Tue Mar 03, 2009 9:38 am

Re: Array() HELP!

Post 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
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Array() HELP!

Post 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.
BomBas
Forum Commoner
Posts: 41
Joined: Wed Mar 04, 2009 1:04 pm

Re: Array() HELP!

Post 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 :)
Post Reply