Author

Topic: PHP array question (Read 1453 times)

hero member
Activity: 532
Merit: 500
February 25, 2012, 08:49:13 AM
#9
Im contemplating sending you 5BTC for this extremely useful post Smiley
Give Django a try and once you get comfortable with it you'll realize how shitty php is in general..
hero member
Activity: 576
Merit: 514
February 25, 2012, 08:14:18 AM
#8
Something that should be pointed out: using * in queries is generally a bad idea. Fetch the columns you need, not everything.
(Yeah I know I've been lazy in my examples too)
full member
Activity: 196
Merit: 100
February 25, 2012, 08:09:32 AM
#7
Well, if so:
Quote
I want to fetch this from mysql, put in an array and then loop through all miners, in this example just to display the ip and names.

Code:
  $query = "SELECT * FROM miners";
    $result = mysql_query($query) or die(mysql_error());  //fetch from mysql
    $miner=array();
    while ($r = mysql_fetch_assoc($result)) {
        $miner[]=$r;      //put in array

    }
 
    foreach ($miner as $d) {     //loop though all miners
        echo "miner :  ". $d["ip"] ." ". $d["name"] .$d["whateverotherfield"]."
"; //output miner from array element
    }

well, if you 'nt need to put all fetched rows in array, you can do smth like that:

Code:
  $query = "SELECT * FROM miners";
    $result = mysql_query($query) or die(mysql_error());  //fetch from mysql
    $miner=array();
    while ($r = mysql_fetch_assoc($result)) {
        echo "miner :  ". $d["ip"] ." ". $d["name"] .$d["whateverotherfield"]."
";   //output miner from row

    }

no such kludge, by the way.
hero member
Activity: 576
Merit: 514
February 25, 2012, 07:39:12 AM
#6
You could always write a function if you need this more than once.

Code:
function mysql_fetch_all($q) {
$r=array();
while ($tmp=mysql_fetch_array($q, MYSQL_ASSOC)) { array_push($r, $tmp); }
return($r);
}

function mysql_fetch_all_query($q) {
$q=mysql_query($q);
$r=array();
while ($tmp=mysql_fetch_array($q, MYSQL_ASSOC)) { array_push($r, $tmp); }
return($r);
}

$id=mysql_query('SELECT * FROM miners');
$tmp=mysql_fetch_all($id);
print_r($tmp);

// or

$tmp=mysql_fetch_all_query('SELECT * FROM miners');
print_r($tmp);

Of course this could be made prettier, but bascially it's working.
hero member
Activity: 518
Merit: 500
February 25, 2012, 07:12:22 AM
#5
Why don't you use the returned arrays while fetching them from Mysql?

TBH, because my code is already working with arrays that are initialized on startup, so hardcoded.
Now I just want to fetch the data from a db without rewriting everything.
Looks like I may have to.



hero member
Activity: 576
Merit: 514
February 25, 2012, 06:44:15 AM
#4
Why don't you use the returned arrays while fetching them from Mysql?

Code:
$id=mysql_query('SELECT * FROM miners');
for ($i=0; $i {
$r=mysql_fetch_array($id, MYSQL_ASSOC);
echo $r['name']."
\n";
}

Or use PDO, it has a fetchAll statement (and prepare/execute is nicer too)
hero member
Activity: 518
Merit: 500
February 25, 2012, 06:27:58 AM
#3
Im contemplating sending you 5BTC for this extremely useful post Smiley
hero member
Activity: 532
Merit: 500
February 25, 2012, 06:21:12 AM
#2
Don't use PHP.
hero member
Activity: 518
Merit: 500
February 25, 2012, 05:32:34 AM
#1
Im struggling with PHP to get data from a mysql db and put it in to PHP arrays in a way thats actually easy to use.

Assume I have a table "miners'" with the following fields:

ip (also primary key)
name
status
location
...

I want to fetch this from mysql, put in an array and then loop through all miners, in this example just to display the ip and names.

The best Ive come up with is this:

Code:
  $query = "SELECT * FROM miners";
    $result = mysql_query($query) or die(mysql_error());
    $i=0;
    while ($r = mysql_fetch_assoc($result)) {
        $miner[$i]=$r;
        $i++;
    }
 
    foreach ($miner as $i=>$d) {
        echo "miner :  ". $d["ip"] ." ". $d["name"] .$d["whateverotherfield"];
    }

I does work, but its such a kludge.
Im sure there is a better way, but I just dont see it.

Any help appreciated.

   
     
Jump to: