使用多个电话号码和电子邮件循环遍历数据库 mysql php
loop through database mysql php with multiple phonenumbers and email
我正在尝试遍历数据库并为每个用户获取正确的数据。
但是我有多个 phone 和每个用户的邮件。
当我尝试循环时,我只能为每个用户获得一个(正确的data/user)号码,
当我尝试子循环时,我只从数据库中获取前两个数字。
所以第一个用户有正确的 phone 和邮件数据,其余用户有 phone 和来自第一个用户的邮件数据。
我不知道从这里可以做什么☹。
<?php
/**
* @author Johan Fuchs
* @copyright 2020
*/
//LEDEN.PHP
//login.php========================================================
require_once 'login.php';
$conn = new mysqli($hostname, $username, $password, $database);
if ($conn->connect_error) die("Fatal Error");
//begin html gedeelte==============================================
echo <<<_END
<html>
<head>
<title>display test</title>
<link href="" rel="stylesheet" type="text/css">
</head>
<body>
<h2>display test</h2>
</body>
</html>
_END;
//display leden===========================================================================================================================================================================
$query = "SELECT * FROM lid";
$result = $conn->query($query);
if (!$result) die ("Database access failed");
$rows = $result->num_rows;
for ($j = 0 ; $j < $rows ; ++$j)
{
$row = $result->fetch_array(MYSQLI_NUM);
$rs0 = htmlspecialchars($row[0]);
$rs1 = htmlspecialchars($row[1]);
$rs2 = htmlspecialchars($row[2]);
$rs3 = htmlspecialchars($row[3]);
$rs4 = htmlspecialchars($row[4]);
//get info telefoonnummer lid======================================
$subquery = "SELECT * FROM telefoonnummer WHERE lidnummer='$row[0]'";
$subresult = $conn->query($subquery);
if (!$subresult) die ("Database access failed");
$subrows = $subresult->num_rows;
$t = 0;
while ($t < $subrows)
{
$subrow = $subresult->fetch_array(MYSQLI_NUM);
$rs7 = htmlspecialchars($subrow[1]);
$tels[] = $rs7;
$t++;
}
$tel1s = $tels[0];
$tel2s = $tels[1];
//get info email lid================================================
$subquery = "SELECT * FROM email WHERE lidnummer='$row[0]'";
$subresult = $conn->query($subquery);
if (!$subresult) die ("Database access failed");
$subrows = $subresult->num_rows;
$m = 0;
while ($m < $subrows)
{
$subrow = $subresult->fetch_array(MYSQLI_NUM);
$rs8 = htmlspecialchars($subrow[1]);
$mails[] = $rs8;
$m++;
}
$mail1s = $mails[0];
$mail2s = $mails[1];
//get info postcode lid============================================
$subquery = "SELECT * FROM postcode WHERE postcode='$row[4]'";
$subresult = $conn->query($subquery);
if (!$subresult) die ("Database access failed");
$subrow = $subresult->fetch_array(MYSQLI_NUM);
$ps4 = htmlspecialchars($subrow[0]);
$rs6 = htmlspecialchars($subrow[1]);
$rs5 = htmlspecialchars($subrow[2]);
//display data lid=================================================
echo <<<_END
=================================================================
<pre>
Lidnummer :$rs0
Voornaam :$rs2
Achternaam :$rs1
Adres :$rs5
Huisnummer :$rs3
Postcode :$rs4
Woonplaats :$rs6
Telefoonnummer :$rs7 :$tel1s :$tel2s
Email :$rs8 :$mail1s :$mail2s
</pre>
_END;
}
?>
用户数据结果
display test
=================================================================
Lidnummer :1
Voornaam :firstname01
Achternaam :surname01
Adres :street01
Huisnummer :01
Postcode :1111AA
Woonplaats :city01
Telefoonnummer :0611111111 :0601010101 :0611111111
Email :test@mail11.com :test@mail01.com :test@mail11.com
=================================================================
Lidnummer :2
Voornaam :firstname02
Achternaam :surname02
Adres :street02
Huisnummer :02
Postcode :2222bb
Woonplaats :city02
Telefoonnummer :0622222222 :0601010101 :0611111111
Email :test@mail22 :test@mail01.com :test@mail11.com
=================================================================
Lidnummer :3
Voornaam :firstname03
Achternaam :surename03
Adres :street03
Huisnummer :03
Postcode :3333cc
Woonplaats :city03
Telefoonnummer :0633333333 :0601010101 :0611111111
Email
任何解决此问题的线索?
提前致谢:)
您需要在每次迭代中使用 unset() 函数来取消设置 $tels
和 $mails
数组的数组键。如果你不这样做,这就是将要发生的事情:
第一次迭代
$tels[0] will have First User Mobile
$tels[1] will have First User Mobile
第二次迭代
$tels[0] will have First User Mobile
$tels[1] will have First User Mobile
$tels[2] will have Second User Mobile
$tels[3] will have Second User Mobile
等等 mails
也是如此
在代码中,您只分配前 2 个键值,它们始终包含第一个用户的详细信息。
$tel1s = $tels[0];
$tel2s = $tels[1];
所以解决方案是在sub-query底部添加unset()函数,用于电话和电子邮件。
while ($t < $subrows)
{
$subrow = $subresult->fetch_array(MYSQLI_NUM);
$rs7 = htmlspecialchars($subrow[1]);
$tels[] = $rs7;
$t++;
}
$tel1s = $tels[0];
$tel2s = $tels[1];
unset($tels); /* This will unset the array $tels for next iteration */
这将确保在每次迭代时,$tels[0]
和 $tels[1]
将被重新分配,您将获得所需的信息。
类似地,在将值分配给变量 $mail1s
和 $mail2s
之后,也取消设置 $mails[]
数组。这应该可以解决您的问题。
我正在尝试遍历数据库并为每个用户获取正确的数据。 但是我有多个 phone 和每个用户的邮件。 当我尝试循环时,我只能为每个用户获得一个(正确的data/user)号码, 当我尝试子循环时,我只从数据库中获取前两个数字。 所以第一个用户有正确的 phone 和邮件数据,其余用户有 phone 和来自第一个用户的邮件数据。 我不知道从这里可以做什么☹。
<?php
/**
* @author Johan Fuchs
* @copyright 2020
*/
//LEDEN.PHP
//login.php========================================================
require_once 'login.php';
$conn = new mysqli($hostname, $username, $password, $database);
if ($conn->connect_error) die("Fatal Error");
//begin html gedeelte==============================================
echo <<<_END
<html>
<head>
<title>display test</title>
<link href="" rel="stylesheet" type="text/css">
</head>
<body>
<h2>display test</h2>
</body>
</html>
_END;
//display leden===========================================================================================================================================================================
$query = "SELECT * FROM lid";
$result = $conn->query($query);
if (!$result) die ("Database access failed");
$rows = $result->num_rows;
for ($j = 0 ; $j < $rows ; ++$j)
{
$row = $result->fetch_array(MYSQLI_NUM);
$rs0 = htmlspecialchars($row[0]);
$rs1 = htmlspecialchars($row[1]);
$rs2 = htmlspecialchars($row[2]);
$rs3 = htmlspecialchars($row[3]);
$rs4 = htmlspecialchars($row[4]);
//get info telefoonnummer lid======================================
$subquery = "SELECT * FROM telefoonnummer WHERE lidnummer='$row[0]'";
$subresult = $conn->query($subquery);
if (!$subresult) die ("Database access failed");
$subrows = $subresult->num_rows;
$t = 0;
while ($t < $subrows)
{
$subrow = $subresult->fetch_array(MYSQLI_NUM);
$rs7 = htmlspecialchars($subrow[1]);
$tels[] = $rs7;
$t++;
}
$tel1s = $tels[0];
$tel2s = $tels[1];
//get info email lid================================================
$subquery = "SELECT * FROM email WHERE lidnummer='$row[0]'";
$subresult = $conn->query($subquery);
if (!$subresult) die ("Database access failed");
$subrows = $subresult->num_rows;
$m = 0;
while ($m < $subrows)
{
$subrow = $subresult->fetch_array(MYSQLI_NUM);
$rs8 = htmlspecialchars($subrow[1]);
$mails[] = $rs8;
$m++;
}
$mail1s = $mails[0];
$mail2s = $mails[1];
//get info postcode lid============================================
$subquery = "SELECT * FROM postcode WHERE postcode='$row[4]'";
$subresult = $conn->query($subquery);
if (!$subresult) die ("Database access failed");
$subrow = $subresult->fetch_array(MYSQLI_NUM);
$ps4 = htmlspecialchars($subrow[0]);
$rs6 = htmlspecialchars($subrow[1]);
$rs5 = htmlspecialchars($subrow[2]);
//display data lid=================================================
echo <<<_END
=================================================================
<pre>
Lidnummer :$rs0
Voornaam :$rs2
Achternaam :$rs1
Adres :$rs5
Huisnummer :$rs3
Postcode :$rs4
Woonplaats :$rs6
Telefoonnummer :$rs7 :$tel1s :$tel2s
Email :$rs8 :$mail1s :$mail2s
</pre>
_END;
}
?>
用户数据结果
display test
=================================================================
Lidnummer :1
Voornaam :firstname01
Achternaam :surname01
Adres :street01
Huisnummer :01
Postcode :1111AA
Woonplaats :city01
Telefoonnummer :0611111111 :0601010101 :0611111111
Email :test@mail11.com :test@mail01.com :test@mail11.com
=================================================================
Lidnummer :2
Voornaam :firstname02
Achternaam :surname02
Adres :street02
Huisnummer :02
Postcode :2222bb
Woonplaats :city02
Telefoonnummer :0622222222 :0601010101 :0611111111
Email :test@mail22 :test@mail01.com :test@mail11.com
=================================================================
Lidnummer :3
Voornaam :firstname03
Achternaam :surename03
Adres :street03
Huisnummer :03
Postcode :3333cc
Woonplaats :city03
Telefoonnummer :0633333333 :0601010101 :0611111111
Email
任何解决此问题的线索?
提前致谢:)
您需要在每次迭代中使用 unset() 函数来取消设置 $tels
和 $mails
数组的数组键。如果你不这样做,这就是将要发生的事情:
第一次迭代
$tels[0] will have First User Mobile
$tels[1] will have First User Mobile
第二次迭代
$tels[0] will have First User Mobile
$tels[1] will have First User Mobile
$tels[2] will have Second User Mobile
$tels[3] will have Second User Mobile
等等 mails
也是如此
在代码中,您只分配前 2 个键值,它们始终包含第一个用户的详细信息。
$tel1s = $tels[0];
$tel2s = $tels[1];
所以解决方案是在sub-query底部添加unset()函数,用于电话和电子邮件。
while ($t < $subrows)
{
$subrow = $subresult->fetch_array(MYSQLI_NUM);
$rs7 = htmlspecialchars($subrow[1]);
$tels[] = $rs7;
$t++;
}
$tel1s = $tels[0];
$tel2s = $tels[1];
unset($tels); /* This will unset the array $tels for next iteration */
这将确保在每次迭代时,$tels[0]
和 $tels[1]
将被重新分配,您将获得所需的信息。
类似地,在将值分配给变量 $mail1s
和 $mail2s
之后,也取消设置 $mails[]
数组。这应该可以解决您的问题。