i want to save every result from an sql statement in a differen array. I tried:
$sql = "SELECT * FROM `link_server` WHERE `in_use` = false;";
$sql .= "SELECT * FROM `link_queue` WHERE `active` = false;";
if ($db->multi_query($sql))
{
do
{
// Erstes Abfrageergebnis ausgeben
if ($result = $db->store_result())
{
// Abfrageergebnis ausgeben
while ($server_fetch = $result->fetch_array())
{
$server[] = $server_fetch;
}
$result->close();
}
// Trenner fuer Zweites Abfrageergebnis
if ($db->more_results())
{
echo "<hr />test";
$queue[] = $server_fetch;
}
} while ($db->next_result());
echo "Servers:";
print_r($server);
echo "Queue:";
print_r($queue);
}
The result from the first statement should be saved in the array $server, and the second should be saved in the array $queue.
The above example stores the complete return (from both statements) in the first array ($server). The second array is empty.
How can i solve that?
Thanks for your help!
$db->more_results() is a way of flagging that the end of the results of a query have been reached, not to get the next set of results. You can use it to tell your loop to start loading the next array - for example by setting a flag.
if ($result = $mysqli->store_result()) {
while ($server_fetch = $result->fetch_row()) {
if (!$second) {
$server[] = $server_fetch;
} else {
$queue[] = $server_fetch;
}
}
$result->close();
}
/* next set of results */
if ($mysqli->more_results()) {
$second = true;
}
Alternatively, you could use a variable variable like so:
/* set up variables */
$server = array();
$queue = array();
$active = 'server';
/* execute multi query */
if ($mysqli->multi_query($query)) {
do {
/* store result set */
if ($result = $mysqli->store_result()) {
while ($server_fetch = $result->fetch_row()) {
${$active}[] = $server_fetch;
}
$result->close();
}
/* next set of results */
if ($mysqli->more_results()) {
$active = 'queue';
}
} while ($mysqli->next_result());
}
Although I have to concur with the commentors, this adds a lot of effort and additional code complexity to an otherwise simple pair of requests.
Related
I want to create a function that automatically makes a connection to the database and performs the given queries but I can't get it to work and it gives no errors.
I think I'm not outputting in the correct way my goal is to output a array that stores all the returned values from the queries.
Here is my code so far hope you can help:
public function db_query() {
$ini = parse_ini_file($_SERVER['DOCUMENT_ROOT'] . '/app.ini');
$mysqli = new mysqli($ini['db_location'], $ini['db_user'], $ini['db_password'], $ini['db_name']);
// create string of queries separated by ;
$query = "SELECT name FROM mailbox;";
$query .= "SELECT port FROM mailbox";
// execute query - $result is false if the first query failed
$result = mysqli_multi_query($mysqli, $query);
if ($result) {
do {
// grab the result of the next query
if (($result = mysqli_store_result($mysqli, 0)) === false && mysqli_error($mysqli) != '') {
echo "Query failed: " . mysqli_error($mysqli);
while ($row = $result->fetch_row()) {
echo $row[0];
}
}
} while (mysqli_more_results($mysqli) && mysqli_next_result($mysqli)); // while there are more results
} else {
echo "First query failed..." . mysqli_error($mysqli);
}
}
Note: I did not add the parameter for the query just for testing
purposes.
public function db_query($mysqli) {
$return = [];
$result = mysqli_query($mysqli, "SELECT name FROM mailbox");
while ($row = $result->fetch_row()) {
$return[] = $row[0];
}
$result = mysqli_query($mysqli, "SELECT port FROM mailbox");
while ($row = $result->fetch_row()) {
$return[] = $row[0];
}
return $return;
}
simple, clean, efficient, always works
I use this code to get rows from database.
// Prepare WC statement
$queryUP = $pdo->prepare("SELECT * FROM unitprices WHERE id_quot = :idQuotation");
// Execute Unit prices statement
$queryUP->execute(array(
'idQuotation' => $idQuotation
));
// How to check the results is empty or not ?
if (results) {
// foreach($queryUP as $rowup) {
//...
// }
} else {
// do another thing
}
I don't how to do to check if there is some results in the query before continuing the code ?
$queryUP = $pdo->prepare("SELECT * FROM unitprices WHERE id_quot = ?");
$queryUP->execute(array($idQuotation));
//here you go
$results = $queryUP->fetchAll();
if ($results) {
// foreach($results as $rowup) {
//...
// }
} else {
// do another thing
}
Hope you are doing your foreach in the template, not right in place as shown here.
If your memory allows you, you can fetch all:
$queryUP = $pdo->prepare("SELECT * FROM unitprices WHERE id_quot = :idQuotation");
$queryUP->bindParam(':idQuotation', $idQuotation, PDO::PARAM_STR);
$queryUP->execute();
$result = $queryUP->fetchAll(PDO::FETCH_ASSOC);
if (count($result) > 0) {
} else {
}
I have this php code :
public function getcabang(){
$cabang = $this->conn->prepare("SELECT nama_cabang,alamat FROM cabang");
if ($cabang->execute()){
$cbg = $cabang->get_result()->fetch_assoc();
$cabang->close();
return $cbg;
} else {
return NULL;
}
}
but it always returning 1 row :
{"error":false,"cabang":{"nama_cabang":"Senayan","alamat":"Pintu 1 Senayan"}}
even though it have more than 1 row in table
You need to iterate to get every row, here is an example from php.net :
$query = "SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 50,5";
if ($result = $mysqli->query($query)) {
/* fetch associative array */
while ($row = $result->fetch_assoc()) {
printf ("%s (%s)\n", $row["Name"], $row["CountryCode"]);
}
/* free result set */
$result->free();
}
In your case, that would be :
public function getcabang(){
$cabang = $this->conn->prepare("SELECT nama_cabang,alamat FROM cabang");
if ($cabang->execute()){
$cbg = array();
$result = $cabang->get_result();
while($row = $result->fetch_assoc()) {
$cbg[] = $row;
}
$cabang->close();
return $cbg;
} else {
return NULL;
}
}
Also, a better solution could be to use mysqli_fetch_all (Available only with mysqlnd)
I have some php that gets data from the database, and I just want to get it into my view. I had this code from earlier where I just wanted 1 row, however now I want to get all the data into the view.
Do I have to make a loop and get each row into a string and keep appending to it?
here's the php:
if ($result = $mysqli->query("SELECT * FROM myData")) {
$row_cnt = $result->num_rows;
if ($row_cnt > 0) {
$row = $result->fetch_assoc();
$data = $row["data"];
echo $data;
} else {
echo "no data";
}
/* close result set */
$result->close();
}
Something like this should do it, unless you need it in the code later. If that is the case store it to an array.
while($row = $result->fetch_assoc()) {
echo $row["data"];
//$array[] = $row["data"];
}
Try this way :
if ($result = $mysqli->query("SELECT * FROM myData")) {
$row_cnt = $result->num_rows;
if ($row_cnt > 0) {
While ($row = $result->fetch_assoc()) {
$data .= $row["data"]."<br>";
}
echo $data;
} else {
echo "no data";
}
/* close result set */
$result->close();
}
I am trying to return all selected data from mysql table. What I want is to return data in an array so that I will display that as json data. I acheived following so far but I do not know how ca I return that mysql data.
public function getHome() {
$result = mysql_query("SELECT * FROM places") or die(mysql_error());
// check for result
$no_of_rows = mysql_num_rows($result);
if ($no_of_rows > 0) {
while($row = mysql_fetch_array($result)) {
$data[] = $row;
}
return $data;
} else {
// user not found
return false;
}
}
here where I am calling this method
if($db->getHome()) {
$data = $db->getHome();
$response['success'] = 1;
$response['uid'] = $data['uid'];
$response['name'] = $data['name'];
$response['profile_photo'] = $data['profile_photo_path'];
$response['places']['place_photo'] = $data['place_photo_path'];
$response['places']['created_at'] = $data['created_at'];
echo json_encode($response);
} else {
echo "bye";
}
here is what it echos
{"tag":"home","success":1,"error":0,"uid":null,"name":null,"profile_photo":null,"places":{"place_photo":null,"created_at":null}}
You need to define $data as an array first, besides that your code looks fine.
$data = array();
As you are, potentially, returning multiple rows you should be doing something like:
$data = $db->getHome(); // There's no need to call this twice
if($data) {
foreach($data as $place) {
// Do what you need to do with each place here
}
}
Take a look at to see the contents of your $data print_r($data);