gpt4 book ai didi

php - 在 Select MSQL 查询中使用 PHP 变量并返回错误

转载 作者:行者123 更新时间:2023-11-30 21:46:44 26 4
gpt4 key购买 nike

<分区>

我是 PHP 和 MySQL 的新手,我正在尝试使用 PHP 变量作为表名。但是当我这样做时它返回:

SQL Error: 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 '321321' at line 1

我的代码是:

$dbname = "321321";
require_once('authenticate.php');
$ownerID = $_SESSION["accNumber"];
if($dbname != $ownerID){
header('Location: login.php');
}


$db_host = 'localhost'; // Server Name
$db_user = 'root'; // Username
$db_pass = '***'; // Password
$db_name = 'test_database'; // Database Name

$conn = mysqli_connect($db_host, $db_user, $db_pass, $db_name);
if (!$conn) {
die ('Failed to connect to MySQL: ' . mysqli_connect_error());
}



$sql = 'SELECT * FROM '.$dbname.'';
$query = mysqli_query($conn, $sql);

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com