我的php中有两个数组要打印。我想让它们连接在一起,但我不知道如何。 $ names的数组打印但描述数组“$ desc”没有。有没有办法一起打印?
$query = "SELECT eName FROM Events";
$query2 = "SELECT eDescription FROM Events";
$result = mysql_query($query);
$result2 = mysql_query($query2);
$names = array();
$desc = array();
echo "hello there people!" . $query . " ".$result;
for($i=0; $i<sizeof($result); $i++){
echo $result[$i] ."\n" . $result2[$i];
}
while($entry = mysql_fetch_row($result)){
$names[] = $entry[0];
}
while($entry2 = mysql_fetch_row($result2)){
$desc[] = $entry2[0];
}
echo "Which Event would you like to see?<br>";
$stop = count($names);
//echo $stop . "\n";
$i = 0;
print_r($names);
print_r($desc);
foreach($names as $value){
echo $value . " " . $desc[i] ."<br>";
$i++;
}
答案 0 :(得分:2)
为什么要进行两次查询以从同一来源获取数据?
$sql = mysql_query("select `eName`, `eDescription` from `Events`");
while($row = mysql_fetch_assoc($sql)) {
echo $row['eName']." ".$row['eDescription']."<br />";
}
更简单。
答案 1 :(得分:0)
试试这个:
foreach($names as $key => $value){
echo $value . " " . $desc[$key] ."<br />";
}
只要数组$key
匹配,信息就会一起打印。