1

我有一个网页每 20 秒自动刷新一次,以检查新电子邮件和其他一些信息。

一个文件automate.php 有<?php include ... ?>大约6 个文件。

在包含的所有 6 个文件中,它们运行 SQL 查询,但如果两个通过automat.php 文件同时运行,有时它们会尝试一起运行。

例如,如果include1.phpinclude2.php两者同时运行查询

include1.php:
($sql="QUERY HERE";)

include2.php:
($sql="QUERY HERE";)

他们都是,$sql所以他们不知道区别。

阻止这种情况发生的最好方法是什么?

4

2 回答 2

0

This is the way I'd do it:

  1. Turn your includes into classes (Models)

    class Model1 {
        function getBlah($param) {
            // Do sql here.
        }
    
        // other functions here.
    }
    
  2. Then call an SQL method from each class in the order you want.

    $obj = new Model1();
    echo $obj->getBlah('Horse');
    
于 2013-07-25T10:14:04.470 回答
0

it's not a problem that they run simultaneously (which I'm not sure they do in this case).

If you have a website that has a form that inputs some information into a database each time it's filled out, if two people fill out the form at exactly the same time does this break the website?

EDIT: if I've misunderstood this and you actually meant that query 2 is using the query stored in the first $sql , it doesn't because you've redefined it for the second query, and re defined it again for query 3 / 4 / 5 etc. IE:

//query 1
$sql = 'SELECT * FROM `test`'; 
//rest of query 

//query 2    
$sql = 'SELECT count(*) FROM `test2`;

//$sql no longer = 'SELECT * FROM test' as you've just redefined it as 'SELECT count(*) FROM test2;

于 2013-07-25T10:03:19.993 回答