致命错误:在/中调用未定义的函数UNIX_TIMESTAMP()

时间:2013-06-18 18:50:30

标签: php mysql pdo

我正在尝试将我的数据库从MySQL更改为PDO。这很困难,必须解决很多错误。

我不知道如何解决这个错误。

  

致命错误:在第63行的/ ...中调用未定义的函数UNIX_TIMESTAMP()。

这是错误

的部分
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错了吗?我该如何解决这个问题?

感谢。

4 个答案:

答案 0 :(得分:2)

UNIX_TIMESTAMP()不是PHP函数。此外,您已经在SQL中获得了它,并且SQL字符串中没有:timestamp参数。所以只要失去':timestamp' => UNIX_TIMESTAMP()你就应该好。

另外,你的session_id部分搞砸了。首先,您将会话ID直接转储到SQL字符串中,然后将其添加到参数数组中,但值和键被反转。

答案 1 :(得分: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。如果没有,您应该将其添加到查询中。

答案 2 :(得分:0)

UNIX_TIMESTAMP()不是PHP函数,来自MYSQL。而是使用time()

答案 3 :(得分: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;
}

}