2012-07-20 34 views
0

這是我的php文件中的一個函數,它用於提供我的android應用程序的請求。PHP文件無法輸入代碼的一部分

function checkin($DB, $TechID, $ClientID, $SiteID){ 
    $dbConnection = mysql_connect($DB['server'], $DB['loginName'], $DB['password']); 
    if(!$dbConnection){ 
     die('Error! ' . mysql_error()); 
     } 
    mysql_select_db($DB['database'], $dbConnection); 

    $file2 = "C:/wamp/www/file2.txt"; 
    $data2 = "ClientID:".$ClientID." TechID:".$TechID." SiteID:".$SiteID; 
    file_put_contents($file2, $data2); 

    $result1 = mysql_query("SELECT COUNT(*) FROM Log") or die('Error! ' . mysql_error()); 
    $query = "SELECT `Type` FROM `Log` WHERE `TechID` = '".$TechID."' ORDER BY LogTime DESC LIMIT 1"; 
    $file5 = "C:/wamp/www/file5.txt"; 
    file_put_contents($file5, $query); 
    $result2 = mysql_query($query) or die('Error! ' . mysql_error()); 
    while($row1 = mysql_fetch_array($result1)){ 
     $count = $row1['COUNT(*)']; 
     $file3 = "C:/wamp/www/file3.txt"; 
     $data3 = "ClientID:".$ClientID." TechID:".$TechID." SiteID:".$SiteID." Count:".$count; 
     file_put_contents($file3, $data3); 
     while($row2 = mysql_fetch_array($result2)){ 
      $file4 = "C:/wamp/www/file4.txt"; 
      $data3 = "ClientID:".$ClientID." TechID:".$TechID." SiteID:".$SiteID." Count:".$count; 
      file_put_contents($file4, $data3); 
      /*if($row2['Type']!="Checkin"){ 
       $count = $count+1; 
       $Time = date('Y/m/d H:i'); 
       mysql_query("INSERT INTO Log (LogID, TechID, ClientID, SiteID, LogTime, Type) 
          VALUES (".$count.", ".$TechID.", ".$ClientID.", ".$SiteID.", ".$Time.", Checkin)"); 
      }else{ 
       $query2 = "SELECT TechEmail FROM Tech WHERE TechID=".$TechID; 
       $result3 = mysql_query($query2) or die('Error! ' . mysql_error()); 
       $subject = "Please check out"; 
       $message = "You have forgot to logout from the last site, please check out manually"; 
       $from = "[email protected]"; 
       $header = "Form:".$from; 
       while($row3 = mysql_fetch_array($result3)){ 
        mail($row3['TechEmail'], $subject, $message, $header); 
       } 
      }*/ 
     } 
    } 
} 

你可以看到,我已經隱藏了一些代碼,因爲我調試它,我創建一些文件,只是爲了看看,不能執行哪些代碼的一部分。我發現程序不能進入要創建file4的區域。我已經找出可能問題來自$ query,當它執行時,有時候mysql會迴應「未知表格狀態:TABLE_TYPE」,我不明白爲什麼。

+1

您的功能太大,嵌套太多。劃分並征服它:創建多個函數並使主函數調用子例程。您可以更好地處理錯誤和調試。代替代碼也容易得多。 – hakre 2012-07-20 08:24:03

+0

這聽起來像你的information_schema中可能有一些重複。 – Kao 2012-07-20 08:26:04

+0

但它不應該是問題的原因 – Conrad 2012-07-20 08:27:39

回答

2

寫在註釋以上,你應該分而治之,使您的生活更輕鬆(尤其是你寫的代碼,而你在這大功能發揮與它周圍)。這確實工作一樣容易:

function file_put($number, $data) 
{ 
    $path = sprintf("C:/temp/wamp/www/file%d.txt", $number); 
    file_put_contents($path, $data); 
} 

例如,這只是替換,你只需要很多重複線(編號)文件把一些字符串

但你也可以做到這一點用。更復雜的東西,比如數據庫操作。您可能希望將錯誤處理移出視野,並在需要時小心地連接到數據庫,並採用更靈活的方式來獲取數據。這可以通過將(輕度棄用的)mysql_*函數移動到其自己的一個或兩個類中來完成,以便它離開你的視線。這將使它的使用更容易(這是我第一次展示):

// Create your database object to use it later on: 
$config = array(
    'server' => 'localhost', 
    'name' => 'root', 
    'password' => '', 
    'db' => 'test', 
); 
$db = new MySql($config); 

我叫數據庫類MySql,因爲它代表了MySQL連接,並將其與舊版本的MySQL擴建工程。您只需要將該數據庫對象傳遞到問題的函數中。與file_put功能相結合,應該是這樣的:

function checkin(MySql $DB, $TechID, $ClientID, $SiteID) 
{ 
    $query = sprintf("SELECT `Type` FROM `Log` WHERE `TechID` = '%d' ORDER BY LogTime DESC LIMIT 1", $TechID); 
    file_put(5, $query); 

    $result1 = $DB->query("SELECT COUNT(*) FROM Log");  
    $result2 = $DB->query($query); 

    foreach ($result1 as $row1) { 
     list($count) = $row1; 
     $data = "ClientID:$ClientID TechID:$TechID SiteID:$SiteID Count:$count" 
     file_put(3, $data); 
     foreach ($result2 as $row2) { 
      file_put(4, $data); 
     } 
    } 
} 

仍然checkin功能是緊挨是大(代碼已經12行),但比你的第一個版本要短得多,因爲它代表的工作編寫文件和訪問數據庫。我希望這個演示很有用。以下是完整的代碼示例:

/** 
* MySql Exception 
*/ 
class MySqlException extends RuntimeException 
{ 
} 

/** 
* MySql Database Class 
*/ 
class MySql 
{ 
    private $server; 
    private $name; 
    private $password; 
    private $db; 
    private $connection; 

    public function __construct(array $config) 
    { 
     $this->server = $config['server']; 
     $this->name = $config['name']; 
     $this->password = $config['password']; 
     $this->db = $config['db']; 
    } 

    private function connect($server, $name, $password) 
    { 
     $this->connection = mysql_connect($server, $name, $password); 
     if (!$this->connection) { 
      $this->error("Unable to connect to '%s' as user '%s'", $server, $name); 
     } 
    } 

    private function select($db) 
    { 
     if (!mysql_select_db($db, $this->connection)) { 
      $this->error("Unable to select database '%s'", $db); 
     } 
    } 

    private function close() 
    { 
     $this->connection && mysql_close($this->connection); 
    } 

    private function connectSelect() 
    { 
     $this->connect($this->server, $this->name, $this->password); 
     $this->select($this->db); 
    } 

    /** 
    * @param $query 
    * @return MySqlResult 
    */ 
    public function query($query) 
    { 
     $this->connection || $this->connectSelect(); 
     $result = mysql_query($query, $this->connection); 
     if (!$result) { 
      $this->error("Unable to execute query '%s'", $query); 
     } 
     return new MySqlResult($result); 
    } 

    /** 
    * @param string $format 
    * @param ... 
    * @throws MySqlException 
    */ 
    private function error($format) 
    { 
     $args = func_get_args(); 
     array_shift($args); 
     $format .= ': %s'; 
     $args[] = $this->connection ? mysql_error($this->connection) : mysql_error(); 
     throw new MySqlException(vsprintf($format, $args)); 
    } 

    public function __destruct() 
    { 
     $this->close(); 
    } 
} 

/** 
* MySql Result Set - Array Based 
*/ 
class MySqlResult implements Iterator, Countable 
{ 
    private $result; 
    private $index = 0; 
    private $current; 

    public function __construct($result) 
    { 
     $this->result = $result; 
    } 

    public function fetch($result_type = MYSQL_BOTH) 
    { 
     $this->current = mysql_fetch_array($this->result, $result_type); 
     return $this->current; 
    } 

    /** 
    * Return the current element 
    * @link http://php.net/manual/en/iterator.current.php 
    * @return array 
    */ 
    public function current() 
    { 
     return $this->current; 
    } 

    public function next() 
    { 
     $this->current && $this->fetch(); 
    } 

    /** 
    * Return the key of the current element 
    * @link http://php.net/manual/en/iterator.key.php 
    * @return mixed scalar on success, or null on failure. 
    */ 
    public function key() 
    { 
     return $this->current ? $this->index : null; 
    } 

    /** 
    * Checks if current position is valid 
    * @link http://php.net/manual/en/iterator.valid.php 
    * @return boolean The return value will be casted to boolean and then evaluated. 
    * Returns true on success or false on failure. 
    */ 
    public function valid() 
    { 
     return (bool)$this->current; 
    } 

    /** 
    * Rewind the Iterator to the first element 
    * @link http://php.net/manual/en/iterator.rewind.php 
    * @return void Any returned value is ignored. 
    */ 
    public function rewind() 
    { 
     $this->fetch(); 
    } 

    /** 
    * Count of rows. 
    * 
    * @link http://php.net/manual/en/countable.count.php 
    * @return int The count of rows as an integer. 
    */ 
    public function count() 
    { 
     return mysql_num_rows($this->result); 
    } 
} 

// Create your database object to use it later on: 
$config = array(
    'server' => 'localhost', 
    'name' => 'root', 
    'password' => '', 
    'db' => 'test', 
); 
$db = new MySql($config); 

function file_put($number, $data) 
{ 
    $path = sprintf("C:/temp/wamp/www/file%d.txt", $number); 
    file_put_contents($path, $data); 
} 

function checkin(MySql $DB, $TechID, $ClientID, $SiteID) 
{ 
    $query = sprintf("SELECT `Type` FROM `Log` WHERE `TechID` = '%d' ORDER BY LogTime DESC LIMIT 1", $TechID); 
    file_put(5, $query); 

    $result1 = $DB->query("SELECT COUNT(*) FROM Log");  
    $result2 = $DB->query($query); 

    foreach ($result1 as $row1) { 
     list($count) = $row1; 
     $data = "ClientID:$ClientID TechID:$TechID SiteID:$SiteID Count:$count"; 
     file_put(3, $data); 
     foreach ($result2 as $row2) { 
      file_put(4, $data); 
     } 
    } 
} 

checkin($db, 1, 2, 3, 4); 
+1

另請參閱:[如何成功地重寫舊的mysql-php代碼與不推薦的mysql_ *函數?](http://stackoverflow.com/q/10919277/367456) - 將這些舊的函數移動到數據庫類允許您稍後在一箇中心位置替換它們,而您的代碼庫的其他部分已經得到改進。 – hakre 2012-07-20 13:48:50

1

簡單的回答應該是:

mysql_query("INSERT INTO Log (LogID, TechID, ClientID, SiteID, LogTime, Type) 
         VALUES (".$count.", ".$TechID.", ".$ClientID.", ".$SiteID.", ".$Time.", Checkin)"); 

從$瓦爾刪除引號。或單引號。也刪除點。像:

mysql_query("INSERT INTO Log (LogID, TechID, ClientID, SiteID, LogTime, Type) 
         VALUES ('$count', '$TechID', '$ClientID', '$SiteID', '$Time', Checkin)"); 
+0

我已經成功解決了這個問題,但是謝謝你爲我提供了一個替代解決方案 – Conrad 2012-07-20 10:10:45

+0

不客氣!快樂編碼:) – 2012-07-20 10:11:21