dsf55s1233 2013-07-16 23:29
浏览 25
已采纳

PHP / MySQL多个查询

Whoops firgured otu where my parse error was. I had my semi-colon inside my first query.

So essentially I am trying to query with three different select statements in the same PHP script. Is this possible? (Last question I promise, after this I think the basics should get me a few weeks without having to ask more)

<?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());

?>  

Part Two Ok so I changed the code as suggested and tried to add it into a table. I'm still getting Parse error: syntax error, unexpected '$results1' (T_VARIABLE) in C:\xampp\htdocs\3718\assign5SELECT.php on line 7

I tried it without the table and it is still the same error. Is there something I am missing? Here is the updated code with the new variables.

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\">
";
while ($row1 = mysql_fetch_assoc($results1)) {
echo "<tr>
";
foreach($row1 as $value1) {
echo "<td>
";
echo $value1;
echo "</td>
";
}
echo "</tr>
";
}
echo "</table>
";

$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\">
";
while ($row2 = mysql_fetch_assoc($results2)) {
echo "<tr>
";
foreach($row2 as $value2) {
echo "<td>
";
echo $value2;
echo "</td>
";
}
echo "</tr>
";
}
echo "</table>
";

$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\">
";
while ($row3 = mysql_fetch_assoc($results3)) {
echo "<tr>
";
foreach($row3 as $value3) {
echo "<td>
";
echo $value3;
echo "</td>
";
}
echo "</tr>
";
}
echo "</table>
";
?>  
  • 写回答

2条回答 默认 最新

  • douao8204 2013-07-16 23:38
    关注
    $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());
    

    The problem you are encountering is that you are overwriting your results by using the same variable name.

    Example:

    $Var = "test";
     echo $Var; // Will output "test"
    $Var = "Another String"; 
     echo $Var; // Will output "Another String" rather than "test"; 
    

    So append:

    $Results_a = ...;
    $Results_b = ...;
    $Results_c = ...;
    

    So you can work with your variables easily, another thing you could look at is SQL joins: http://dev.mysql.com/doc/refman/5.0/en/join.html to cut down your amount of seperate queries

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 onlyoffice编辑完后立即下载,下载的不是最新编辑的文档
  • ¥15 求caverdock使用教程
  • ¥15 Coze智能助手搭建过程中的问题请教
  • ¥15 12864只亮屏 不显示汉字
  • ¥20 三极管1000倍放大电路
  • ¥15 vscode报错如何解决
  • ¥15 前端vue CryptoJS Aes CBC加密后端java解密
  • ¥15 python随机森林对两个excel表格读取,shap报错
  • ¥15 基于STM32心率血氧监测(OLED显示)相关代码运行成功后烧录成功OLED显示屏不显示的原因是什么
  • ¥100 X轴为分离变量(因子变量),如何控制X轴每个分类变量的长度。