简体   繁体   English

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

[英]Storing mysql row data into php variable name and value

I need help with storing values from a sql query into variables. 我需要将sql查询中的值存储到变量中的帮助。 The problem is in order to get the appropriate values I jad to write a long UNION query. 问题是为了获得合适的值,我不得不写一个长的UNION查询。 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: 这是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 <> ''

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. 我需要使用php访问这些值,以便可以建立表格。 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. 我已经准备好执行SQL语句,并且需要一些帮助将这些值转换为php变量。

Once you got the result of the mysql query, you can run it through extract 一旦获得mysql查询的结果,就可以通过解压缩运行它

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. 有关更多详细信息,请参见http://www.php.net/manual/zh/function.extract.php

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. 而且,在这种情况下,使用mysql可以获得更轻松的结果。

$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. 然后,您可以使用它进行各种操作,循环并输出表,检查值,键等。使用您所建议的内容,跟踪创建的var并不是一件容易的事,或通常很容易与他们合作。

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... 可能因您的需求而异常复杂,但有注释建议您使用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'];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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