Web Analytics Made Easy -
StatCounter foreach loop with mysql_query(); - CodingForum

Announcement

Collapse
No announcement yet.

foreach loop with mysql_query();

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • foreach loop with mysql_query();

    Hello everyone,

    I usually do this to get all variables from the $_POST super variable

    PHP Code:
    foreach ($_GET as $key=>$value) {$$key $value;} 
    I would like to do the same here:

    PHP Code:
    $query "SELECT * FROM `employees` WHERE `employee_id` = $id ";
        
    $employee_set mysql_query($query);
        while (
    $row mysql_fetch_array($employee_set))
        {
                   foreach (
    $employee_set as $key=>$value) {$$key $value;}
        } 
    Does this work?

  • #2
    Neither piece of code is valid PHP any more.

    You need ${$key} and either mySQLi or PDO database calls.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

    Comment


    • #3
      First code still work on PHP 5, I was able to work around it but I'll stick to the while loop better. Thanks.

      Comment


      • #4
        The mySQLi and PDO cersions were introduced in July 2004 so they work on PHHP 5 too.

        The mysql_ code you have was marked as obsolete to be replaced in PHP 5.5 back in July 2013 - now long dead with PHP 5.6 being the earliest version of PHP that has any support at all.

        ${$key} works in both PHP 5 and 7 whereas $$key only works in PHP 5 - an easy fix.
        Stephen
        Learn Modern JavaScript - http://javascriptexample.net/
        Helping others to solve their computer problem at http://www.felgall.com/

        Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

        Comment


        • #5
          Oh okay, thanks. I'll replace it.

          Comment


          • #6
            More importantly, you should NEVER blindly paste user data into your query string -- that's what we have the prepare/exec model for; NOR should you EVER blindly trust that the keys you get from client side are correct or contain the values you want!!!

            That's what led to a number of forum softwares having massive security gaps that let avatar uploads be exploited by faking a form input to trick it into a field that was blindly called by include()

            The former as others have said was supposed to go the way of the dodo twelve years ago; the latter is just sloppy practice that can/will bite you sooner or later. NEVER blindly trust $_GET or $_POST and NEVER just dump them into variables wholesale.

            Might I ask why you feel the need to make a local (or worse, global) variable out of every piece of information, making extra copies and doubling up your memory footprint for no good reason?

            OH, also, assuming ID's are unique, there should only be one return value, so use "if", not "while".
            Walk the dark path, sleep with angels, call the past for help.
            https://cutcodedown.com
            https://medium.com/@deathshadow

            Comment

            Working...
            X