gpt4 book ai didi

PHP 将 mysql 结果传递给变量

转载 作者:行者123 更新时间:2023-11-28 23:08:58 24 4
gpt4 key购买 nike

我想将 mysql 查询的结果传递给一个变量,该变量将我们数据库的电子邮件集成到 Mailchimp!但是我不知道如何将 mysql 结果传递给变量!

更精确的查询是:

$sql = "SELECT email, DATE_FORMAT(users.reg, '%Y-%m-%d') FROM users  
WHERE DATE(reg) = CURDATE()";
$result = $conn->query($sql);

if ($result->num_rows > 0)
{
while($row = $result->fetch_assoc())
{
echo " " . $row["email"]. "<br>";
}
} else {
echo "0 results";
}


$data = [
'email' => $row["email"],
'status' => 'subscribed'
];

syncMailchimp($data);

如何将查询结果传递给 $data 电子邮件?

最佳答案

尝试这样的事情:

<?php

$sql = "SELECT email, DATE_FORMAT(users.reg, '%Y-%m-%d') FROM users
WHERE DATE(reg) = CURDATE()";
$result = $conn->query($sql);

$data = [];
if ($result->num_rows > 0)
{
while($row = $result->fetch_assoc())
{
$data = [
'email' => $row["email"],
'status' => 'subscribed'
];
syncMailchimp($data);
}

}
else
{
echo "0 results";
}

它会在循环期间将所有email 发送到mailchimp。 1 对 1。

或者您可以使用单独的循环将其添加到 syncMailchimp 中:

<?php

$sql = "SELECT email, DATE_FORMAT(users.reg, '%Y-%m-%d') FROM users
WHERE DATE(reg) = CURDATE()";
$result = $conn->query($sql);

$emails = [];
if ($result->num_rows > 0)
{
while($row = $result->fetch_assoc())
{
$emails[] = [
'email' => $row["email"],
'status' => 'subscribed'
];
}

}
else
{
echo "0 results";
}


foreach($emails as $email){
syncMailchimp($email);
}

关于PHP 将 mysql 结果传递给变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46445818/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com