1

As you can see from the code below im very new to PHP, so please excuse the newbie question but ive been struggling with this all afternoon and just cant figure out what exactly is the problem.

Basically Ive got 3 dropdown menus for team1, team2 and venue. When user selects teams and venue an isset function is triggered and I extract the result from the game out of my database.

As you can see in the code below I use 3 echo statements to test if correct values from dropdown menus has been captured. When I open the page the echo statements confirm the correct results are captured from the drop-downs, I go on and query the database with the variables I echoed above.

The Problem

In the while loop, I want to echo the results from the query BUT nothing gets displayed. I tried doing a var_dump to see the contents of the variables but NOTHING gets displayed. What am I doing wrong?

foreach($_REQUEST["team1"] as $teamone){
        $team1 = $teamone;
         }
        foreach($_REQUEST["team2"] as $teamtwo){
        $team2 = $teamtwo;  
        }
         foreach($_REQUEST['venue'] as $venue){
             $venue = $venue;
         }
        //These echo statments are a test to see if the correct dropdown values has been captured
        echo $team1.'<br>';
        echo $team2.'<br>';
        echo $venue;
        //Use results from dropdown menu to query database 
    $result = mysql_query('SELECT * 
                      FROM  `results` 
                      WHERE  `hometeam` =  "$team1" &&  `awayteam` = "$team2"') or 
                          die(mysql_error());                                                           

        while($row = mysql_fetch_array($result)){
        echo $row['awayteamscore'];
        echo $row['hometeamscore'];
        }

If anyone can point me in the right direction it would be greatly appreciated.

8
  • The mysql query returns any results or any errors? I assume that you connect to the database properly in some other part of the code. Right? Commented Jan 26, 2014 at 10:37
  • did you var_dump($_REQUEST) ? Commented Jan 26, 2014 at 10:39
  • @AggelosSynadakis I get no errors and I can confirm I am successfully connecting to DB Commented Jan 26, 2014 at 10:41
  • 1
    Though this is not related to your question,but if you are new to PHP then you should invest your time in PDO rather than mysql_ operations. Commented Jan 26, 2014 at 10:42
  • Why have you got 3 forloops to access 3 different variables? Commented Jan 26, 2014 at 10:42

5 Answers 5

1

I suspect you're receiving sql syntax error for two reasons at least First when you want to use the variable value within string you must use double embracing quotes

$sql="select * from mytable where col1='$my_var'";

And second: SQL expects string values ebraced with simgle quotes as I pointed above. In your case you use "" quotes.

And one more recommendation. When debugging php app it might be useful to enable extended output by inserting

error_reporing(E_ALL);

somewhere in the beginning of php script

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

2 Comments

Thank you that fixed it, ill remember that for in the future
dough not part of my original question, can you perhaps give me some advice to cleaning up my code in terms of the 3 forloops im using. Obviously this is not the most efficient way to get the values from the 3 dropboxes, any idea what I can do to use 1 forloop instead of 3?
1

you mixed between double and single quotes . while variables shouldnt be rounded by double quotes.

   $result = mysql_query(" SELECT * 
                  FROM  `results` 
                  WHERE  `hometeam` =  '$team1' &&  `awayteam` = '$team2' ") or 
                      die(mysql_error());                                                           

    while($row = mysql_fetch_array($result)){
    echo $row['awayteamscore'];
    echo $row['hometeamscore'];
    }

Comments

0

You put your SQL query in single quotes and variables do not work in there. Try this:

<?php
$var = "stuff";
echo '$var';
echo "$var";
?>

What you can do instead is to concatenate strings like this:

$result = mysql_query('SELECT * 
                      FROM  `results` 
                      WHERE  `hometeam` =  "'.$team1.'" &&  `awayteam` = "'.$team2.'"')

Comments

0

My Friend Your code is just fine you just need to check that your submiting the dropboxes with the form so first check your html and when you var_dump($_REQUEST) it should be an array containing all the values that you trying to gather

Comments

-1

Your foreach loops are overriding the values. It would be best to put it in an array like so

$team1 = array();
foreach($_REQUEST["team1"] as $teamone){
    $team1[] = $teamone;
}

if you need to make the values of $team1 into a string then this would be more appropriate

$team1 = "";
foreach($_REQUEST["team1"] as $teamone){
    $team1 .= $teamone;
}

Fixing the above first might help. If not then maybe the problem is in your db queries.

//Using double quotes

"SELECT * FROM  `results` WHERE  `hometeam` =  '$team1' &&  `awayteam` = '$team2'"

//Using single quotes

'SELECT * FROM  `results` WHERE  `hometeam` =  "'.$team1.'" &&  `awayteam` = "'.$team2.'"'

Then display the result

while($row = mysql_fetch_array($result)){
    echo $row['awayteamscore'];
    echo $row['hometeamscore'];
}

2 Comments

you are wrong my man while($row = mysql_fetch_array($result)) will not cause an infinite loop even php themselves use it
@AboQutiesh yup I saw that. edited it. sorry got used to mysqli prepared statements

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.