I am trying to insert an array value into a sql SELECT statement.

When I try to assign the selected value or an array (from a form
SELECT option/value array) I put in this code:

foreach ($_GET['sort'] as $val){

 $sql = "SELECT * FROM `new_mowers` WHERE '$val' = '%  $userquery%' LIMIT 0, 30";

}

I am using Homesite 5.0 to develop and it does not highlight foreach as it does for any other PHP construct.

I thought the problem may be a glitch in Homesite but when I ran the code anyway the page returned blank. If I take this code out and hardcode the query everything is fine?

Why is Homesite and PHP not recognizing foreach?

    Test using this code:

    <?php
    $array = array("hello", "world", "of", "php");
    foreach ($array as $value) {
    	echo $value . " ";
    }
    echo "!";
    ?>

      the for each loop itself looks fine,

      you say it displays a blank page, this could be because your not even querying the database, all you are doing is declaring the variable $sql as a string. You'll need to run it through mysql_query to display any query results.

      also try putting error_reporting(E_ALL); at the top of the script in the hope it force php to show an error if your php.ini is configured not to show them.

        Thanks laserlight..I will try that but has to wait til Monday.

        To Jonno946... I have error_reporting(E_all); set at the top of script but did not report anything.

        I am picking up the vars $sort[] array and $userquery from a submitted form. I have been echoing the $sql string to the browser to see what variables are being passed and my only problem is iterating though the array to find the value of the key.

        As I said, when the query is hardcoded my query results are fetched from the database and printed to the browser.

        I am going to try laserlight's suggestion just to see if foreach will work with a supplied array in the script.

        Thanks guys!

          I tried the code and it worked. I was able to get it sort for the value of the array and now have that part working. Thanks.

          I still have some sql issues but that is for another forum. '

          Thanks for the assist.

            Write a Reply...