2013-06-18 64 views
0

我試圖將我的數據庫從MySQL更改爲PDO。這很困難,必須解決很多錯誤。致命錯誤:調用未定義的函數UNIX_TIMESTAMP()在/

我不知道如何解決這個錯誤。

Fatal error: Call to undefined function UNIX_TIMESTAMP() in /... on line 63.

這是有錯誤的部分

function create_album($album_name, $album_description) { 
    global $database; 
    $database->query("INSERT INTO albums(album_id, id, timestamp, name, description) 
     VALUES (':album_id', '".$_SESSION['id']."', 
     UNIX_TIMESTAMP(), ':album_name', ':album_description')", array('$_SESSION[id]' => ':session_id', 
     ':timestamp' => UNIX_TIMESTAMP(), ':album_name' => $album_name, ':album_description' => $album_description));//this is line 63 

    mkdir('uploads/'.$album_name, 0744); 
    mkdir('uploads/thumbs/'.$album_name, 0744); 
} 

爲什麼有一個致命的錯誤,我用unix_timestamp錯在這裏?我該如何解決這個問題?

謝謝。

+0

'UNIX_TIMESTAMP'是一個MySQL的功能,而不是一個PHP的。 –

+4

你可能想要time(); – Orangepill

回答

2

UNIX_TIMESTAMP()不是PHP函數。此外,你已經在SQL中獲得了它,並且SQL字符串中沒有:timestamp參數。所以就輸了':timestamp' => UNIX_TIMESTAMP(),你應該很好。

此外,你的session_id部分是搞砸了。首先,將會話ID直接轉儲到SQL字符串中,然後將其添加到參數數組中,但將值和鍵逆轉。

1

UNIX_TIMESTAMP是一個MySQL函數,不是PHP函數。你不需要綁定一個參數到UNIX_TIMESTAMP,你可以在你的查詢中使用它。

擺脫':timestamp' => UNIX_TIMESTAMP(),這是你的問題。第一個UNIX_TIMESTAMP不是PHP函數,並且:timestamp在您的查詢中不存在。

另外,':session_id'應該是關鍵。

更新:您需要使用prepare/execute而不是query來運行預準備語句。

$stmt = $database->prepare("INSERT INTO albums(id, timestamp, name, description) VALUES (':session_id', UNIX_TIMESTAMP(), ':album_name', ':album_description')"); 
$stmt->execute(array(
    ':session_id' => $_SESSION[id], 
    ':album_name' => $album_name, 
    ':album_description' => $album_description 
)); 

我假設album_id是AUTO_INCREMENT。如果不是,則應將其添加到查詢中。

+0

好的,這對我很有意義,非常感謝。數據庫中現在有數據,但id = 0,album_name是:album_name和album_description是:album_description。那麼數組是不是到達數據庫,或者是否填充的數據沒有到達數組? – Robske

+0

@Robske:當我做出這個答案時,我沒有注意到這一點,你正在做一個準備好的聲明,所以你不能使用'query',你需要執行''。檢查更新的答案。 –

+0

我認爲你的兩個答案都很好,但現在我得到一個錯誤致命錯誤:調用未定義的方法數據庫::準備()在/ ...我不知道我必須定義一個方法或不是它,還有其他原因嗎? – Robske

0

UNIX_TIMESTAMP()不是PHP函數,來自MYSQL。取而代之的是使用時間()

0

Rocket Hazmat, 該數據庫來自member.php;

require_once('config.inc.php'); 
require_once("database.class.php"); 
require_once("member.class.php"); 
require("album.func.php"); 
require("image.func.php"); 
require("thumb.func.php"); 
/* Start an instance of the Database Class */ 
$database = new database("xxx", "xxx", "xxx", "xxx"); 
/* Create an instance of the Member Class */ 
$member = new member(); 

database.class.php是;

級數據庫{

public $pdo = null; 


public $statement = null; 


* Database Constructor 
* 
* This method is used to create a new database object with a connection to a datbase 
*/ 
public function __construct() { 
    /* Try the connections */ 
    try { 
     /* Create a connections with the supplied values */ 
     $this->pdo = new PDO("mysql:host=" . Config::read('hostname') . ";dbname=" . Config::read('database') . "", Config::read('username'), Config::read('password'), Config::read('drivers')); 


/* 
* Database Query 
* 
* This method is used to create a new database prepared query 
* 
* @param string $query The prepared statement query to the database 
* @param array|string $bind All the variables to bind to the prepared statement 
* @return return the executed string 
*/ 
public function query($query, $bind = null, $fetch = 'FETCH_ASSOC') { 
    /* Prepare the query statement */ 
    $this->statement = $this->pdo->prepare($query); 
    /* Bind each value supplied from $bind */ 
    if($bind != null) { 
     foreach($bind as $select => $value) { 
      /* For each type of value give the appropriate param */ 
      if(is_int($value)) { 
       $param = PDO::PARAM_INT; 
      } elseif(is_bool($value)) { 
       $param = PDO::PARAM_BOOL; 
      } elseif(is_null($value)) { 
       $param = PDO::PARAM_NULL; 
      } elseif(is_string($value)) { 
       $param = PDO::PARAM_STR; 
      } else { 
       $param = FALSE; 
      } 
      /* Bid value */ 
      if($param) { 
       $this->statement->bindValue($select, $value, $param); 
      } 
     } 
    } 
    /* Execute Query & check for any errors */ 
    if(!$this->statement->execute()){ 
     $result = array(
      1 => 'false', 
      2 => '<b>[DATABASE] Error - Query:</b> There was an error in sql syntax', 
     ); 
     return $result; 
    } 
    /* Return all content */ 
    if($fetch == 'FETCH_ASSOC') { 
     $result = $this->statement->fetch(PDO::FETCH_ASSOC); 
    } elseif($fetch == 'FETCH_BOTH') { 
     $result = $this->statement->fetch(PDO::FETCH_BOTH); 
    } elseif($fetch == 'FETCH_LAZY') { 
     $result = $this->statement->fetch(PDO::FETCH_LAZY); 
    } elseif($fetch == 'FETCH_OBJ') { 
     $result = $this->statement->fetch(PDO::FETCH_OBJ); 
    } elseif($fetch == 'fetchAll') { 
     $result = $this->statement->fetchAll(); 
    } 
    return $result; 
} 

}

相關問題