Jump to content

PHP Mysqli making the results from SELECT * into an array


james909

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);
}
}
}

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.