将mysql行数据存储到php变量名和值中

时间:2013-12-17 19:27:23

标签: php mysql variables

我需要帮助将sql查询中的值存储到变量中。问题是为了获得适当的值我jad写一个长的UNION查询。只有两列,结果不多。理想情况下,我想为每一行创建一个变量,其中变量名称是第一列的结果,该变量的相应值将是第二列的结果。

这是UNION查询:

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 <> ''

以下是数据所需内容的一小部分示例:

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

我需要使用php访问这些值,以便我可以构建一个表。我想使用变量访问它们。例如:

echo $mon_m
echo $mon_a
echo $mon_n

其中

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

我准备好了SQL语句,需要一些帮助才能将这些值转换为php变量。

2 个答案:

答案 0 :(得分:2)

获得mysql查询的结果后,可以通过extract

运行它
extract($results);

这将为数组中的每个键创建一个变量,因此请确保将结果作为关联数组返回。有关详细信息,请参阅http://www.php.net/manual/en/function.extract.php

答案 1 :(得分:1)

为什么不使用数组?

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

然后像这样输出它们

echo $output_array['mon_m'];

而且,在这种情况下,使用mysql可以更轻松地获得结果。

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

然后你可以用它来做各种各样的事情,循环并输出一个表,检查值,键等。根据你提出的建议,我认为跟踪它并不容易。 vars创建,或者很容易与他们一起工作。

EDIT ::

这是一种更好的(?)方法。可能特别过于复杂以满足您的需求,但评论建议您使用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();  
    }
}

并像这样使用

$my_array = getObject($your_query);

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