Result is empty after while loop - php

Why is this not possible? Is there some way I can make the resolut not to be empty?
$sqlAllInfo = "SELECT item1, item2 FROM example";
$resAllInfo = mysql_query($sqlAllInfo);
while($rowAllInfo = mysql_fetch_assoc($resAllInfo)){
echo $rowAllInfo['item1'];
}
$rowAllInfo = mysql_fetch_assoc($resAllInfo);
echo $rowAllinfo['item2'];
Thanks for your time

No, the result will always be empty after your while loop, because the while loop extracts all value from the result resource..the while loop continues looping while there is still information to extract, and will finish when it is empty or when an error occurs
if you ment to get the last entry
$rowAllInfo still contains the last entry ;)

You are looping through the result set using while until there are no more results left. The while loop exits when mysql_fetch_assoc returns false, because there are no more results. Calling mysql_fetch_assoc again still means that there are no more results.

This line:
while($rowAllInfo = mysql_fetch_assoc($resAllInfo)){
assigns a new value to rowAllInfo and afterwards checks whether it is still "true"-ish (as "true" as PHP can be ;-) )
Now, after the last row is fetched, mysql_fetch_assoc() will return false, which is then assigned to $rowAllInfo. As $rowAllInfo is now "false", the loop will not execute anymore, but - look - it's too late! You Variable already has the value false assigned to it.
Even after that, you call mysql_fetch_assoc() once again. But, as you have already fetched all rows within your loop, no more rows are left, and once again $rowAllInfo is set to "false".
So, whatever you are trying to do - this is probably not your way. A common way to achieve what I understand you are trying to do is the following:
$allRows = array();
while( $row = mysql_fetch_assoc($res) ) {
$allRows[] = $row;
}
// show the array we just created...
echo print_r( $arrRows, 1 );

Related

PHP in_array not working properly?

I'm working on in_array() method. If the value read is already in the array it should be skipped and proceed to the next value. If the current value is not on the array yet, it should be pushed to the array.
here's my code:
while ($Result_Data_2 = mysqli_fetch_array($Result)){ //130 rows from database
$Res_Array = array();
$SQL_Result_Time = $Result_Data_2['Interpretation_Time'];
/* Some statements here */
if(in_array($SQL_Result_Time, $Res_Array, true)){
break;
}
else{
array_push($Res_Array, $Number, $SQL_Questionnaire_ID, $SQL_User_ID, $SQL_Psychology_FirstName, $SQL_Psychology_LastName, $SQL_Result_Date, $SQL_Result_Time);
}
echo "<pre>";print_r($Res_Array);echo "</pre>";
}
Problem: It seems that it ignores my condition which is if(in_array($SQL_Result_Time, $Res_Array, true)){break; } and still inserts the value into the array. It still duplicates data
Question:
How to prevent the duplication data where if the current value was found inside the array it would just skip the statement and proceed to another value for checking the array and so on?
Is my logic on checking the value on the array is right?
You are re-initialising your array on every iteration of the while loop. You should declare it outside of the loop:
$Res_Array = array();
while ($Result_Data_2 = mysqli_fetch_array($Result)){ //130 rows from database
$SQL_Result_Time = $Result_Data_2['Interpretation_Time'];
/* Some statements here */
if(in_array($SQL_Result_Time, $Res_Array, true)){
break;
}
else{
array_push($Res_Array, $Number, $SQL_Questionnaire_ID, $SQL_User_ID, $SQL_Psychology_FirstName, $SQL_Psychology_LastName, $SQL_Result_Date, $SQL_Result_Time);
}
echo "<pre>";print_r($Res_Array);echo "</pre>";
}
Also, as mentioned by Marvin Fischer in his answer, your break statement will terminate the while loop on the first duplicated value. You should instead use continue
while ($Result_Data_2 = mysqli_fetch_array($Result)){ //130 rows from database
...
if(in_array($SQL_Result_Time, $Res_Array, true)){
continue;
}
....
}
This question should clarify any issues you have with break and continue statements
First of all, inside of a loop you should use continue, otherwise you cancel the whole loop, secondly you empty $Res_Array at the beginning of every loop purging the old data, inserting the new one and echoing it again

how does the while loop iterate assoc array

how does
while($result=mysqli_fetch_assoc($query)){ echo $result['name'];}
iterate all the rows in the table, i mean it should keep printing the first row only how does it print all rows i think i am fetching only the array of first row
Take an example of this loop
$i=0;
while($i<5)
{
// do something
$i++;
}
That loop will only stop when your condition ($i<5) results in FALSE
When you do
while($result=mysqli_fetch_assoc($query))
This loop willl also keep working as long as your fetch returns a row, which is not FALSE. The only different thing happening here is that you are not doing the increment yourself, when mysqli_fetch_assoc fetches a row it automatically increments the internal pointer of that result set to the next row so when you call the same function again you get the next row and when there are no more rows left it will return NULL which means $result=NULL; which will result in FALSE and you will exit the loop.

PHP While loop only echoes once

this is the PHP code i have:
while($row1 = mysql_fetch_object($query1)) {
echo "*".$row1->id."*";
while ($row2 = mysql_fetch_object($query2)) {
echo "%".$row2->id."%";
}
}
I want it to output for example: *1*%1%%2%%3%%4%*2*%1%%2%%3%%4%*3*%1%%2%%3%%4%
But what this loop outputs is: *1*%1%%2%%3%%4%*2**3*
(It only outputs the $row2 values in the first loop of $row1.
How can I fix this? Thanks.
A few things to note about mysql_fetch_object(). From the PHP document:
Warning: This extension is deprecated as of PHP 5.5.0
Please note above.
returns an object with properties that correspond to the fetched row and moves the internal data pointer ahead.
Please note what I bolded.
The $result object (in your code this would be $query2) is an iterative object that has a pointer pointing to the current item.
Current Item
|
v
[1][2][3][4]
When your first loop hits your second loop, it iterates over the whole thing, such that at the end, the object now looks something like this:
Current Item
|
v
[1][2][3][4]
For each iteration of the first loop, after the first time, the mysql_fetch_object() function basically goes like this:
mysql_fetch_object() ~
1. Get the next time
2. Uh, there are no more objects because we're at item [4]. Return done.
So, how do you get it to work? You could simply save the results into an array and then iterate over that array or you can reset the pointer with mysql_data_seek() (which is also deprecated as of 5.5).
To reset the data pointer, it would be something like this:
while($row1 = mysql_fetch_object($query1)) {
echo "*".$row1->id."*";
while ($row2 = mysql_fetch_object($query2)) {
echo "%".$row2->id."%";
}
// put the result pointer back to the front
mysql_data_seek($query2, 0)
}
Note1, This SO question/answer helped me find the function to use to reset the pointer.
Note, the downside is that you're calling a function that's creating an object, which creates processing overhead every time it runs.
The other option would be save the results into an array and just loop through the array every time:
$secondary_result = array();
while ($row2 = mysql_fetch_object($query2)) {
$secondary_result[] = $row2;
}
while($row1 = mysql_fetch_object($query1)) {
echo "*".$row1->id."*";
foreach($secondary_result as $row2) {
echo "%".$row2->id."%";
}
}
Note, this method will be creating extra memory usage of storing the objects in an array, but it would save on CPU processing as you're not re-creating the objects over and over again, as well as calling a function.
If you just print output, you can consider just saving the result first. No matter now many times you loop over $secondary_result, the final result will always be the same (as per your code, the first loop shows no signs of being directly influencing the second result).
In that case, this makes much more sense
$buffer = '';
while ($row2 = mysql_fetch_object($query2)) {
$buffer .= "%".$row2->id."%";
}
while($row1 = mysql_fetch_object($query1)) {
echo "*".$row1->id."*";
echo $buffer;
}
but I really don't know why you'd do that. If you're doing a nested loop, usually it's because the result of the first loop is affecting the second loop.
But I hope that helps!
Cheers!
EDIT
Per #Blazemonger's comment about looking ahead, the PDO equivalent would be: MySqli:Fetch-Object
When you have a result object from using the PDO function, you would loop like this:
while($row1 = $query1->fetch_object()) {
echo "*".$row1->id."*";
while ($row2 = $query1->fetch_object()) {
echo "%".$row2->id."%";
}
// put the result pointer back to the front
$query2->data_seek(0);
}
The above example shows both the fetch Object and pointer reset versions of MySqli.
The problem is that once you iterate fully through $query2, that's the end of your results. The next time through your $row1 loop, you're still at the end of $query2 and have no results left. Try using mysql_data_seek to go back to the start of your results:
while($row1 = mysql_fetch_object($query1)) {
echo "*".$row1->id."*";
mysql_data_seek($query2, 0);
while ($row2 = mysql_fetch_object($query2)) {
echo "%".$row2->id."%";
}
}
if you really need to repeat your second query data many times, get it into array first, and loop over this array as many times as you need.
First of all: The mysql_* functions are deprecated and will be removed in PHP 5.5. Consider using mysqli or PDO instead.
That being said; back to your question:
Each result set contains an internal pointer to one of the records in the result set. Initially, this pointer points to the first record, and is advanced with each call to mysql_fetch_object.
After your first inner loop, the internal pointer of the $query2 result set will already be at the end of the list, so subsequent calls to mysql_fetch_object will only return FALSE.
If your inner query depends on values from $row1, you will need to re-execute the second query within your outer loop. Otherwise, you can reset the result pointer with mysql_data_seek.
Perhaps you're not getting anything when you call $row2 = mysql_fetch_object($query2) which would give you the output you're getting.
After the first loop there are no more results for query2 to return. So the while is false in each additional loop. You would want to reset the query with mysqli_data_seek or storing all the data in a separate array and looping through that instead.
Please also note:
Per the documentation http://us2.php.net/manual/en/function.mysql-fetch-object.php
The mysql extension is deprecated as of PHP 5.5.0, and will be removed
in the future. Instead, the MySQLi or PDO_MySQL extension should be
used

PHP Array from MySQL is not empty but has an empty entry - how to avoid

I am trying to check whether an entry exists (one or more) in our database. However, even when I know there are no entries, I am getting an array which has a first entry of zero. Therefore it is not empty and I am not getting what I need.
Here's my code:
<?php
$query = mysql_query("SELECT * FROM table WHERE column = $yfbid_number AND timestamp BETWEEN (NOW()- Interval 1 DAY) AND NOW()");
$array[] = array();
while ($row = mysql_fetch_assoc($query)){
$array[] = $row['column'];
}
?>
When doing print_r on an array which should be empty, I am getting: ( [0] => Array ( ) ) and therefore count is 1 and not zero, which messes up my code. Any ideas how to get to a truly empty array in this situation?
I'd rather not delete this entry but avoid it in the first place, because in most use cases I will get either an empty array or one that only has one entry (a real entry), in which case I will want to easily distinguish between the two. (as it is now, both give a count of 1 entry, which is very bad for our porpuses). Thanks.
Change:
$array[] = array();
to
$array = array();
With your version, if $array doesn't already exist, PHP will first create an array, then append an empty array to it. So you end up with a 1 element array whose only member is an empty array.
$array[] = array(); should be $array = array(). Right now, you are appending an array element to an array that is initialized. Turn notices on and you'll get a complaint about an undefined variable (probably).
I suggest you first do a SELECT COUNT(*) to determine how many entries you'll get. Then you KNOW that the result will be a useful answer, and can make or not make a subsequent query on the basis of your result.

Why you should not use mysql_fetch_assoc more than 1 time?

Some people say you should not use mysql_fetch_assoc more than one time, why is that?
e.g.: I want to display two tables one with users who paid for membership, other with users who did not, so instead of querying database 2 times I query it one time and get $result variable with both types of users then I run loop mysql_fetch_assoc and see if list['membership'] = 'paid' then echo ...
Second time I loop loop mysql_fetch_assoc and see if list['membership'] = 'free' then echo ...
What uses less resources considering I got about equal amount of users who registered and unregistered.
Think of your query result set as a sausage, and mysql_fetch_assoc() as a knife that slices off a piece of that sausage. Every time you fetch a row, another piece of sausage is cut off, and it's always a NEW piece of sausage. You can't go and cut off a previously cut piece, because it's been eaten already.
Quote by Typer85 (link):
Please be advised that the resource result that you pass to this function can be thought of as being passed by reference because a resource is simply a pointer to a memory location.
Because of this, you can not loop through a resource result twice in the same script before resetting the pointer back to the start position.
For example:
<?php
// Assume We Already Queried Our Database.
// Loop Through Result Set.
while( $queryContent = mysql_fetch_row( $queryResult ) {
// Display.
echo $queryContent[ 0 ];
}
// We looped through the resource result already so the
// the pointer is no longer pointing at any rows.
// If we decide to loop through the same resource result
// again, the function will always return false because it
// will assume there are no more rows.
// So the following code, if executed after the previous code
// segment will not work.
while( $queryContent = mysql_fetch_row( $queryResult ) {
// Display.
echo $queryContent[ 0 ];
}
// Because $queryContent is now equal to FALSE, the loop
// will not be entered.
?>
The only solution to this is to reset the pointer to make it point at the first row again before the second code segment, so now the complete code will look as follows:
<?php
// Assume We Already Queried Our Database.
// Loop Through Result Set.
while( $queryContent = mysql_fetch_row( $queryResult ) {
// Display.
echo $queryContent[ 0 ];
}
// Reset Our Pointer.
mysql_data_seek( $queryResult );
// Loop Again.
while( $queryContent = mysql_fetch_row( $queryResult ) {
// Display.
echo $queryContent[ 0 ];
}
?>
Of course you would have to do extra checks to make sure that the number of rows in the result is not 0 or else mysql_data_seek itself will return false and an error will be raised.
Also please note that this applies to all functions that fetch result sets, including mysql_fetch_row, mysql_fetch_assos, and mysql_fetch_array.
When someone says you can't call mysql_fetch_assoc() twice, they mean against the same resource. The resource result you pass in to mysql_fetch_assoc() is done by reference. You'll need to reset the position of the pointer before you can use mysql_fetch_assoc() a second time.
EDIT: And to do so, try using mysql_data_seek().
It appears that what you want to do is to treat the query result as an array (rows) of arrays(fields). But that's not really what the mysql library provides. What I will often do is in fact copy the rows into an array of arrays (just loop on mysql_fetch until empty) and then do what I want with my own rowset using array functions that PHP provides for the purpose. This also minimizes the amount of time the table is locked.

Categories