Skip to content

CURRENT_TIMESTAMP & $prefix #1046

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
sajjadef98 opened this issue Apr 9, 2025 · 1 comment
Open

CURRENT_TIMESTAMP & $prefix #1046

sajjadef98 opened this issue Apr 9, 2025 · 1 comment

Comments

@sajjadef98
Copy link

My code has an error when I use prefix and changes the CURRENT_TIMESTAMP variable? What is the problem?
php v =8.4.3
mysqlv=5.7.33
faremework = https://github.com/flare-framework/Flare/

<?php

require_once __DIR__ . '/../app/cli-config.php';

$table = MysqliDb::$prefix . 'users';

if (!$db->tableExists($table)) {
    $query = "
CREATE TABLE IF NOT EXISTS `{$table}` (
    `id` INT AUTO_INCREMENT PRIMARY KEY,
    `name` VARCHAR(100) NOT NULL,
    `email` VARCHAR(150) NOT NULL UNIQUE,
    `password` VARCHAR(255) NOT NULL,
    `token` VARCHAR(255) DEFAULT NULL,
    `photo` VARCHAR(255) DEFAULT NULL,
    `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; ";

    echo "🔧 Running SQL:\n$query\n";

    $db->rawQuery($query) ;
    if ($db->tableExists($table)) {
        echo "✅ Table '$table' created successfully.\n";
    } else {
        echo "❌ Failed to create table '$table'.\n";
    }
} else {
    echo "⚠️ Table '$table' already exists.\n";
}
CREATE TABLE IF NOT EXISTS `Ydnujs_users` (
    `id` INT AUTO_INCREMENT PRIMARY KEY,
    `name` VARCHAR(100) NOT NULL,
    `email` VARCHAR(150) NOT NULL UNIQUE,
    `password` VARCHAR(255) NOT NULL,
    `token` VARCHAR(255) DEFAULT NULL,
    `photo` VARCHAR(255) DEFAULT NULL,
    `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
mysqli_sql_exception {#14
  #message: "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Ydnujs_CURRENT_TIMESTAMP, `updated_at` TIMESTAMP NOT
 NULL DEFAULT Ydnujs_CURRENT' at line 1"
@sajjadef98
Copy link
Author

I solved it.

require_once __DIR__ . '/../app/cli-config.php';

$table = MysqliDb::$prefix . 'users';

if (!$db->tableExists($table)) {
    $query = "
    CREATE TABLE IF NOT EXISTS `{$table}` (
        `id` INT AUTO_INCREMENT PRIMARY KEY,
        `name` VARCHAR(100) NOT NULL,
        `email` VARCHAR(150) NOT NULL UNIQUE,
        `password` VARCHAR(255) NOT NULL,
        `token` VARCHAR(255) DEFAULT NULL,
        `photo` VARCHAR(255) DEFAULT NULL,
        `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
        `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
    ";

    $result = $db->mysqli()->query($query);
    if ($result===true) {
        echo "✅ Table '$table' created successfully.\n";
    } else {
        echo "❌ Failed to create table '$table'.\n";
    }
} else {
    echo "⚠️ Table '$table' already exists.\n";
}

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant