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

Query and ajax are required in the while loop

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-09-09 08:28:01849browse

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>

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>

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>

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>

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>

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>

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>
Statement:
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