如果 mysql 没有产生任何结果,您如何为变量赋值?
How can you assign a value to a variable if mysql does not yield any result?
有两个数据库。我正在尝试查看第一个数据库中的电子邮件是否也存在于第二个数据库中。这是我试过的代码:
// Getting info from the first "subscribers" database.
$sql = "SELECT name, email, status
FROM subscribers
WHERE status='active'";
$result = mysqli_query($link,$sql);
while($row = mysqli_fetch_assoc($result)) {
// To check if all emails in "subscribers" database are also present in "phpbb_users" database.
// If they are present then user type=0 otherwise user type=1.
$subscriber_email = $row['email'];
$forum_sql = mysqli_query($forum_link, "SELECT user_type
FROM phpbb_users
WHERE user_email='$subscriber_email'");
while($forum_row = mysqli_fetch_assoc($forum_sql)){
$ut = $forum_row['user_type'];
if($ut=="0"){
$type="0";
} else {
$type="1";
}
echo $type;
}
// To diplay the data from "subscribers" database.
echo $row["name"];
echo $row['email'];
}
输出为:
onera 和 julian 的类型为空,因为 phpbb_users 数据库中不存在电子邮件。我怎样才能避免这个空白 space 并使它成为 1 或其他任何东西。 var_dump() 什么都不显示。
您可以将所有 php 代码替换为 SQL 单个语句,使用 exists
和子查询:
select name, email,
exists (select 1 from phpbb_users u where u.user_email = s.email) as exists_in_phpbb
from subscribers s
where status = 'active'
这会在名为 exists_in_phpbb
的结果集中添加一列,其中包含 1
的电子邮件可以在 phpbb_users
中找到,如果没有,则包含 0
。
这是本节代码中的一个简单逻辑问题:
while($forum_row = mysqli_fetch_assoc($forum_sql)){
$ut = $forum_row['user_type'];
if($ut=="0"){
$type="0";
} else {
$type="1";
}
echo $type;
}
如果用户不在第二个table,则此代码将永远不会运行。所以 type
永远不会输出。
重新组织代码,使类型默认设置并始终输出,即使第二行没有行table:
$type = "1";
while($forum_row = mysqli_fetch_assoc($forum_sql)) {
$ut = $forum_row['user_type'];
if($ut == "0"){
$type = "0";
}
}
echo $type;
试试这条线..
echo empty($ut) ? 1 : $ut;
尝试
if (!isset($type)) {
echo "1"
}
在内部 while 之后
有两个数据库。我正在尝试查看第一个数据库中的电子邮件是否也存在于第二个数据库中。这是我试过的代码:
// Getting info from the first "subscribers" database.
$sql = "SELECT name, email, status
FROM subscribers
WHERE status='active'";
$result = mysqli_query($link,$sql);
while($row = mysqli_fetch_assoc($result)) {
// To check if all emails in "subscribers" database are also present in "phpbb_users" database.
// If they are present then user type=0 otherwise user type=1.
$subscriber_email = $row['email'];
$forum_sql = mysqli_query($forum_link, "SELECT user_type
FROM phpbb_users
WHERE user_email='$subscriber_email'");
while($forum_row = mysqli_fetch_assoc($forum_sql)){
$ut = $forum_row['user_type'];
if($ut=="0"){
$type="0";
} else {
$type="1";
}
echo $type;
}
// To diplay the data from "subscribers" database.
echo $row["name"];
echo $row['email'];
}
输出为:
onera 和 julian 的类型为空,因为 phpbb_users 数据库中不存在电子邮件。我怎样才能避免这个空白 space 并使它成为 1 或其他任何东西。 var_dump() 什么都不显示。
您可以将所有 php 代码替换为 SQL 单个语句,使用 exists
和子查询:
select name, email,
exists (select 1 from phpbb_users u where u.user_email = s.email) as exists_in_phpbb
from subscribers s
where status = 'active'
这会在名为 exists_in_phpbb
的结果集中添加一列,其中包含 1
的电子邮件可以在 phpbb_users
中找到,如果没有,则包含 0
。
这是本节代码中的一个简单逻辑问题:
while($forum_row = mysqli_fetch_assoc($forum_sql)){
$ut = $forum_row['user_type'];
if($ut=="0"){
$type="0";
} else {
$type="1";
}
echo $type;
}
如果用户不在第二个table,则此代码将永远不会运行。所以 type
永远不会输出。
重新组织代码,使类型默认设置并始终输出,即使第二行没有行table:
$type = "1";
while($forum_row = mysqli_fetch_assoc($forum_sql)) {
$ut = $forum_row['user_type'];
if($ut == "0"){
$type = "0";
}
}
echo $type;
试试这条线..
echo empty($ut) ? 1 : $ut;
尝试
if (!isset($type)) {
echo "1"
}
在内部 while 之后