你好,早安,
我还在学习PHP,出于某种原因,我的脚本不会在我的foreach循环中发布任何数据。任何想法为什么? emailRow Echos很好,但我要删除我的代码如下:
<?php
include 'includes/header.php';
$accountUser = array();
$upgradeEmail = $_GET['currEmail'];
$emailQuery = "SELECT fbID, firstName, lastName FROM users WHERE emailOne='".$upgradeEmail."' AND authLevel=0";
<?php echo $emailRow['fbID']; ?>
<?php echo $emailRow['firstName']; ?>
<?php echo $emailRow['lastName']; ?>
while($emailRow = mysql_fetch_assoc($emailQuery, $conn))
{
$accountUser[]=$emailRow;
}
?>
<table>
<?php foreach($accountUser as $emailData) { ?>
<tr><td> <?php emailData['fbID']; ?> </td><td><?php emailData['firstName']; ?></td><td><?php emailData['lastName']; ?></td></tr>
<?php } ?>
</table>
答案 0 :(得分:3)
您已在$emailQuery
中构建了SQL查询,但从未执行过它。致电mysql_query()
,并将其结果资源传递给mysql_fetch_assoc()
。
$emailQuery = "SELECT fbID, firstName, lastName FROM users WHERE emailOne='".$upgradeEmail."' AND authLevel=0";
$result = mysql_query($emailQuery);
if ($result)
{
while($emailRow = mysql_fetch_assoc($result, $conn))
{
$accountUser[]=$emailRow;
}
}
else // your query failed
{
// handle the failure
}
请确保通过mysql_real_escape_string()
上的$upgradeEmail
来保护您的数据库免受SQL注入,因为您是从$_GET.
$upgradeEmail = mysql_real_escape_string($_GET['currEmail']);
答案 1 :(得分:0)
你实际上并没有回应任何东西 以及不运行查询 还有一些其他方法可以做事,比通常的丑陋PHP更清洁。
一个功能
function sqlArr($sql){
$ret = array();
$res = mysql_query($sql) or trigger_error(mysql_error()." ".$sql);
if ($res) {
while($row = mysql_fetch_array($res)){
$ret[] = $row;
}
}
return $ret;
}
代码
$email = mysql_real_escape_string($_GET['currEmail']);
$data = sqlArr("SELECT * FROM users WHERE emailOne='$email' AND authLevel=0");
include 'template.php';
模板
<? include 'includes/header.php' ?>
<table>
<? foreach($data as $row) { ?>
<tr>
<td><?=$row['fbID']?></td>
<td><?=$row['firstName']?></td>
<td><?=$row['lastName']?></td>
</tr>
<? } ?>
</table>
答案 2 :(得分:-1)
你有一个语法错误。您无法在现有的php标记中打开新的php标记。您必须先关闭已打开的标记。
至于让查询起作用,
首先,您必须在打印数据或回显数据之前获取数据...
while($emailRow = mysql_fetch_assoc($emailQuery, $conn))
{
$accountUser[]=$emailRow;
}
那么你可以写一些陈述..
echo $emailRow['fbID']; etc. code.
其次,您没有触发查询,只是编写了查询语句。使用mysql_query
来解雇它。
您的代码将是这样的......
<?php include 'includes/header.php';
$accountUser = array();
$upgradeEmail = $_GET['currEmail'];
$emailQuery = mysql_query("SELECT fbID, firstName, lastName FROM users WHERE emailOne='".$upgradeEmail."' AND authLevel=0") or die (mysql_error());
while($emailRow = mysql_fetch_assoc($emailQuery, $conn))
{
$accountUser[]=$emailRow;
}
echo $emailRow['fbID'];
echo $emailRow['firstName'];
echo $emailRow['lastName'];
print '<table>';
foreach($accountUser as $emailData) {
print '<tr><td>'$.emailData['fbID'].'</td><td>'.$emailData['firstName'].'</td><td>'.$emailData['lastName'].'</td></tr>';
}
print '</table';
?>
随意使用此代码,根据您的需要进行修改。