Jump to content

PHP Mysqli making the results from SELECT * into an array


Go to solution Solved by mac_gyver,

Recommended Posts

here is my code for selecting one row by the ID:

$mysqli = new mysqli("localhost", "levelDbUser", "honey", "levelDb");

$outpoststatement = $_GET['level'];

$statement = $mysqli->prepare("SELECT * FROM users WHERE 'id' =  ?");

$statement->bind_param("i",$outpoststatement);

$statement->execute();

Now how to I get the results from this row into an array of the columns:

$array[user_type]

$array[username]

$array[average_value]
$array[total_value]
$array[description]

thank you trq for the link, i have made a loop to get the results into an array, this is my code:

$mysqli = new mysqli("localhost", "levelDbUser", "honey", "levelDb");

$outpoststatement = $_GET['level'];

$statement = $mysqli->prepare("SELECT * FROM users WHERE 'id' =  ?");

$statement->bind_param("i",$outpoststatement);

$statement->execute();

$level_array = array('user_type','username','average_value','total_value','description');

    foreach($level_array as $level)
    {
        $statement->execute();
        $result = $statement->get_result();
        while ($column = $result->fetch_array(MYSQLI_NUM))
        {
            foreach ($column as $c)
            {
               $array[$column] = $c;
            }
        
        }
    }

and it is returning this error: Fatal error: Call to undefined method mysqli_stmt::get_result()

i done some researching and i think the problem is this method was introduced in php 5.3 and my host is running PHP Version 5.2.17

 

is there a known method for getting the results from the database select * into an array in this version of PHP?

You can get your results into an array by manually setting them into the array after your query:


$mysqli = new MySQLi('localhost', 'levelDbUser', 'honey', 'levelDB');

if (!$connection) {
echo "Database connection failed:" . mysqli_errno();
exit();
}
if (isset($_GET['level'])) {

$outpoststatement = $_GET['level'];

$sql = "SELECT * FROM users WHERE id = ?";
$statement = $mysqli->prepare($sql);
$statement->bind_param("s", $outpoststatement);
$statement->bind_result($usertype, $username, $average_value, $total_value, $description);
$statement->execute();
$statement->store_result();
while ($statement->fetch()) {
$level_array = array(
'user_type' => $usertype,
'username' => $username,
'average_value' => $average_value,
'total_value' => $total_value,
'description' => $description
);
foreach ($level_array as $level) {
//run script with your newly formed array(s)
var_dump($level);
}
}
}
Edited by InoBB
  • Solution

you must bind a variable to each field that is selected. by using SELECT *, no one knows just by looking at the query how many fields there actually are and there are apparently more than the 5 fields that were listed.

 

you need to SELECT the actual fields that you want in the query so that you will have the information in front of you to bind all the selected fields.

Edited by mac_gyver
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.