james909 Posted May 2, 2013 Share Posted May 2, 2013 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] Quote Link to comment https://forums.phpfreaks.com/topic/277533-php-mysqli-making-the-results-from-select-into-an-array/ Share on other sites More sharing options...
trq Posted May 2, 2013 Share Posted May 2, 2013 See http://php.net/manual/en/mysqli-stmt.get-result.php Quote Link to comment https://forums.phpfreaks.com/topic/277533-php-mysqli-making-the-results-from-select-into-an-array/#findComment-1427708 Share on other sites More sharing options...
james909 Posted May 2, 2013 Author Share Posted May 2, 2013 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() Quote Link to comment https://forums.phpfreaks.com/topic/277533-php-mysqli-making-the-results-from-select-into-an-array/#findComment-1427711 Share on other sites More sharing options...
trq Posted May 2, 2013 Share Posted May 2, 2013 The error makes little sense. It definitely exists. Quote Link to comment https://forums.phpfreaks.com/topic/277533-php-mysqli-making-the-results-from-select-into-an-array/#findComment-1427712 Share on other sites More sharing options...
james909 Posted May 2, 2013 Author Share Posted May 2, 2013 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? Quote Link to comment https://forums.phpfreaks.com/topic/277533-php-mysqli-making-the-results-from-select-into-an-array/#findComment-1427716 Share on other sites More sharing options...
Barand Posted May 2, 2013 Share Posted May 2, 2013 (edited) What PHP version are you using. mysqli_stmt::get_result mysqli_stmt_get_result(PHP 5 >= 5.3.0) Edited May 2, 2013 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/277533-php-mysqli-making-the-results-from-select-into-an-array/#findComment-1427717 Share on other sites More sharing options...
InoBB Posted May 2, 2013 Share Posted May 2, 2013 (edited) 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 May 2, 2013 by InoBB Quote Link to comment https://forums.phpfreaks.com/topic/277533-php-mysqli-making-the-results-from-select-into-an-array/#findComment-1427786 Share on other sites More sharing options...
james909 Posted May 2, 2013 Author Share Posted May 2, 2013 thank inoBB it is returning this error: Warning: mysqli_stmt::bind_result() [mysqli-stmt.bind-result]: Number of bind variables doesn't match number of fields in prepared statement and the array values are all: NULL Quote Link to comment https://forums.phpfreaks.com/topic/277533-php-mysqli-making-the-results-from-select-into-an-array/#findComment-1427810 Share on other sites More sharing options...
Solution mac_gyver Posted May 2, 2013 Solution Share Posted May 2, 2013 (edited) 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 May 2, 2013 by mac_gyver Quote Link to comment https://forums.phpfreaks.com/topic/277533-php-mysqli-making-the-results-from-select-into-an-array/#findComment-1427817 Share on other sites More sharing options...
james909 Posted May 2, 2013 Author Share Posted May 2, 2013 fixed it! thank you for all the help trq, barand, inobb, mac_gyver. great help on phpfreaks as always Quote Link to comment https://forums.phpfreaks.com/topic/277533-php-mysqli-making-the-results-from-select-into-an-array/#findComment-1427831 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.