0

哎呀,我的解析错误在哪里。我的第一个查询中有分号。

所以基本上我试图在同一个 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());

?>  

第二部分好的,所以我按照建议更改了代码并尝试将其添加到表中。我仍然收到 Parse error: syntax error, unexpected '$results1' (T_VARIABLE) in C:\xampp\htdocs\3718\assign5SELECT.php on line 7

我在没有桌子的情况下尝试了它,它仍然是同样的错误。有什么我想念的吗?这是带有新变量的更新代码。

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";
?>  
4

2 回答 2

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 以减少单独查询的数量

于 2013-07-16T23:38:26.363 回答
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());

?>  

但是,这是使用mysqli_multi_query执行多个查询的更方便的方法:

文档中的示例:

$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";
于 2013-07-16T23:41:21.567 回答