SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member
    Join Date
    Apr 2003
    Location
    sydney
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    arrays and mysql query

    I am trying to perform a sql query based on the variables in the array $empl which have been submitted by a dynamic multiple select form on another page.

    The array information is passed correctly and echoed as it should, but the query will not execute giving me ae error:

    "Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in /var/www/emplyeestest1.php

    The code is shown below, any help with it would really be appreciated:

    $empl=$_POST['employees'];

    echo "You selected ".count($empl)." employees. Their names arebr />";
    for($i=0;$i<count($empl);$i++)
    {
    echo $empl[$i]."<br />";
    }

    $dbcnx = mysql_connect('localhost', 'root', 'abcedfgh');
    mysql_select_db('haec');

    $employees = mysql_query("select Employee from employees where id=$empl");
    while ($employee = mysql_fetch_array($employees)) {

    $row = htmlspecialchars($employee['Employee']);
    echo ('<p>' . $row['Employee'] . '</p>');
    }

  2. #2
    My precious!!! astericks's Avatar
    Join Date
    Mar 2002
    Location
    Vancouver, BC
    Posts
    1,971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $employees = mysql_query("select Employee from employees where id= '$empl' " );

    I just put single quotes for $empli

    try it, not sure if that's the prob. I dont see any extra or missing - dots or ; or stuff that might cause parse errors.

  3. #3
    SitePoint Member
    Join Date
    Apr 2003
    Location
    sydney
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by astericks
    $employees = mysql_query("select Employee from employees where id= '$empl' " );

    I just put single quotes for $empli

    try it, not sure if that's the prob. I dont see any extra or missing - dots or ; or stuff that might cause parse errors.
    I tried what you suggested, it fixed the parse problem, but those variables in $empl array still will not operate the query - anymore suggestions??

  4. #4
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,347
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    what you want is the IN clause

    ... where id in ( 23, 54, 67 )

    this will still work even if only one id is selected

    if id is a character field, then each value would have to be in quotes, but it's probably not, eh

    rudy

  5. #5
    SitePoint Member
    Join Date
    Apr 2003
    Location
    sydney
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I cant work out where you got ( 23, 54, 67 ) from Rudy, I have also tried using the :

    foreach($empl as $value) {
    echo "$value <BR>\n";
    }

    this "$value" will pass only one array variable and return one id query, no good if we need to return multiple id queries at the same time. For example give all the employees with selected id's, but at least it seems to be doing something - any suggestions how to make it work properly??

    seno

  6. #6
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,347
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    those were an example to show the IN syntax

    the values come from the SELECT MULTIPLE as passed in the form

    i don't do php but i think the comma-separated list of values will be available in $_POST['employees']

    so just stuff that into your query

    rudy

  7. #7
    SitePoint Member
    Join Date
    Jun 2003
    Location
    The Netherlands
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Seno,
    Did u figure it out yet?
    I am searching through this forum in hope to find a solution for a problem like yours.
    If possible could you show me the correct code so I can better idea of what you did?

    Thanks,

    Wheezy

  8. #8
    SitePoint Member
    Join Date
    Apr 2003
    Location
    sydney
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Wheezy
    Seno,
    Did u figure it out yet?
    I am searching through this forum in hope to find a solution for a problem like yours.
    If possible could you show me the correct code so I can better idea of what you did?

    Thanks,

    Wheezy
    Yeah, i worked out a solution, but i had to use query for each array element.

    I used the if(in_array()); function to find out if the array element was present in the array. If it was, it executed the query in the if(in_array()){query}.

    From the result of that query and others (multiple elements - multiple queries), i arranged a new array consisting of all the query results.

    Then this new array was passed through the foreach loop as shown:

    foreach($NewArray as $value) {
    if ($value==""){
    echo"";
    }else{
    echo " <br><font color=#0000FF>" . $value . "</font>";
    }
    }

    That worked for me.

    Seno


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •