简体   繁体   中英

Storing mysql row data into php variable name and value

I need help with storing values from a sql query into variables. The problem is in order to get the appropriate values I jad to write a long UNION query. There are only two columns, and not many results. Ideally, I want to create a variable for each row where the variable name is the result from the first column and the corresponding value for that variable would be the result from the second column.

Here is the UNION query:

SELECT 'mon_m' as shift, count(mon_m) as count
FROM schedule
WHERE week_of = '2013-12-23'
AND mon_m <> ''

UNION

SELECT 'mon_a', COUNT(mon_a) 
FROM schedule
WHERE week_of = '2013-12-23'
AND mon_a <> ''

UNION

SELECT 'mon_n', COUNT(mon_n)
FROM schedule
WHERE week_of = '2013-12-23'
AND mon_n <> ''

UNION

SELECT 'tue_m', count(tue_m) 
FROM schedule
WHERE week_of = '2013-12-23'
AND tue_m <> ''

UNION

SELECT 'tue_a', COUNT(tue_a) 
FROM schedule
WHERE week_of = '2013-12-23'
AND tue_a <> ''

UNION

SELECT 'tue_n', COUNT(tue_n)
FROM schedule
WHERE week_of = '2013-12-23'
AND tue_n <> ''

UNION

SELECT 'wed_m', count(wed_m) 
FROM schedule
WHERE week_of = '2013-12-23'
AND wed_m <> ''

UNION

SELECT 'wed_a', COUNT(wed_a) 
FROM schedule
WHERE week_of = '2013-12-23'
AND wed_a <> ''

UNION

SELECT 'wed_n', COUNT(wed_n)
FROM schedule
WHERE week_of = '2013-12-23'
AND wed_n <> ''

UNION

SELECT 'thu_m', count(thu_m) 
FROM schedule
WHERE week_of = '2013-12-23'
AND thu_m <> ''

UNION

SELECT 'thu_a', COUNT(thu_a) 
FROM schedule
WHERE week_of = '2013-12-23'
AND thu_a <> ''

UNION

SELECT 'thu_n', COUNT(thu_n)
FROM schedule
WHERE week_of = '2013-12-23'
AND thu_n <> ''

UNION

SELECT 'fri_m', count(fri_m) 
FROM schedule
WHERE week_of = '2013-12-23'
AND fri_m <> ''

UNION

SELECT 'fri_a', COUNT(fri_a) 
FROM schedule
WHERE week_of = '2013-12-23'
AND fri_a <> ''

UNION

SELECT 'fri_n', COUNT(fri_n)
FROM schedule
WHERE week_of = '2013-12-23'
AND fri_n <> ''

UNION

SELECT 'sat', count(sat) 
FROM schedule
WHERE week_of = '2013-12-23'
AND sat <> ''

UNION

SELECT 'sun', COUNT(sun) 
FROM schedule
WHERE week_of = '2013-12-23'
AND sun <> ''

Here is a small sample of what the data would like:

ColumnA    ColumnB
mon_m      1
mon_a      5
mon_n      6
tue_m      3
tue_a      6
tue_n      2

I need to access these values using php so I can build a table. I'd like to access them using variables. For example:

echo $mon_m
echo $mon_a
echo $mon_n

Where

$mon_m = 1
$mon_a = 5
$mon_n = 6

I have the SQL statement ready to go and need some help with getting these values into php variables.

Once you got the result of the mysql query, you can run it through extract

extract($results);

This will create a variable for each key in the array, so make sure your result is returned as an associative array. See http://www.php.net/manual/en/function.extract.php for more details.

Why not use an array?

$output_array = array(
    "mon_m" => 1,
    "mon_a" => 2,
    "mon_n" => 3
);

Then output them like this

echo $output_array['mon_m'];

And, your results can be even easier to get with mysql in this situation.

$r = mysql_query("select * from table where columnB=" . $var);
while($row = mysql_fetch_assoc($r)){
    $column = $row['columnA'];
    $output_array[$column] = $row['columnB'];
}

Then you can do all kinds of stuff with it, loop through and output a table, check for values, keys, etc. With what you have proposed, I don't think it will be very easy to keep track of the vars created, or very easy to work with them in general.

EDIT::

And here is a better(?) way to do it. Probably exceptionally over-complicated for your needs, but with the comments suggesting you use PDO...

function getObject($sql){
    $dbCon = new object();
        // building data source name 
        $dsn = 'mysql:host=' . DB_HOST .
               ';dbname='    . DB_NAME .
               ';connect_timeout=15';

        $dbCon->dbh = new PDO($dsn, DB_USER, DB_PASS);

    try {

        $stmt = $dbCon->dbh->prepare($sql);

        $stmt->setFetchMode(PDO::FETCH_OBJ);

        if($stmt->execute()){
                return (array) $stmt->fetchObject();
        } else {
            return false;
        }
    }

    catch(PDOException $e) {  
        $_messages[] = $e->getMessage();  
    }
}

And use it like this

$my_array = getObject($your_query);

if(!empty($my_array['mon_m'])){
    echo $my_array['mon_m'];
}

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