连接到两个数据库表以将数据存储在阵列中

时间:2018-12-04 18:30:06

标签: php mysql arrays

我需要一些代码帮助,因为在将数据存储在Missing required module 'Minizip'数组中遇到问题。我想同时从用户表中提取module.modulemap$_SESSIONusernameid的数据时同时连接到两个不同的数据库表,并同时提取广告系列表中firstname的数据,因此我可以将它们存储在current_campaign数组中。

但是,当我尝试这样做时:

campaign_name

它将给我显示出这样的信息:

$_SESSION

应该是:

Array([campaign] => somename [campaign] => youtube [campaign] => google [campaign] => linkedlin [campaign] => bing [campaign] => facebook)

我认为问题出在这段代码中:

$link = mysqli_connect('localhost', 'mydbusername', 'mydbpassword', 'mydbname');
$sql = "SELECT id, username, password, firstname, current_campaign FROM users WHERE username = ?";
$campaign_db = "SELECT campaign_name FROM campaign WHERE username = 'myusername'";

if($stmt = mysqli_prepare($link, $sql)){
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "s", $param_username);

// Set parameters
$param_username = $username;

// Attempt to execute the prepared statement
if(mysqli_stmt_execute($stmt)){
    // Store result
    mysqli_stmt_store_result($stmt);

    // Check if username exists, if yes then verify password
    if(mysqli_stmt_num_rows($stmt) == 1){                    
       // Bind result variables
       mysqli_stmt_bind_result($stmt, $id, $username, $hashed_password, $firstname, $lastname, $email, $gender, $current_campaign, $country);

       if(mysqli_stmt_fetch($stmt)) {
          if(password_verify($password, $hashed_password)) {

             // Password is correct, so start a new session
             session_start();

             // Store data in session variables
             $_SESSION["loggedin"] = true;
             $_SESSION["id"] = $id;
             $_SESSION["username"] = $username; 
             $_SESSION["firstname"] = $firstname;
             $_SESSION["current_campaign"] = $current_campaign;


             if($stmt_1 = mysqli_prepare($link, $campaign_db))
             {
                 if(mysqli_stmt_execute($stmt_1)) 
                 {
                     mysqli_stmt_store_result($stmt_1);

                     if(mysqli_stmt_num_rows($stmt_1) >= 1)
                     {
                        mysqli_stmt_bind_result($stmt_1, $campaign);

                        /* execute query */
                        $stmt_1->execute();

                        /* Get the result */
                        $result = $stmt_1->get_result();
                        $num_of_rows = $result->num_rows;

                        while ($campaign = $result->fetch_assoc())
                        {
                           $_SESSION["campaign"] = $campaign['campaign_name'];
                        }
                    }
                }
            }
        }
    }
}

我在Array ( [campaign] => facebook ) 的数组中看到的内容是因为数组中仅存储一个数据。我可以毫无问题地将用户名,id,名字和current_campaign存储在数组中。

能否请您举一个示例,说明在获取要存储在if($stmt_1 = mysqli_prepare($link, $campaign_db)) { if(mysqli_stmt_execute($stmt_1)) { mysqli_stmt_store_result($stmt_1); if(mysqli_stmt_num_rows($stmt_1) >= 1) { mysqli_stmt_bind_result($stmt_1, $campaign); /* execute query */ $stmt_1->execute(); /* Get the result */ $result = $stmt_1->get_result(); $num_of_rows = $result->num_rows; while ($campaign = $result->fetch_assoc()) { $_SESSION["campaign"] = $campaign['campaign_name']; } } } } 数组中的数据时如何能够同时连接到两个不同的数据库表?

谢谢。

0 个答案:

没有答案