array_merge()问题与PDO

时间:2016-10-13 15:23:34

标签: php arrays pdo

我有一个函数应该返回一个像这样的数组

Array
(
[company1] => position1
[company2] => user2
)

我有一个正确的SQL查询,它可以很好地获取所需的数据(在MySQL工作台中测试并通过转储数据。

执行该操作的功能(请参阅CAPS中有问题的注释中的注释):

function check_user_status ($db){
    try {
        $log = new PDO("mysql:host=".$db['server'].";dbname=".$db['db'], $db['mysql_login'], $db['mysql_pass'], array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));
        // set the PDO error mode to exception
        $log->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        // prepare sql and bind parameters
        $stmt=$log->prepare ("select a.companyname,b.role from companies a, roles b where a.companyid=b.companyid and (b.uid = :uid and b.suspended = 0);");

        $stmt->bindParam(":uid", $_SESSION['uid'], PDO::PARAM_INT);
        $stmt->execute();
        $count=$stmt->rowCount();
        //echo "<br>count = $count<br>"; //outputs the numbner of resulting rows for debuging
        if($count >=1) {
            unset ($_SESSION['status']); //purge any previous user_status
            $_SESSION['status'] = array();
            while ($userRow = $stmt->fetch(PDO::FETCH_ASSOC)) {
            //echo "<br>{$userRow['companyname']} => {$userRow['role']}<br>";  outputs the data for debuging
            array_merge ($_SESSION['status'], array($userRow['companyname'] => $userRow['role']));// THIS DOESN'T WORK FOR SOME REASON
            }
            return true;
        }
        else
        {
            //return false;
        } 

    }
    catch(PDOException $e) {
        return false;
        echo 'error: '. $e->getMessage();
    }

}

现在这个:

check_user_status($db);
var_dump ($_SESSION['status']);

输出空数组:

array(0) { }

array_merge_recursive()也不起作用。通知已启用,不会发出通知。任何帮助都将受到高度赞赏。

1 个答案:

答案 0 :(得分:2)

RTM:http://php.net/array_merge

  

返回值:返回结果数组。

你有:

(define (long a b)
(cond ((null? a) '())
      ((null? b) '())
      (else
(if (equal? (car a) (car b))
    (cons a (long (cdr a) (cdr b)))
    (cons b (long (cdr a) (cdr b)))))))

&#34;出于某种原因&#34; =你完全忽略了返回值,这是你的合并数组。