0

I came across an old function that I would like to convert to mysqli but I'm stuck at the mysql_result ($result, 0, $colname['i]) part; would it be best to use data/field_seek and if so, what would be the best method?

function friend_data($q, $colname)
    {
            $result = mysql_query ($q);
            if (mysql_num_rows($result) > 0)
            {
                    $colname = explode("|", $colname);
                    for ($i = 0; $i < count($colname); $i++)
                    {
                            global $$colname[$i];
                            $$colname[$i] = mysql_result ($result, 0, $colname[$i]);
                    }
            }
    }

The way this is used right now:

$q = "SELECT first_name,last_name,user_email FROM users WHERE country = '".$country."';

and calling the function friend_data, supplied with the column names:

friend_data ($q, "first_name|last_name|user_email");

the variables ($first_name,$last_name,$user_email) can now be used in the rest of the code which I would like maintain in that format like below:

   function friend_data($q, $colname)
    {
            $result = $db->query ($q);

            if (mysqli_num_rows($result) > 0)
            {
                    $colname = explode("|", $colname);
                    for ($i = 0; $i < count($colname); $i++)
                    {
                            global $$colname[$i];
                            $$colname[$i] = ... ;
                    }
            }
    }

So mysql_result ($result, 0, $colname[$i]) specifically I don't know how to "translate" into working mysqli code.

Any help/tips/pointers on how to proceed would be greatly appreciated!

Willy Fox
  • 3
  • 2
  • 1
    This should do the trick: http://php.net/manual/de/mysqli-stmt.bind-result.php – Jeff Jan 13 '17 at 01:28
  • @barmar why is this marked as duplicate? I've seen the result posted by Barmar BEFORE I posted my question. Oh well. – Willy Fox Jan 13 '17 at 03:21
  • @WillyFox Do the solutions in that question not work? – Barmar Jan 13 '17 at 03:28
  • @barmar array dereferencing works in response to the OPs question; not sure how that applies to the contents of my question - I honestly wish I knew or I would not be asking. – Willy Fox Jan 13 '17 at 03:36
  • You asked how to convert `mysql_result()` to `mysqli`. There are lots of answers to that question that show exactly how to do that. – Barmar Jan 13 '17 at 03:40
  • Actually, I wonder why this function needs to use `mysql_result` in the first place. Just fetch the row as an associative array and access the elements. – Barmar Jan 13 '17 at 03:43
  • @barmar I'll think of a clever way to change the title of my post, but my question still remains - not duplicate. Thanks for clearing that up. – Willy Fox Jan 13 '17 at 03:43
  • @barmar I like the row > []. That's what I ended up doing and used list to access them. Thank you! – Willy Fox Jan 13 '17 at 03:47

1 Answers1

2

This is old, old, old school coding. Basically, they're doing the work manually that PHP does for you automatically.

We're going to have to fix a lot here. First, mysqli does NOT use a global connector like mysql_query does. You'll have to pass that into the function as argument #3. Don't use global if you can help it. Next, we're going to return an array. They way he did it was clunky.

function friend_data($q, $cols, $connection) {
     $data = []; // New array
     $col = explode('|', $cols);
     foreach($col as $col_row) $data[$col_row] = [];
     $result = $connection->query($q);

     if(!$result->num_rows) return $data; // No rows, so no data

     while($row = $result->fetch_assoc()) {
          foreach($row as $key => $data_col) $data[$key][] = $data_col;
     }
     return $data;
}

Once you call your function, you can do the following to get the vars back out

$data = friend_data ($q, "first_name|last_name|user_email", $connection);
list($first_name, $last_name, $user_email) = $data;

This is much cleaner and easier to work with going forward

miken32
  • 35,483
  • 13
  • 81
  • 108
Machavity
  • 28,730
  • 25
  • 78
  • 91
  • thank you very much! Yes, it was an old, old, old school app I put together - playing in the big sandbox for a few years and coming back to code made me realize how much has changed! While this does not work as is, it did give me a new perspective on how to tackle the problem, so THANK YOU! – Willy Fox Jan 13 '17 at 03:02