我解析错误的地方哎呀。我在第一次询问中得到了分号。
基本上我试图在同一个PHP脚本中使用三个不同的select语句进行查询。这可能吗? (最后一个问题我保证,在此之后我认为基础知识应该让我几周而不必再问更多)
<?php
include("server_connect.php");
mysql_select_db("rnissen");
$query = "SELECT column_one, column_two, column_four FROM tbltable;"
$results = mysql_query($query) or die(mysql_error());
$querytwo = "SELECT column_one, column_two, column_five FROM tbltable WHERE column_five = 1989";
$results = mysql_query($querytwo) or die(mysql_error());
$querythree = "SELECT COUNT(column_five) FROM tbltable WHERE column_five = 1989";
$results = mysql_query($querythree) or die(mysql_error());
?>
第二部分 好的,我按照建议更改了代码,并尝试将其添加到表中。我还在 解析错误:语法错误,第7行的C:\ xampp \ htdocs \ 3718 \ assign5SELECT.php中的意外'$ results1'(T_VARIABLE)
我在没有桌子的情况下尝试了它,但仍然是同样的错误。有什么我想念的吗?这是带有新变量的更新代码
mysql_select_db("rnissen");
$query = "SELECT column_one, column_two, column_four FROM tbltable;"
$results1 = mysql_query($query) or die(mysql_error());
echo "Column One, Column Two, Column Four : </br>";
echo "<table border=\"1\">\n";
while ($row1 = mysql_fetch_assoc($results1)) {
echo "<tr>\n";
foreach($row1 as $value1) {
echo "<td>\n";
echo $value1;
echo "</td>\n";
}
echo "</tr>\n";
}
echo "</table>\n";
$querytwo = "SELECT column_one, column_two, column_five FROM tbltable WHERE column_five = 1989";
$results2 = mysql_query($querytwo) or die(mysql_error());
echo "Column One, Column Two, Column Five : </br>";
echo "<table border=\"1\">\n";
while ($row2 = mysql_fetch_assoc($results2)) {
echo "<tr>\n";
foreach($row2 as $value2) {
echo "<td>\n";
echo $value2;
echo "</td>\n";
}
echo "</tr>\n";
}
echo "</table>\n";
$querythree = "SELECT COUNT(column_five) FROM tbltable WHERE column_five = 1989";
$results3 = mysql_query($querythree) or die(mysql_error());
echo "Column 4 has this many 1989s : </br>";
echo "<table border=\"1\">\n";
while ($row3 = mysql_fetch_assoc($results3)) {
echo "<tr>\n";
foreach($row3 as $value3) {
echo "<td>\n";
echo $value3;
echo "</td>\n";
}
echo "</tr>\n";
}
echo "</table>\n";
?>
答案 0 :(得分:1)
$query = "SELECT column_one, column_two, column_four FROM tbltable;"
$results = mysql_query($query) or die(mysql_error());
$querytwo = "SELECT column_one, column_two, column_five FROM tbltable WHERE column_five = 1989";
$results = mysql_query($querytwo) or die(mysql_error());
$querythree = "SELECT COUNT(column_five) FROM tbltable WHERE column_five = 1989";
$results = mysql_query($querythree) or die(mysql_error());
您遇到的问题是您使用相同的变量名称覆盖结果。
示例:
$Var = "test";
echo $Var; // Will output "test"
$Var = "Another String";
echo $Var; // Will output "Another String" rather than "test";
所以追加:
$Results_a = ...;
$Results_b = ...;
$Results_c = ...;
因此,您可以轻松处理变量,您可以看到的另一件事是SQL连接:http://dev.mysql.com/doc/refman/5.0/en/join.html以减少单独查询的数量
答案 1 :(得分:0)
您需要将查询保存到其他变量:
<?php
include("server_connect.php");
mysql_select_db("rnissen");
$query = "SELECT column_one, column_two, column_four FROM tbltable;"
$results1 = mysql_query($query) or die(mysql_error());
$querytwo = "SELECT column_one, column_two, column_five FROM tbltable WHERE column_five = 1989";
$results2 = mysql_query($querytwo) or die(mysql_error());
$querythree = "SELECT COUNT(column_five) FROM tbltable WHERE column_five = 1989";
$results3 = mysql_query($querythree) or die(mysql_error());
?>
文档示例:
$query = "SELECT CURRENT_USER();";
$query .= "SELECT Name FROM City ORDER BY ID LIMIT 20, 5";
/* execute multi query */
if (mysqli_multi_query($link, $query)) {
do {
/* store first result set */
if ($result = mysqli_store_result($link)) {
while ($row = mysqli_fetch_row($result)) {
printf("%s\n", $row[0]);
}
mysqli_free_result($result);
}
/* print divider */
if (mysqli_more_results($link)) {
printf("-----------------\n");
}
} while (mysqli_next_result($link));
}
对于您更新的问题,您在此处缺少分号:
$query = "SELECT column_one, column_two, column_four FROM tbltable";