简体   繁体   中英

PHP PDO foreach variables

I'm currently working on a PHP script, which uses pdo to access the database tables. It's just about done just trying to figure out this last thing. I'm still relatively new to PHP, and probably could figure it out myself by looking around, I just have no idea what to look for.

I'll tell you what I have, I have a table called servers. It has a column called location (USA, CANADA, MEXICO, and EU are the values in each row). There is then another table for each location that corresponds to server table, (USA, CANADA, MEXICO, and EU). Then in each table with the location has a column called SLOTS say the first row has 2, second has 3, etc. I want to get the sum of all of them and then put it into a variable, and then each of them will be displayed onto the same page. What I have now works fine, but I want to have it when more locations are added I don't have to go back and add it, it'll get it from the location column in the servers table, and make it a bit cleaner then it is. I was looking into variable variables but not sure if it'd work or how to get it working in this situation?

What I have

$usaresult = $db->query("SELECT SUM(SLOTS) AS value_sum FROM USA");
$usarow = $usaresult->fetch(PDO::FETCH_ASSOC);
$usaslots = $usarow['value_sum'];
if(!isset($usarow['value_sum'])) {
    $usaslots = '0';
}

$canadaresult = $db->query("SELECT SUM(SLOTS) AS value_sum FROM CANADA");
$canadarow = $canadaresult->fetch(PDO::FETCH_ASSOC);
$canadaslots = $canadarow['value_sum'];
if(!isset($canadarow['value_sum'])) {
    $canadaslots = '0';
}

$mexicoresult = $db->query("SELECT SUM(SLOTS) AS value_sum FROM MEXICO");
$mexicorow = $mexicoresult->fetch(PDO::FETCH_ASSOC);
$mexicoslots = $mexicorow['value_sum'];
if(!isset($mexicorow['value_sum'])) {
    $mexicoslots = '0';
}

$euresult = $db->query("SELECT SUM(SLOTS) AS value_sum FROM EU");
$eurow = $euresult->fetch(PDO::FETCH_ASSOC);
$euslots = $eurow['value_sum'];
if(!isset($eurow['value_sum'])) {
    $euslots = '0';
}

Here is something that I was thinking of, doesn't work and is probably completely off, and probably something much better could be done, but should give an idea of what I'm looking for, I think?

$query = "SELECT * FROM servers";

try 
{ 
    $stmt = $db->prepare($query); 
    $stmt->execute();
    $result = $stmt->fetchAll();
}

catch(PDOException $ex) 
{ 
    die("Failed to run query: " . $ex->getMessage()); 
}

foreach ($result as $row) {
    ${$row['location']}result = $db->query("SELECT SUM(SLOTS) AS value_sum FROM $row['location']");
    ${$row['location']}row = ${$row['location']}result->fetch(PDO::FETCH_ASSOC);
    ${$row['location']}slots = ${$row['location']}row['value_sum'];
    if(!isset(${$row['location']}row['value_sum'])) {
    ${$row['location']}slots = '0';
    }
}

If anyone could help me with this, or point me in the right direction, would be great. Thanks in advance if anyone is able to.

Why not have one table for all server, for example like this

CREATE TABLE `servers` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `location` varchar(255) NOT NULL,
  `slots` int(11) NOT NULL,
  PRIMARY KEY (`ID`)
) 

and then query it like this

SELECT location, slots from servers group by location 

This will neatly sum up the slots per location, you can have multiple rows for each location, and you can add locations at will without having to add tables

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM