Bitcoin Forum
May 07, 2024, 12:47:48 AM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: PHP array question  (Read 1426 times)
P4man (OP)
Hero Member
*****
Offline Offline

Activity: 518
Merit: 500



View Profile
February 25, 2012, 10: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.

   
     

1715042868
Hero Member
*
Offline Offline

Posts: 1715042868

View Profile Personal Message (Offline)

Ignore
1715042868
Reply with quote  #2

1715042868
Report to moderator
1715042868
Hero Member
*
Offline Offline

Posts: 1715042868

View Profile Personal Message (Offline)

Ignore
1715042868
Reply with quote  #2

1715042868
Report to moderator
1715042868
Hero Member
*
Offline Offline

Posts: 1715042868

View Profile Personal Message (Offline)

Ignore
1715042868
Reply with quote  #2

1715042868
Report to moderator
It is a common myth that Bitcoin is ruled by a majority of miners. This is not true. Bitcoin miners "vote" on the ordering of transactions, but that's all they do. They can't vote to change the network rules.
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
legitnick
Hero Member
*****
Offline Offline

Activity: 532
Merit: 500



View Profile WWW
February 25, 2012, 11:21:12 AM
 #2

Don't use PHP.

5 BITCOIN RAFFLE GIVEAWAY
"I dont lift" - Lord Furrycoat
P4man (OP)
Hero Member
*****
Offline Offline

Activity: 518
Merit: 500



View Profile
February 25, 2012, 11:27:58 AM
 #3

Im contemplating sending you 5BTC for this extremely useful post Smiley

Bitsky
Hero Member
*****
Offline Offline

Activity: 576
Merit: 514


View Profile
February 25, 2012, 11: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<mysql_num_rows($id); $i++)
{
$r=mysql_fetch_array($id, MYSQL_ASSOC);
echo $r['name']."<br />\n";
}

Or use PDO, it has a fetchAll statement (and prepare/execute is nicer too)

Bounty: Earn up to 68.7 BTC
Like my post? Feel free to drop a tip to 1BitskyZbfR4irjyXDaGAM2wYKQknwX36Y
P4man (OP)
Hero Member
*****
Offline Offline

Activity: 518
Merit: 500



View Profile
February 25, 2012, 12:12:22 PM
 #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.




Bitsky
Hero Member
*****
Offline Offline

Activity: 576
Merit: 514


View Profile
February 25, 2012, 12:39:12 PM
Last edit: February 25, 2012, 01:15:11 PM by Bitsky
 #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.

Bounty: Earn up to 68.7 BTC
Like my post? Feel free to drop a tip to 1BitskyZbfR4irjyXDaGAM2wYKQknwX36Y
neo_rage
Full Member
***
Offline Offline

Activity: 196
Merit: 100



View Profile
February 25, 2012, 01:09:32 PM
 #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"]."<BR>"; //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"]."<BR>";   //output miner from row

    }

no such kludge, by the way.

Bitsky
Hero Member
*****
Offline Offline

Activity: 576
Merit: 514


View Profile
February 25, 2012, 01:14:18 PM
 #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)

Bounty: Earn up to 68.7 BTC
Like my post? Feel free to drop a tip to 1BitskyZbfR4irjyXDaGAM2wYKQknwX36Y
legitnick
Hero Member
*****
Offline Offline

Activity: 532
Merit: 500



View Profile WWW
February 25, 2012, 01:49:13 PM
Last edit: February 29, 2012, 09:07:40 PM by legitnick
 #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..

5 BITCOIN RAFFLE GIVEAWAY
"I dont lift" - Lord Furrycoat
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!