php - 在 Web 服务器上执行()错误,但在本地服务器上没有错误

标签 php mysql

我的客户网站有一个商店定位器,它使用数据库来获取商店的所有信息。该商店定位器在我的台式机和笔记本电脑本地服务器(本地主机)上完美运行,但是一旦我将所有内容上传到我的测试站点,商店定位器将无法工作。在名为 locate.php 的文件中,我包含一个名为 store.php 的文件/类,当我转到本地服务器上的locate.php 文件时,我只会收到以下通知没什么大不了的。

enter image description here

当我访问我的网站时,出现此错误。

enter image description here

locate.php 中,我还包含一个名为 defines.php 的文件,其中包含访问数据库所需的信息(主机、用户名、密码、数据库名称)。起初我以为这可能是 GoDaddy 的问题,但我使用了 localhost 和我的网站 IP 作为主机,删除并重新上传了数据库,但似乎没有任何效果。

这是我在此线程中提到的文件。

locate.php

include_once("./defines.php");

include_once("./class/store.php");

function vectorLength($x,$y){
    return sqrt(($x*$x)+($y*$y));
}

$R=6378.1;
$count=0;

$total = $_GET['total'];


//$stores[$total];

//$distances[$total];

$lat=$_GET['lat'];
$lng=$_GET['lng'];

$maxdist=0;
$maxid=-1;

//first filter for 100km radius (0.9 degrees on equator = 100km)
$verified_stores = getStores("status=".VERIFIED." AND latitude > '".($lat-0.9)."' AND latitude < '".($lat+0.9)."' AND longitude > '".($lng-0.9)."' AND longitude < '".($lng+0.9)."'",NULL,0,0);

for($i=0;$i<count($verified_stores);$i++){
    $clat = $verified_stores[$i]->latitude;
    $clng = $verified_stores[$i]->longitude;

    $dlat=$lat-$clat;
    $dlng=$lng-$clng;


    //second filter using haversine distance
    $dLat=deg2rad($dlat);
    $dLng=deg2rad($dlng);

    //haversine calculations
    $a = pow(sin($dLat/2),2)+cos(deg2rad($lat)) * cos(deg2rad($clat))*pow(sin($dLng/2),2);

    $c = 2 * asin(sqrt($a));

    $d = $R*$c;

    if($d>$_GET['radius']) continue;

    if($count<$total){
        //keep track of farthest distance
        if($maxdist<$d){
            $maxdist=$d;
            $maxid=$count;
        }

        //insert into list of not full
        $stores[$count]=$verified_stores[$i]->storeID;

        $distances[$count]=$d;
        $count++;
    }
    else{
        //scan distances
        if($d<$maxdist){
            //replace farthest store
            $stores[$maxid]=$verified_stores[$i]->storeID;
            $distances[$maxid]=$d;

            $maxdist=0;

            //find next maxid
            for($j=0;$j<$total;$j++){
                if($maxdist<$distances[$j]){
                    $maxdist=$distances[$j];
                    $maxid=$j;
                }
            }
        }
    }
}

//bubble sort stores

$total=$count;
for($i=0;$i<$total-1;$i++){
    $swapped=false;
    for($j=$i+1;$j<$total;$j++){
        if($distances[$i]>$distances[$j]){
            //swap $i and $j
            $temp=$distances[$i];
            $distances[$i]=$distances[$j];
            $distances[$j]=$temp;

            $temp=$stores[$i];
            $stores[$i]=$stores[$j];
            $stores[$j]=$temp;
            $swapped=true;
        }
    }
}

echo '<?xml version="1.0" encoding="UTF-8"?>';
echo "\n<LOCATIONS>\n";


for($i=0;$i<$total;$i++){
    $store = getStore($stores[$i]);

    $clat=$store->latitude;
    $clng=$store->longitude;

    $dLat=deg2rad($lat-$clat);
    $dLng=deg2rad($lng-$clng);

    //haversine calculations
    $a = pow(sin($dLat/2),2)+cos(deg2rad($lat)) * cos(deg2rad($clat))*pow(sin($dLng/2),2);

    $c = 2 * asin(sqrt($a));

    $d = $R*$c;

    $webpage = $store->webpage;
    if(strlen($webpage)==0) $webpage="<![CDATA[&nbsp;]]>";

    echo "<STORE>\n";
    echo "<DIST>$d</DIST>\n";   
    echo "<NAME><![CDATA[".htmlspecialchars($store->name)."]]></NAME>\n";
    echo "<ADDR>".htmlspecialchars($store->address)."</ADDR>\n";
    echo "<PHONE>".htmlspecialchars($store->phone)."</PHONE>\n";
    echo "<WEBPAGE>$webpage</WEBPAGE>\n";
    echo "<LAT>$clat</LAT>\n";
    echo "<LNG>$clng</LNG>\n";
    echo "</STORE>\n";
}


echo "</LOCATIONS>\n";

?>

store.php

<?php

class Store{
    public $storeID,$name,$contact,$address,$phone,$fax,$email,$webpage,$latitude,$longitude,$status,$created,$updated;

    public function __construct($storeID=NULL,$name=NULL,$contact=NULL,$address=NULL,$phone=NULL,$fax=NULL,$email=NULL,$webpage=NULL,$latitude=0,$longitude=0,$status=0,$created=NULL,$updated=NULL){
        $this->storeID=$storeID;
        $this->name=$name;
        $this->contact=$contact;
        $this->address=$address;
        $this->phone=$phone;
        $this->fax=$fax;
        $this->email=$email;
        $this->webpage=$webpage;
        $this->latitude=$latitude;
        $this->longitude=$longitude;
        $this->status=$status;
        //$this->enabled=$enabled;
        $this->created=$created;
        $this->updated=$updated;
    }
    public function toString(){
        $s="StoreID: ".$this->storeID."<br/>";
        $s.="Name: ".$this->name."<br/>";
        $s.="Contact: ".$this->contact."<br/>";
        $s.="Address: ".$this->address."<br/>";
        $s.="Phone: ".$this->phone."<br/>";
        $s.="Fax: ".$this->fax."<br/>";
        $s.="E-mail: ".$this->email."<br/>";
        $s.="Webpage: ".$this->webpage."<br/>";
        $s.="Latitude: ".$this->latitude."<br/>";
        $s.="Longitude: ".$this->longitude."<br/>";
        $s.="Status: ".$this->status."<br/>";
        $s.="Created: ".$this->created."<br/>";
        $s.="Updated: ".$this->updated."<br/>";
        return $s;
    }
}

function restoreStore($id){updateStoreStatus($id,UNVERIFIED);}
function verifyStore($id){updateStoreStatus($id,VERIFIED);}
function unverifyStore($id){updateStoreStatus($id,UNVERIFIED);}
function trashStore($id){updateStoreStatus($id,RECYCLING_BIN);}
function updateStoreStatus($id,$status){
    $db=new mysqli(DB_HOST, DB_ADMIN, DB_PWORD, DB_NAMES);
    $q=$db->prepare('UPDATE Stores SET status=?, updated=? WHERE storeID=?');
    $date=date("Y-m-d H:i:s");
    $q->bind_param("isi",$status,$date,$id);
    $q->execute();
    $q->close();
    return 1;
}
function emptyStores(){
    $db=new mysqli(DB_HOST, DB_ADMIN, DB_PWORD, DB_NAMES);
    $q=$db->prepare('DELETE FROM Stores WHERE status='.RECYCLING_BIN);
    $q->execute();
    $q->close();    
    return 1;
}
function deleteStore($id){
    $db=new mysqli(DB_HOST, DB_ADMIN, DB_PWORD, DB_NAMES);
    $q=$db->prepare('DELETE FROM Stores WHERE storeID=?');
    $q->bind_param("i",$id);
    $q->execute();
    $q->close();    
    return 1;
}

function insertStore($store){
    $db = new mysqli(DB_HOST, DB_ADMIN, DB_PWORD, DB_NAMES);

    if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }

    $store->created=date("Y-m-d H:i:s");        //overwrite dates
    $store->updated=$store->created;

    if($q = $db->prepare('INSERT INTO Stores (name,contact,address,phone,fax,email,webpage,latitude,longitude,status,created,updated) values (?,?,?,?,?,?,?,?,?,?,?,?)')){
        $q->bind_param("sssssssssiss",
            $store->name,
            $store->contact,
            $store->address,
            $store->phone,
            $store->fax,
            $store->email,
            $store->webpage,
            $store->latitude,
            $store->longitude,
            $store->status,
            $store->created,
            $store->updated);
        $q->execute();
        $q->close();
    }
    else{
        printf($db->errno);
        exit();
    }

    $q=$db->prepare('SELECT storeID FROM Stores WHERE name=? AND created=?');
    $q->bind_param("ss",$store->name,$store->created);
    $q->execute();
    $q->bind_result($sid);
    $q->fetch();
    $q->close();

    if(strlen($sid)>0) return $sid;
    else return ERROR_DEFAULT;
}


//PRE-CONDITION: email, password, billingID, groupID must exist
function updateStore($store){
    $db=new mysqli(DB_HOST, DB_ADMIN, DB_PWORD, DB_NAMES);

    //BEGIN ERROR CHECKING -------------------------------

    //END ERROR CHECKING ---------------------------------

    $store->updated=date("Y-m-d H:i:s");        //overwrite dates

    $q=$db->prepare('UPDATE Stores SET name=?, contact=?, address=?, phone=?, fax=?, email=?, webpage=?, latitude=?, longitude=?, status=?, updated=? WHERE storeID=?');
    $q->bind_param("sssssssssisi", $store->name,$store->contact,$store->address,$store->phone,$store->fax,$store->email,$store->webpage,$store->latitude,$store->longitude, $store->status, $store->updated,$store->storeID);
    $q->execute();
    $q->close();

    $q=$db->prepare('SELECT storeID FROM Stores WHERE name=? AND created=?');
    $q->bind_param("ss",$store->username,$store->created);
    $q->execute();
    $q->bind_result($sid);
    $q->fetch();
    $q->close();

    if(strlen($sid)>0) return $sid;
    else return ERROR_DEFAULT;
}


function getStore($id){
    $db=new mysqli(DB_HOST, DB_ADMIN, DB_PWORD, DB_NAMES);
    $q=$db->prepare('SELECT storeID,name,contact,address,phone,fax,email,webpage,latitude,longitude,status,created,updated FROM Stores WHERE storeID=?');
    $q->bind_param("i",$id);
    $q->execute();
    $q->bind_result($storeID,$name,$contact,$address,$phone,$fax,$email,$webpage,$latitude,$longitude,$status,$created,$updated);
    $s=NULL;
    if($q->fetch()){
        $s=new Store($storeID,$name,$contact,$address,$phone,$fax,$email,$webpage,$latitude,$longitude,$status,$created,$updated);
    }
    $q->close();    
    return $s;
}

function getStoreCount($filter=NULL){
    $db=new mysqli(DB_HOST, DB_ADMIN, DB_PWORD, DB_NAMES);
    $sql='SELECT COUNT(*) FROM Stores';
    if($filter!=NULL) $sql.=" WHERE ".$filter;

    $q=$db->prepare($sql);
    $q->execute();
    $q->bind_result($count);
    $q->fetch();
    $q->close();    
    return $count;
}

function getStores($filter=NULL,$order=NULL,$start=0,$limit=10){
    $db = new mysqli(DB_HOST, DB_ADMIN, DB_PWORD, DB_NAMES);

    $sql='SELECT storeID,name,contact,address,phone,fax,email,webpage,latitude,longitude,status,created,updated FROM Stores';
    if($filter!=NULL) $sql.=" WHERE ".$filter;
    if($order!=NULL) $sql.=" ORDER BY ".$order;
    if($limit>0) $sql.=' LIMIT '.$start.','.$limit;

    $q=$db->prepare($sql);
    $q->execute();
    $q->bind_result($storeID,$name,$contact,$address,$phone,$fax,$email,$webpage,$latitude,$longitude,$status,$created,$updated);
    $s=NULL;
    while($q->fetch()){

        if($s==NULL) $s=array();
        $store=new Store($storeID,$name,$contact,$address,$phone,$fax,$email,$webpage,$latitude,$longitude,$status,$created,$updated);
        $s[]=$store;
    }
    $q->close();    
    return $s;
}

?>

defines.php

<?php
define("CHARSET","utf-8");
define("BASE_URL","http://MYURL/");
define("SITE_NAME","MY SITE Maps Manager");
define("SITE_TITLE","Default Title");
define("GOOGLE_ANALYTICS_TRACKER_ID","");
define("RECAPTCHA_PUBLIC_KEY","");
define("RECAPTCHA_PRIVATE_KEY","");

define("PRIVATE_KEY","");

define("RECYCLING_BIN",-1);
define("DEFAULT_STATUS",0);
define("VERIFIED",1);
define("UNVERIFIED",2);

define("ERROR_DEFAULT",-1);

define("DB_HOST","localhost");
define("DB_ADMIN","GODADDY_DBUSER");
define("DB_PWORD","MYPASSWORD");
define("DB_NAMES","DATABASENAME");
?>

最佳答案

看起来我们已经用完了 mysql[i] 技巧。

输入 PDO:

表名更新为: 商店

<?php

    // THIS IS ESSENTIALLY THE SAME STORE CLASS AS YOU HAD
    // EXCEPT THIS TIME WE CHANGED EVERYTHING CONNECTED WITH mysql[i]
    // IN OTHER WORDS; THIS IS THE PDO EQUIVALENT OF YOUR STORE CLASS:




    /***********************************************/
    /******    BEGIN STORE CLASS DEFINITION   ******/
    /***********************************************/
    class Store{
        public $storeID,$name,$contact,$address,$phone,$fax,$email,$webpage,$latitude,$longitude,$status,$created,$updated;

        //CREATE A STATIC PROPERTY TO HOLD YOUR DB-RESOURCE:
        /**@var PDO $db*/
        protected static $db;

        public function __construct($storeID=NULL,$name=NULL,$contact=NULL,$address=NULL,$phone=NULL,$fax=NULL,$email=NULL,$webpage=NULL,$latitude=0,$longitude=0,$status=0,$created=NULL,$updated=NULL){
            // JUST CALL THE DB CONNECTION METHOD ONCE AND USE THE HANDLE EVERYWHERE IN YOUR CODE LIKE SO:
            self::establishDBConnection();

            $this->storeID      = $storeID;
            $this->name         = $name;
            $this->contact      = $contact;
            $this->address      = $address;
            $this->phone        = $phone;
            $this->fax          = $fax;
            $this->email        = $email;
            $this->webpage      = $webpage;
            $this->latitude     = $latitude;
            $this->longitude    = $longitude;
            $this->status       = $status;
            $this->created      = $created;
            $this->updated      = $updated;
            //$this->enabled=$enabled;
        }

        // TRY PUTTING YOUR DATABASE CONNECTION LOGIC IN ONE METHOD FOR SIMPLICITY:
        public static function establishDBConnection() {
            try {
                if(static::$db){
                    return static::$db;
                }else{
                    static::$db = new PDO('mysql:host='.DB_HOST.';dbname='. DB_NAMES,DB_ADMIN,DB_PWORD);
                    static::$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                    return self::$db;
                }
            } catch (PDOException $e) {
                // IF THERE WAS AN ANY KIND OF CONNECTION ERROR, "DIE IT OUT" TO THE OUTPUT STREAM:
                die($e->getMessage());
            }
        }

        public function toString(){
            $s="StoreID: ".$this->storeID."<br/>";
            $s.="Name: ".$this->name."<br/>";
            $s.="Contact: ".$this->contact."<br/>";
            $s.="Address: ".$this->address."<br/>";
            $s.="Phone: ".$this->phone."<br/>";
            $s.="Fax: ".$this->fax."<br/>";
            $s.="E-mail: ".$this->email."<br/>";
            $s.="Webpage: ".$this->webpage."<br/>";
            $s.="Latitude: ".$this->latitude."<br/>";
            $s.="Longitude: ".$this->longitude."<br/>";
            $s.="Status: ".$this->status."<br/>";
            $s.="Created: ".$this->created."<br/>";
            $s.="Updated: ".$this->updated."<br/>";
            return $s;
        }
    }
    /***********************************************/
    /*******    END STORE CLASS DEFINITION   *******/
    /***********************************************/



    function restoreStore($id){updateStoreStatus($id,UNVERIFIED);}

    function verifyStore($id){updateStoreStatus($id,VERIFIED);}

    function unverifyStore($id){updateStoreStatus($id,UNVERIFIED);}

    function trashStore($id){updateStoreStatus($id,RECYCLING_BIN);}

    function updateStoreStatus($id,$status){
        $db     = Store::establishDBConnection();
        $q      = $db->prepare('UPDATE stores SET status=:status, updated=:updated WHERE storeID=:storeID');
        $date   = date("Y-m-d H:i:s");
        $q->execute(array(
            'status'    => $status,
            'updated'   => $date,
            'storeID'   => $id,
        ));
        $q->execute();
        return 1;
    }

    function emptyStores(){
        $rcBin  = RECYCLING_BIN;
        $db     = Store::establishDBConnection();
        $q      = $db->prepare('DELETE FROM stores WHERE status=:rcBIN');
        $q->bindParam(':rcBIN', $rcBin);
        $q->execute();
        return 1;
    }

    function deleteStore($id){
        $db = Store::establishDBConnection();
        $q=$db->prepare('DELETE FROM stores WHERE storeID=:storeID');
        $q->bindParam(':storeID', $id);
        $q->execute();
        return 1;
    }

    function insertStore($store){
        $db             = Store::establishDBConnection();
        $store->created =date("Y-m-d H:i:s");        //overwrite dates
        $store->updated =$store->created;

        $sql            = 'INSERT INTO stores (name, contact, address, phone, fax, email, webpage, latitude, longitude, status, created, updated) ';
        $sql           .= 'values (:name, :contact, :address, :phone, :phone, :fax, :email, :webpage, :latitude, :longitude, :status, :created, :updated)';

        if($q = $db->prepare($sql)){
            $arrValues  = array(
                "name"          =>$store->name,
                "contact"       =>$store->contact,
                "address"       =>$store->address,
                "phone"         =>$store->phone,
                "fax"           =>$store->fax,
                "email"         =>$store->email,
                "webpage"       =>$store->webpage,
                "latitude"      =>$store->latitude,
                "longitude"     =>$store->longitude,
                "status"        =>$store->status,
                "created"       =>$store->created,
                "updated"       =>$store->updated
        );

            $q->execute($arrValues);
        }
        else{
            printf($db->errorCode());
            exit();
        }

        $q          = $db->prepare('SELECT storeID FROM stores WHERE name=:name AND created=:created');
        $q->bindParam(":name",      $store->name);
        $q->bindParam(":created",   $store->created);
        $objSID     = $q->fetch(PDO::FETCH_OBJ);

        if(strlen($objSID->storeID)>0) return $objSID->storeID;
        else return ERROR_DEFAULT;
    }


    //PRE-CONDITION: email, password, billingID, groupID must exist
    function updateStore($store){
        $db         = Store::establishDBConnection();

        //BEGIN ERROR CHECKING -------------------------------

        //END ERROR CHECKING ---------------------------------

        $store->updated=date("Y-m-d H:i:s");        //overwrite dates

        $sql        = 'UPDATE stores SET name=:name, contact=:contact, address=:address, phone=:phone, fax=:fax, email=:email, webpage=:webpage, latitude=:latitude, longitude=:longitude, status=:status, updated=:updated WHERE storeID=:storeID';
        $q          = $db->prepare($sql);

        $arrValues  = array(
            "name"          =>$store->name,
            "contact"       =>$store->contact,
            "address"       =>$store->address,
            "phone"         =>$store->phone,
            "fax"           =>$store->fax,
            "email"         =>$store->email,
            "webpage"       =>$store->webpage,
            "latitude"      =>$store->latitude,
            "longitude"     =>$store->longitude,
            "status"        =>$store->status,
            "created"       =>$store->created,
            "updated"       =>$store->updated,
            "storeID"       =>$store->storeID
        );

        $q->execute($arrValues);

        $q          = $db->prepare('SELECT storeID FROM stores WHERE name=:name AND created=:created');
        $q->bindParam(":name",      $store->username);
        $q->bindParam(":created",   $store->created);
        $objSID     = $q->fetch(PDO::FETCH_OBJ);

        if(strlen($objSID->storeID)>0) return $objSID->storeID;
        else return ERROR_DEFAULT;
    }

    function getStore($id){
        $db         = Store::establishDBConnection();
        $sql        = 'SELECT storeID, name, contact, address, phone, fax, email, webpagee, latitud, longitude, status, created, updated FROM stores WHERE storeID=:storeID';
        $q          = $db->prepare($sql);
        $q->bindParam(":storeID", $id);
        $q->execute();

        $storeObj   = $q->fetch(PDO::FETCH_OBJ);
        $s          = NULL;

        if($storeObj){
            $s      = new Store($storeObj->storeID, $storeObj->name, $storeObj->contact, $storeObj->address, $storeObj->phone, $storeObj->fax, $storeObj->email, $storeObj->webpage, $storeObj->latitude, $storeObj->longitude, $storeObj->status, $storeObj->created, $storeObj->updated);
        }
        return $s;
    }

    function getStoreCount($filter = NULL){
        $db         = Store::establishDBConnection();
        $sql        = 'SELECT COUNT(*) as count FROM stores';
        if($filter != NULL) $sql .= " WHERE " . $filter;

        $q          = $db->prepare($sql);
        $q->execute();
        $objCount   = $q->fetch(PDO::FETCH_OBJ);
        $count      = $objCount->count;
        return $count;
    }

    function getStores($filter = NULL, $order = NULL, $start = 0, $limit = 10){
        $db         = Store::establishDBConnection();
        $sql        ='SELECT storeID, name, contact, address, phone, fax, email, webpage, latitude, longitude, status, created, updated FROM stores ';
        if($filter != NULL) $sql .= " WHERE "   . $filter;
        if($order  != NULL) $sql .= " ORDER BY ". $order;
        if($limit > 0)      $sql .= ' LIMIT '   . $start .',' . $limit;

        $q          = $db->prepare($sql);
        $q->execute();
        $arrData    = $q->fetch(PDO::FETCH_ASSOC);
        $stores     = array();

        foreach($arrData as $intKey=>$objData){
            $store      = new Store($objData->storeID, $objData->name, $objData->contact, $objData->address, $objData->phone, $objData->fax, $objData->email, $objData->webpage, $objData->latitude, $objData->longitude, $objData->status, $objData->created, $objData->updated);
            $stores[]   = $store;
        }
        return $stores;
    }

?>

关于php - 在 Web 服务器上执行()错误,但在本地服务器上没有错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37179932/

相关文章:

php - 未收到 OkHttp POST 正文

php - 为什么 JavaScript 中的链接路径不需要前置/但 PHP 中的路径需要前置?

php - 移动大记录表mysql的最快方法

python - 如何有效地从 mysql 写入和读取数字(float、numpy)数组?

php - 在php中使用按钮保存功能

php - 使用 group by 函数对多列进行 sum()

php - 内连接查询 PHP 问题 MySQL

php - 为什么mySQL数据导出会卡在中间?

mysql - HTML::TableExtract:如何运行正确的参数 [参见实例]

java - 如果连接列包含有条件的数据,如何在 JPA 中检查