0

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\">\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
  • "Is this possible?" - yes, it is. Commented Jul 16, 2013 at 23:31
  • You're overwriting $results with each query. Those need to be different variables to be accessible. Commented Jul 16, 2013 at 23:32
  • The original MySQL extension is now deprecated. I would recommend you learn by using MySQLi (php.net/manual/en/book.mysqli.php) or PDO (php.net/manual/en/ref.pdo-mysql.php) Commented Jul 16, 2013 at 23:39
  • I updated my answer for your edit Commented Jul 17, 2013 at 0:32

2 Answers 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());

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

Sign up to request clarification or add additional context in comments.

Comments

0

You need to save the queries to a different variable:

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

?>  

However, this is a more convenient way to do perform multiple queries using mysqli_multi_query:

Example from the docs:

$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));
}

Edit:

For your updated question, you are missing a semicolon here:

$query = "SELECT column_one, column_two, column_four FROM tbltable";

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.