Page 1 of 1

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

Posted: Thu Aug 02, 2007 6:10 pm
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 ....

Posted: Thu Aug 02, 2007 6:13 pm
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'];
}

Posted: Thu Aug 02, 2007 6:14 pm
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 ;)

Posted: Thu Aug 02, 2007 6:18 pm
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 )

Posted: Thu Aug 02, 2007 6:31 pm
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);
?>

Posted: Thu Aug 02, 2007 6:45 pm
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 ;)

Posted: Thu Aug 02, 2007 7:10 pm
by superdezign
VladSun wrote:Please ... forgive me ;)
You are forgiven, sir.

how to make an array

Posted: Thu Aug 02, 2007 7:28 pm
by djdon11
hello friends ...

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


thanks :)