Returning Multiple Rows with MySQLi and Arrays

Returning Multiple Rows with MySqli and Arrays

You have to use a loop to get them all at once:

<?php
function resultToArray($result) {
$rows = array();
while($row = $result->fetch_assoc()) {
$rows[] = $row;
}
return $rows;
}

// Usage
$query = 'SELECT DISTINCT $fields FROM `posts` WHERE `profile_user_id` = $user_id LIMIT 4';
$result = $mysqli->query($query);
$rows = resultToArray($result);
var_dump($rows); // Array of rows
$result->free();

PHP MySQLi mysqli_fetch_array Returns multiple arrays

Have you tried this?

while($rows             = mysqli_fetch_array($get_db_filenames, MYSQLI_NUM)) {
$array_rows[] = $rows[0];
}

echo "<pre>";
print_r($array_rows);
echo "</pre>";

Return multiple values from a function using mysqli_fetch_assoc

The db_select function returns a full array it looks like, so likely you are wanting to access those two variables like so:

function db_select($sql){  
$rows = array();
$result = db_query($sql);
if(!$result){
return false;
}

// This function could potentially return multiple rows...
while($array = mysqli_fetch_assoc($result)){
// This will return multiple rows
$row[] = $array;
}

// This will return only one row
// (but there may be more that you are missing)
// $row = mysqli_fetch_assoc($result)

// Notice change here
return $row;
}

if (isset($_POST['submit'])){
$rows = db_select("SELECT C_ID,C_Fname FROM Customer WHERE C_Email='$Email'")

// If you have the one row option you echo like so
// echo $rows['C_ID'];
// echo $rows['C_Fname'];

// For multiple row option, do a foreach or if you know the key you can access it directly
foreach($rows as $arrays) {
echo $arrays['C_ID'];
echo $arrays['C_Fname'];
}

}

How to return multiple rows from a table in in php using mysql

Try this,

 while($row_data = mysql_fetch_array($row_data))
{
$row_player_name = $row_data['column_name_player'];
$row_team = $row_data['column_team'];
echo $row_player_name;
echo $row_team;
}

column_name_player is the column name of table which contains player name.

fetching multiple row in mysqli

The native mysqli->fetch() function will return the next row in the resultset until it reaches the end.

Just call it once if you just want to get the next row, or you can include it in a loop if you want to get all rows, for example

while($row = $this->result->fetch())
{
//Do something with $row
}

Edit: This relates to fetching results for prepared statements only, and would only work where $this->result held the result of store_result() after execution of a prepared statement.

Returning array and num rows mysqli prepared

According to the PHP docs, mysqli_stmt_get_result returns FALSE on error:

Returns a resultset for successful SELECT queries, or FALSE for other DML queries or on failure. The mysqli_errno() function can be used to distinguish between the two types of failure.

You're then passing that into mysqli_fetch_assoc which complains because you're giving it a bool instead of the resultset it expects.

Do a little more erroring checking at that point and you'll be fine. There's probably something wrong with your SQL query. Call mysqli_errorno to determine if there's an error, as the docs state above.

EDIT:

Use the mysqli_error function to get a description of the mysql error. It would be best to use this everywhere you're checking for failure as having an error message will make debugging much easier than simply failing silently.

Retrieve Multiple Rows from mysql

For that you need two arrays. Most logical is to store each row in one array and than store this array into another like:

$revs = array();
$allrevs = array();

while(mysqli_stmt_fetch($statement)){
$revs["item_name"] = $item_name;
$revs["username"] = $username;
$revs["review"] = $review;
$allrevs[] = $revs;
}

echo json_encode($allrevs);


Related Topics



Leave a reply



Submit