Using arrays with sql queries help

Discussion in 'PHP' started by passingTime, Jul 14, 2012.

  1. #1
    I am wondering if there is any way to use arrays in sql queries effectively.

    So for example (this is not real code, just an idea):

    $array =
    0 => PHP,
    2 => SQL;

    SELECT ($array) FROM Table1;

    which would output:

    SELECT PHP, SQL FROM Table1;

    I would love to know of any good practices for doing this sort of thing as i have been trying my best to follow the standards for PHP and SQL coding.
     
    Solved! View solution.
    passingTime, Jul 14, 2012 IP
  2. BRUm

    BRUm Well-Known Member

    Messages:
    3,086
    Likes Received:
    61
    Best Answers:
    1
    Trophy Points:
    100
    #2
    Are you referring to stored procedures in a database? That's easily done and I think usually executed with eval().

    Or are you referring to arrays stored in a databse? To do this serialisation is needed and done with the serialize() function, however unless in specific circumstances this defeats the object of a relational database.
     
    BRUm, Jul 14, 2012 IP
  3. #3
    The only way is to convert them to string... at least for table names or field names on select.

    $query='SELECT ',implode(',',$array),' FROM Table1';

    If you are talking about value sets for things like WHERE however, PDO (as opposed to the outdated mysql_ functions people REALLY need to stop using) has something called prepared queries, and PDO (unlike it's cousin mysqli_) lets you pass an array to it's execute method.

    
    $db->prepare('
      SELECT * FROM table1
      LIMIT :start, :max
    ');
    $db->execute(array(
      ':start' => $start,
      ':end' => $end
    ));
    
    Code (markup):
    Unfortunately (or perhaps fortunately from a security standpoing) does not allow you to pass values to SELECT or FROM...

    Because PDO allows for multiple SQL engines, I've taken to storing my queries in a array of strings (private scope to a inherited class of PDO object) -- loading a different array for each engine... then using prep/exec to pass the values.
     
    deathshadow, Jul 14, 2012 IP
  4. passingTime

    passingTime Peon

    Messages:
    49
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thank you a lot for your answers BRUm and deathshadow this is what i was looking for.

    My question has been answered thank you but i will also look into PDOs. Thanks a lot for this information.

    Much love.
     
    passingTime, Jul 14, 2012 IP
  5. BRUm

    BRUm Well-Known Member

    Messages:
    3,086
    Likes Received:
    61
    Best Answers:
    1
    Trophy Points:
    100
    #5
    Interesting stuff Deathshadow. Tell me, is PDO for PHP what LINQ or ADO.NET are for C#?

    I read that try/catch should be used with PDO but I've never used these in PHP, only C#. I think it's good practice to program in a way that doesn't require them, unless it's absolutely necessary.
     
    Last edited: Jul 14, 2012
    BRUm, Jul 14, 2012 IP