how i can make a array of values from the field of db table

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
djdon11
Forum Commoner
Posts: 90
Joined: Wed Jun 20, 2007 5:03 pm

how i can make a array of values from the field of db table

Post by djdon11 »

Hi Friends ...

i have a while loop running on

Code: Select all

$sql="select * from download where order_id='".$custom."'";
$res=mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_array[$res]){
$me = $row['indication'];
}
now i want all the values of field "indication" in a single variable $me but out of the while loop ..

means i want an array which holds every value of field indication ..

can anybody help me for this ....
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

Code: Select all

$sql="select * from download where order_id='".$custom."'";
$res=mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_array[$res]){
$me[] = $row['indication'];
}
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

VladSun wrote:

Code: Select all

$sql="select * from download where order_id='".$custom."'";
$res=mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_array[$res]){
$me[] = $row['indication'];
}
Don't forget to initialize $me as an array prior to running the loop ;)
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

Yes, this is good practice, but ...

Code: Select all

<?
$me[] = 1;
$me[] = 2;
$me[] = 3;

print_r($me);
?>
ouput:

Array ( [0] => 1 [1] => 2 [2] => 3 )
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Right, but since you are assigning values inside of a loop, if the loop doesn't evaluate, the array will not exist at all, giving you undefined variable warnings. It doesn't take that much more to code cleanly, like using long PHP opening tags and declaring variables:

Code: Select all

<?php
$me = array();
while($row = mysql_fetch_array[$res]) {
  // So even if this is totally empty, $me still exists outside of the loop scope
  $me[] = $row['indication'];
}

var_dump($me);
?>
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

OK, OK ! You are right, my mistake :)

I know my person to write *very* clean code. I always use long PHP opening tags (at postedit stage ;) ). I always use:

error_reporting(E_ALL);

use warnings;
use strict;

etc.

Honestly, I started with $me = new Arrray(); ... but I was not sure (don't laugh) whether it is the right way to declare it. So ... I dropped this line.

I am working at the moment on a project which includes javascript, Perl, PHP, Asterisk diaplans ... too many and different syntax schemes.

Please ... forgive me ;)
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

VladSun wrote:Please ... forgive me ;)
You are forgiven, sir.
djdon11
Forum Commoner
Posts: 90
Joined: Wed Jun 20, 2007 5:03 pm

how to make an array

Post by djdon11 »

hello friends ...

thanks to all of you for your precious help .. it is working for me ...


thanks :)
Post Reply