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!

8
  • 1
    This should do the trick: php.net/manual/de/mysqli-stmt.bind-result.php Commented Jan 13, 2017 at 1:28
  • @barmar why is this marked as duplicate? I've seen the result posted by Barmar BEFORE I posted my question. Oh well. Commented Jan 13, 2017 at 3:21
  • @WillyFox Do the solutions in that question not work? Commented Jan 13, 2017 at 3: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. Commented Jan 13, 2017 at 3: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. Commented Jan 13, 2017 at 3:40

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.