I'm trying to run a second query using the results of my first query to set a flag for all the rows that were just retrieved. The first time I use my result to output JSON it works fine, but the second time I try to call fetch_assoc() from it, the array comes back NULL.
$result = API::$connection->query($query);
if (!mysqli_error(API::$connection))
{
API::setHeader(200);
// return json encoded results
/// This works just fine, outputs JSON
echo JSONizer::jsonEncodeSqlResultWithKeys( $result, array (
'pId',
'sender',
'receiver',
'content',
'notified',
'opened',
'time' ) );
// initial update query
$query = 'UPDATE messages
SET received=1
WHERE ';
// start by cycling through each pId received and tack each onto query
$counter = 0;
// this never runs because the associated array returns NULL now
while ($row = mysqli_fetch_assoc($result));
{
$pId = $row['pId'];
// concat query
$query .= 'pId='.$pId;
// if there's still more to go, tack on OR
if ($counter++ != count($result))
{
$query .= ' OR';
}
else
{
$query .= ';';
}
}
I've tried object and procedural style on calling fetch_assoc, did not make a difference. I tried copying the result into another variable to use, but that was NULL as well. Why would this only work once, but not a second time.
Related
I have been trying to loop this query but all I get is the first value.
When I do the same command in workbench I get all the values.
What am I doing wrong here? Any answers are much appreciated!
global $db;
$stmt12 = $db->query('SELECT `Value` FROM overriddenpropertyvalues WHERE ParentGUID LIKE "' . $itemguid . '";');
$propertyvaluerow = $stmt12->fetch();
while ($propertyvaluerow != null) {
You are fetching only one value with ->fetch(). That's why you receive only one value.
Example from here:
$query = $db->prepare('SELECT `Value` FROM `overriddenpropertyvalues` WHERE ParentGUID LIKE :like');
$query->execute([':like' => $itemguid]);
$stmt->bind_result($value);
while ($query->fetch()) {
echo $value."<br/>"
}
There might possibly be a loophole in your Program depending on what the $stmt12->fetch() does. If it fetches an Array of Data, your while loop might not behave as expected. Below is a commented alternative to what you might want to try out:
<?php
global $db;
$stmt12 = $db->query('SELECT `Value` FROM overriddenpropertyvalues WHERE ParentGUID LIKE "' . $itemguid . '";');
// ASSUMES YOU ARE USING PDO SO WE FETCH ALL THE DATA
$propertyvaluerow = $stmt12->fetchAll();
// THERE MIGHT BE A LOOPHOLE IN YOUR PROGRAM DEPENDING ON WHAT $stmt12->fetch() IS AND DOES
// ASSUMING IT FETCHES AN ARRAY OF NESTED OBJECTS OR SCALAR VALUES, THE WHILE LOOP WOULD NOT BEHAVE AS EXPECTED.
// THAT MEANS IF IT IS AN ARRAY YOU COULD USE A DIFFERENT CONSTRUCT LIKE THE ONE BELOW YOUR WHILE CONSTRUCT:
/* while ($propertyvaluerow != null) { */
// CREATE A $count VARIABLE TO HOLD THE INCREMENTAL COUNT THROUGH THE ITERATION:
$count = count($propertyvaluerow);
while($count > 0){
// DO YOUR WORK HERE
//DECREMENT THE VALUE OF COUNT OTHERWISE YOU MAY HAVE AN INFINITE LOOP TO DEAL WITH.
$count--;
}
?>
There is yet another alternative:
<?php
// OR EVEN A MUCH MORE EASIER WAY IS TO USE THE FOREACH LOOP, WHICH ACHIEVES THE SAME THING AS THE WHILE LOOP:
foreach($propertyvaluerow as $iKey=>$objData){
// SIMPLY USE THE $objData IN WITHING THE LOOP
// THE $objData IS THE VALUE OF THE CURRENT OBJECT IN THE $propertyvaluerow IN THE ITERATION
}
Best way:
global $db;
$stmt = $db->prepare('SELECT `Value` FROM OverriddenPropertyValues WHERE ParentGUID=?');
$stmt->execute([$itemguid]);
$rows = $stmt->fetchAll();
foreach($rows as $row) {
// $row->Value or $row['Value']
}
I made a function in a CI model that first queries for the table to get its fields(because these fields will change dynamically over time,so I can't hard code a list of field names),and then when it gets the results of the first query, and builds a fields name list,it queries the table again to get the values belonging to one row or record.It then stores the second query result in an array,which is passed back to the controller.Here is the complete function that performs these steps:
public function getAssetFeatures($as_id)
{
$data = array();
//this sql query gets the field names from the table I want to query.
$sql = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '".DATABASE."' AND TABLE_NAME = 'as_features';";
$query = $this->db->query($sql);
$count = 0;
foreach($query->result_array() as $k)
{
foreach($k as $kk=>$v)
{
if( $v != "as_features_id" &&
$v != "as_id" &&
$v != "cid" &&
$v != "lot_size" )
{
$features_array[$count] = $v;
$count++;
}
}
}
$features_string = implode(",",$features_array);
//I got the field names, put them into an array, then concatenated them into a string, which I will use for the fields in the next query:
$sql = "SELECT $features_string FROM as_features WHERE as_id='$as_id'";
$query = $this->db->query($sql);
//mandatory rooms/features:
foreach($query->result() as $row)
{
foreach($row as $k=>$v)
{
$data["$k"] = $v; //build an associative array with the values of each field for the one row I am querying
}
}
return $data; // return the associative array.
}
At first I thought something was broken in my table or view,but as I kept repeating the same call to the model function by refreshing the page and entering the exact same values,I noticed that sometimes the code worked,and I wouldn't get the errors "undefined index".
So I outputted the results of the array with this code:
echo "<pre>";
print_r($asset['features']);
echo "</pre>";
...and the expected output, which only performs successfully sometimes, but not all the time, for the exact same operation using the exact same parameters, looks like this:
Array
(
[kitchen] => 1
[liv_area] => 0
[dining] => 1
[family] => 0
[bed] => 0
[bath] => 1
[half_bath] => 0
[parking] => 0
[car_storage] => 0
[pool] => 0
[miscellaneous] => 0
)
When the query returns a result set and then a populated array,my form works and looks normal.but,most of the time the query fails,and I get what looks like this:
The issue is with the following snippet of code:
foreach($query->result() as $row)
{
foreach($row as $k=>$v)
{
$data["$k"] = $v; //build an associative array with the values of each field for the one row I am querying
}
}
The way it works is that for every result which is returned it will overwrite $data with the relevant keys. However, because the $query->result() will return the row you want, then return false, the array essentially ends up being:
$data[] = false; i.e., set the whole array to false/empty.
If you change the loop to be, the code inside the loop won't be executed for the false value of $query->result():
while ($row = $query->result())
{
// your code
}
It's worth noting that if you're intending to return more that one row from this, it won't work as it will just overwrite the existing values.
Ok, the problem was my code, not the query. I was passing an encrypted as_id, but neglected to unencrypt it before constructing the select query. That's why it would work sometimes, and not others. For some reason MySQL allowed the encrypted as_id to match existing as_id of INT type. After performing the decrypt, the query results became predictable. This is something I wouldn't expect SO members to have divined. Thanks.
public function getAssetFeatures($as_id)
{
$as_id = $this->utilityclass->decryption9($as_id); // <- I had this commented out. In fact, I removed the line from my example code in the original post, because I didn't think it was important to the question. Not only was it important, it was the problem!
$data = array();
$sql = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '".DATABASE."' AND TABLE_NAME = 'as_features';";
$query = $this->db->query($sql);
I'm trying to use implode() in a script to return the result of an SQL query as a string so I can insert it into another table, however whenever I manage to get the implode to return anything it will only return a single result, even though the query returns more than one result.
Note: my PHP is not the strongest and I am using pre-existing code and reworking it, which is why a lot of the code will look like it's meant for a JSON API.
$rows = $stmt->fetchAll();
if ($rows) {
$response["success"] = 1;
$response["message"] = "Events Scheduled!";
$response["events"] = array();
foreach ($rows as $row) {
$post = array();
$post["id"] = $row["id"];
$post["message"] = $row["message"];
$post["pin"] = $row["pin"];
array_push($response["events"], $post);
$matstring=implode("', '",$post);
}
}
When echo'd out I get:
3', 'Test to check multiple entries are in array for when the check is made, this should be seen.', '12345
imploding $response['events'] returns "Array, Array" and everything else I have tried returns nothing. Where should I look to get the other entry?
I think what are you want is like this:
$str = "'".implode(",'", $post)."'";
echo $str;
$sql = mysql_query("SELECT * FROM table WHERE user='$user'" );
while($data=mysql_fetch_array($sql)){
echo $data['user']; //this is fine
}
echo $data['user'];
$data=mysql_fetch_array($sql);
echo $data['user'];
The last two echos are empty?
I need to have an array outside of the loop that is equivalent to the 'last' loop cycle.
The while loop keeps fetching data, until there is no more data and therefore false is returned by mysql_fetch_array. This last value, false, is still in $data after the loop has ended, but simply printing it using echo, won't print anything you can see. The same goes for the next call of mysql_fetch_array and echo. You can check this by doing a var_dump of the $data variable. This will show you that it contains the boolean false.
If you want to be able to use the data after the loop has ended, you can save all the data you fetch into one big array. That might be a bad option though if you're fetching a lot of data, but from there you should be able to change it yourself so you can save and use the useful data. To store all the data in an array, change the loop into this:
$alldata = array();
while($data=mysql_fetch_array($sql))
{
echo $data['user'];
$alldata[] = $data;
}
You can then for example iterate over $alldata to go over the results again.
Update: In your last comment you said you want to access the last record that was fetched. You can do that like this:
$lastdata = array();
while($data=mysql_fetch_array($sql))
{
echo $data['user'];
$lastdata = $data;
}
$lastdata will then contain the last record in your result.
Because of the following:
while($data = mysql_fetch_array($sql)) {
// mysql_fetch_array returned something 'not false' and this value
// is assigned to $daat
}
// mysql_fetch_array() returned false, because there are no more rows
// false is assigned to $data, and because the statement within while(...)
// isn't true anymore the loop is stopped.
Use
$data = mysql_fetch_array($sql);
if (!$data) {
echo "User don't exists";
}
for example to handle this situation
I have a typical database query:
$query = mysql_query('SELECT titulo,referencia FROM cursos WHERE tipo=1 AND estado=1');
and I can convert it in an array and print the data:
while ($results=mysql_fetch_array($query)): ?>
echo $results['referencia'];
// and so...
endwhile;
but in some cases I need to print the same data in another part of the web page, but the $results array seems to be empty (I use var_dump($results) and I get bool(false)). I plan to use what I learned reading Create PHP array from MySQL column, but not supposed to mysql_fetch_array() creates an array? So, what happen?
Tae, the reason that your $result is false at the end of the while loop is that mysql_fetch_array returns false when it reaches the end of the query set. (See the PHP Docs on the subject) When you reach the end of the query set $results is set to false and the while loop is exited. If you want to save the arrays (database row results) for later, then do as Chacha102 suggests and store each row as it is pulled from the database.
$data = array();
while($results = mysql_fetch_array($query)) {
$data[] = $results;
}
foreach ($data as $result_row) {
echo $result_row['referencia'];
... etc.
}
Try this
while($results = mysql_fetch_array($query))
{
$data[] = $results;
}
Now, all of your results are in $data, and you can do whatever you want from there.
As Anthony said, you might want to make sure that data is actually being retrieved from the query. Check if any results are being returned by echo mysql_num_rows($query). That should give you the number of rows you got