0

我正在尝试将我的数据库从 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

4 回答 4

2

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

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

于 2013-06-18T18:56:41.253 回答
1

UNIX_TIMESTAMP是一个 MySQL 函数,而不是 PHP 函数。您不需要将参数绑定到UNIX_TIMESTAMP,您可以在查询中使用它。

摆脱':timestamp' => UNIX_TIMESTAMP(),这是你的问题。FirstUNIX_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。如果没有,您应该将其添加到查询中。

于 2013-06-18T18:57:22.823 回答
0

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

于 2013-06-18T18:58:04.150 回答
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;
}

}

于 2013-06-19T19:04:02.430 回答