Home > Backend Development > PHP Tutorial > Query and ajax are required in the while loop

Query and ajax are required in the while loop

WBOY
Release: 2016-09-09 08:28:01
Original
822 people have browsed it

I would like to ask if your writing method is the same as mine?
Suppose I want to use loop to retrieve data

<code>while ($row=mysql_fetch_array($sql)){ 
資料顯示...
}
</code>
Copy after login
Copy after login

But suppose I need to loop the $row[id] in the data to do other things
I have to put mysql_query into the while so that I can know the $row[id] of each transaction
Like this

<code>while ($row=mysql_fetch_array($gettimeline)){
$sql2 = mysql_query("SELECT * FROM `xxx`
WHERE `id` = '".$row['id']."'
");
}
</code>
Copy after login
Copy after login

Then after each piece of data is retrieved
won’t he repeat the $sql2 query for each piece of data?
Is this the right way to write it? The same goes for ajax. Suppose I want to delete an article. In order to get the article id, I still have to put it into a while loop

<code>while ($row=mysql_fetch_array($gettimeline)){

$sql2 = mysql_query("SELECT * FROM `xxx`
WHERE `id` = '".$row['id']."'
");

<script>
                        $("#remove_timeline_<? echo $row['timeline_id'];?>").click(function(){
                                $.ajax({
                                    type: "POST",
                                    url:"remove?to=<? echo $row['timeline_id'];?>",
                                    cache: false,
                                    success: function(){    
                                    
                                    }
                                });
                            }
                        });
                        </script>
                        }
</code>
Copy after login
Copy after login

In this way, each stroke will have repeated ajax and query. The more strokes, the slower it will be...What would you do?


Reply content:

I would like to ask if your writing method is the same as mine?

Suppose I want to use loop to retrieve data

<code>while ($row=mysql_fetch_array($sql)){ 
資料顯示...
}
</code>
Copy after login
Copy after login

But suppose I need to loop the $row[id] in the data to do other thingsI have to put mysql_query into the while so that I can know the $row[id] of each transaction

Like this

<code>while ($row=mysql_fetch_array($gettimeline)){
$sql2 = mysql_query("SELECT * FROM `xxx`
WHERE `id` = '".$row['id']."'
");
}
</code>
Copy after login
Copy after login

Then after each piece of data is retrievedwon’t he repeat the $sql2 query for each piece of data?

Is this the right way to write it? The same goes for ajax. Suppose I want to delete an article. In order to get the article id, I still have to put it into a while loop

<code>while ($row=mysql_fetch_array($gettimeline)){

$sql2 = mysql_query("SELECT * FROM `xxx`
WHERE `id` = '".$row['id']."'
");

<script>
                        $("#remove_timeline_<? echo $row['timeline_id'];?>").click(function(){
                                $.ajax({
                                    type: "POST",
                                    url:"remove?to=<? echo $row['timeline_id'];?>",
                                    cache: false,
                                    success: function(){    
                                    
                                    }
                                });
                            }
                        });
                        </script>
                        }
</code>
Copy after login
Copy after login

In this way, each stroke will have repeated ajax and query. The more strokes, the slower it will be...
What would you do?

"Cycle" means "circulation", right?

When the number of data items increases, the number of requests will also increase. Generally, such repeated and similar external IO will be considered for merging.

For example:

<code class="sql">SELECT * FROM `xxx` WHERE id in (1,2,3,4,5)</code>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template