This while loop causes a Fatal error: Maximum execution time of 30 seconds exceeded.
The query IS working. I tested the query in phpmyadmin.
Nothing is inside the loop in order to test whether it's content caused the error.
It looks like the while loop is overloading. It seems that the script takes too long to load (thinking the query is too extended), not seeing any possibilities the loop to be infinite.
while($tags_db = mysqli_fetch_array(mysqli_query($link,
"SELECT *
FROM zk_terms
WHERE parent_id = 1 AND parent_type = 'archive' AND taxonomy = 'tag'"))){
}
You're re-executing the query every iteration, and then mysqli_fetch_array is fetching only the first result each loop. You have to move mysqli_query outside the loop and assign it to a variable.
You need to modify your code:
$query = mysqli_query($link,
"SELECT *
FROM zk_terms
WHERE parent_id = 1 AND parent_type = 'archive' AND taxonomy = 'tag'");
while($tags_db = mysqli_fetch_array($query)){
// your code
}
Place your mysqli_query command outside your while and catch the result in a variable. Then use the variable with mysqli_fetch_array in the while .
otherwise, everytime the while condition is examined, the query is run again
Related
I made my scripts without class. (framework). Now I have changed my mind & I wanna make my scripts with classes. I like it. But there I have a problem.. Infinite Loop While. I didn't take this problem before with without classes..
While Rule;
while ($fetch=$db->fetchObject($db->q("SELECT * FROM auction_house.items ORDER BY id DESC LIMIT ".$min_stuff.",".$max_stuff."")))
{
// etc
}
Class;
public function fetchObject($queryRes)
{
$this->queryRes = $queryRes;
$this->record = mysql_fetch_object($this->queryRes);
return $this->record;
}
When I try to while loop, I am taking infinite loop on page.
How can I solve this problem?
You executing query in each iteration. It always returns a new resource.
Try to separate query and cycle:
$res = $db->q("SELECT * FROM auction_house.items ORDER BY id DESC LIMIT ".$min_stuff.",".$max_stuff."");
while ($fetch=$db->fetchObject($res))
{
// etc
}
Your code is executing on every while loop, so if you keep the $db->q nested there it will execute the query each time it loops, returning for each while iteration the first row of you query result.
try separating your code, like this:
$result = $db->q("SELECT * FROM auction_house.items ORDER BY id DESC LIMIT ".$min_stuff.",".$max_stuff."");
while ($fetch=$db->fetchObject($result)){
// etc
}
this way the $db->q() will only be executed once, as intended, and the while will loop trough its results.
You're starting a new query at each iteration.
You need to store your queryRes:
$queryRes = $db->q("SELECT * FROM auction_house.items ORDER BY id DESC LIMIT ".$min_stuff.",".$max_stuff."");
while ($fetch=$db->fetchObject($queryRes))
{
// etc
}
On an another side, I'm not sure you should store queryRes and record as instance variable of $db.
This is the query I have:
$sqlw = "SELECT * FROM coverages where user_id='3828' ORDER BY sp_id ASC";
$resultw = mysql_query($sqlw);
$roww = mysql_fetch_array($resultw);
while ($roww = mysql_fetch_array($resultw)) {
echo $roww['sp_id']."<br>";
}
echo "TOTAL:".mysql_num_rows($resultw)."<br>";
As you can see its very basic
the results show : TOTAL:29
But when I count the list of the items returned back its only 28.
I ran the query on phpmyadmin it shows a total of 29 rows, I did count them and they are 29.
I ran different other simple queries and it always does the same thing: one row is missing. This could be trivial maybe I am missing something or maybe its server related? any help/ideas would be greatly appreciated. Thank you
Your call to mysql_fetch_array() before the loop disposes of a row.
You have a classic off-by-one error.
There is an extra $roww = mysql_fetch_array($resultw); before your loop starts. This means you're throwing away the first row.
I have two while loops running one after the other (not inside of each other) - I've simplified the code a bit so that only the important parts of it are listed below. The problem arises when I compare the two echoed queries because the 2nd while loop apparently isn't running at all.
I read somewhere that someone got around the problem by using a for loop for the second one but I want to get down to why exactly the second while loop is not running in my code.
$query_work_title = "SELECT title FROM works WHERE ";
while ($row = mysql_fetch_assoc($result_work_id)) {
$query_work_title .= "OR '$work_id' ";
}
echo $query_work_title;
echo '<br />';
$result_work_title = mysql_query($query_work_title) or
die(mysql_error($cn));
// retrieve the authors for each work in the following query
$query_author_id = "SELECT author_id FROM works_and_authors WHERE ";
while ($row = mysql_fetch_assoc($result_work_id)) {
$query_author_id .= "work_id = 'hello' ";
}
echo $query_author_id;
The MySQL extension keeps track of an internal row pointer for each result. It increments this pointer after each call to mysql_fetch_assoc(), and is what allows you to use a while loop without specifying when to stop. If you intend on looping through a result set more than once, you need to reset this internal row pointer back to 0.
To do this, you would mysql_data_seek() after the first loop:
while ($row = mysql_fetch_assoc($result_work_id)) {
$query_work_title .= "OR '$work_id' ";
}
mysql_data_seek($result_work_id, 0);
You've already looped through the result rows, so it's at the end and returns FALSE. (That's why it exited the loop the first time.)
To reset the internal pointer to the beginning of the result set, use mysql_data_seek().
mysql_data_seek($result_work_id, 0);
After the first while() loop completes, the internal pointer in the MySQL result is at the end of itself. You need to tell it to go back to the beginning using mysql_data_seek() between the first and second loops:
mysql_data_seek($result_work_id, 0);
You have already reached the end of your result set, but you can use mysql_data_seek to reset it.
// query your database
$result = mysql_query(...);
// loop through results
while(($row = mysql_fetch_assoc($result))) {
}
// reset result set
mysql_data_seek($result,0);
// loop again
while(($row = mysql_fetch_assoc($result))) {
}
According to your posted code, your SQL will look something like:
SELECT title FROM works WHERE OR '1'
That query will result in an error so your script shouldn't be getting past that point.
Even if it does, your second loop:
while ($row = mysql_fetch_assoc($result_work_id))
is using a result handle that has already been completely iterated by the first loop. By the the time the second loop tries to use it, mysql_fetch_assoc will return FALSE because there are no more rows to fetch. This will cause the second loop to exit immediately.
If both while loops need to access the same rows, combine their logic so the rows only need to be iterated over one time.
mysql_fetch_assoc steps through the results, right? It's already at the end on the second while loop, so it does nothing.
I am trying to query a database, but it seems to just load for an age and not do anything. It's a simple query and shouldnt take longer than a millisecond.
while($row = mysql_fetch_array(getWallPosts($userid)))
{
}
Now when I replace this code with:
echo mysql_num_rows(getWallPosts($userid));
It just displays '1' in fact there's only one record in the DB and it's just a simple SELECT statement.
Here's the getWallPosts function:
function getWallPosts($userid, $limit = "10")
{
$result = dbquery("SELECT * FROM commentpost
WHERE userID = ".$userid."
ORDER BY commentPostID DESC LIMIT 0, ".$limit);
return $result;
}
Also, when I put the SQL string that it's executing into MySQL's query browser. It takes no time at all.
Any ideas?
You appear to be looping indefinitely because you're retrieving a new set (one record) of data each time.
$result = getWallPosts($userid);
while ($row = mysql_fetch_array($result)) {
//printf($row[0]);
}
You need to get the data once and loop through it. Your code is getting the data, running the loop and then getting the data again.
Try:
$posts = getWallPosts($userid);
while($row = mysql_fetch_array($posts))
{
//Code to execute
}
Its an infinite loop. The expression in the while always executes so it will always be true.
You're returning a new result set each time the while statement executes. You should call getWallPosts first, assign it to $results, and then loop over it.
I have a mysql query:
$a=mysql_query("SELECT id FROM table1 WHERE p2='fruit' LIMIT 1");
This will only ever return one result or none.
I'm trying to first count the results, then if it 1, to pass the returned id to a variable.
Question: If I do
$results=count(mysql_fetch_assoc($a));
to count the number of rows returned, can I still do later
while($row = mysql_fetch_array($a)){
$id=$row['id'];
}
or will the first delete the array somehow???
Is their a better way to do all this?
You really not need to do anything if there is one row or null
Consider below code it will set id value if there is 1 row fetched otherwise it will be null
$id=''
while($row = mysql_fetch_array($a)){
$id=$row['id'];
}
No count needed.
$results=count(mysql_fetch_assoc($a));
does not count the number of rows as mysql_fetch_assoc returns one row. I believe you're looking for mysql_num_rows:
$results = mysql_num_rows($a);
$num_rows = mysql_num_rows($a);
You can then do an IF statement on this and later do a while loop on the fetched array
Have you tried mysql_num_rows ??
http://php.net/manual/en/function.mysql-num-rows.php
What happens when using:
while($row = mysql_fetch_array($a)){
$id=$row['id'];
}
it is just like reading a file,every time mysql_fetch_array($a) is called the next line is parsed untill it reaches the end of the file. A cursor of some sort is known in the background. Meaning that if you would enter by hand
//backgroundCursor = 0
$row = mysql_fetch_array($a)
//backgroundCursor = 1
$row = mysql_fetch_array($a)
//backgroundCursor =2
$row = mysql_fetch_array($a)
//backgroundCursor = 3
The while instruction just automates the stuff.
mysql_fetch_array increases the background cursor at every call so if you call it twice it will not give you the same line.
To get a clear view on this read something about reading from a file line by line.
No you cannot as each call to mysql_fetch_assoc() loads a new row, if there is any. You could indeed assign both variables at once:
$results=count($row = mysql_fetch_assoc($a));
but
But mysql_fetch_assoc($a)) returns false and as the result of count(false) is 1 this will not tell you anything. Besides, if there is no row all you really need is $row = mysql_fetch_assoc($a) and test if ($row) {...}.