我正在尝试使用php在我的本地数据库中创建临时表。它不会创建标题,也不会给出错误。我使用DESC temp检查,它没有显示表的结构。这是我的代码:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$hostname = "localhost";
$username = "root";
$password = "";
$database = "autosearch";
//connection to the database
$dbhandle = mysqli_connect($hostname, $username, $password);
$db = mysqli_select_db($dbhandle, $database) or die("Unable to connect to MySQL");
//echo "Connected to MySQL<br>";
$query_createTemporaryTable = "CREATE TEMPORARY TABLE #temp(temp_id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
ArtistName VARCHAR(20), NAMEOfTheDVD VARCHAR(30)";
$result_createtemptable = mysqli_query($dbhandle, $query_createTemporaryTable);
$query_insertintotable = "INSERT INTO temp (ArtistName, NAMEOfTheDVD)
VALUES ('R', 'SHAWooSHANK')";
$result_insertintotable = mysqli_query($dbhandle, $query_insertintotable) or die(mysqli_error($dbhandle));
$query_selecttemptable = "SELECT ArtistName,NAMEOfTheDVD FROM temp";
$result_selecttemptable = mysqli_query( $query_selecttemptable,$dbhandle) or die(mysqli_error());
while($row_selecttemptable = mysqli_fetch_array($result_selecttemptable)){
echo $row_selecttemptable;
}
$delete_temptable = "DROP TABLE 'temp'";
$result_temptable = mysqli_query($delete_temptable, $dbhandle) or die(mysqli_error());
mysqli_close();
?>
答案 0 :(得分:1)
这里有几个错误
首先你有一个点而不是一个逗号作为temp_id之后的分隔符,它不能编译。此外,列列表中有3列,数据列表中只有2列。实际上你不需要传递temp_id
,因为它是一个AutoIncrement列。所以将此行更改为
$query_insertintotable = "INSERT INTO temp (ArtistName, NAMEOfTheDVD)
VALUES ('R', 'SHAWooSHANK')";
答案 1 :(得分:0)
以下是修复错误的代码,临时表重命名为temp_artist
。
这些都是小错误。我建议您在开发PHP环境中设置最高级别的错误检查。
在PHP中使用之前,我还检查所有SQL代码是否都可以在IDE中运行。
错误:
create table statement
上mysqli_query
语句中的第一个参数。drop
语句中的表名应该有反引号而不是引号。array
。代码:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$hostname = "localhost";
$username = "test";
$password = "test";
$database = "testmysql";
//connection to the database
$dbhandle = mysqli_connect($hostname, $username, $password);
$db = mysqli_select_db($dbhandle, $database) or die("Unable to connect to MySQL");
//echo "Connected to MySQL<br>";
$query_createTemporaryTable = "CREATE TEMPORARY TABLE `temp_artist` (temp_id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
ArtistName VARCHAR(20), NAMEOfTheDVD VARCHAR(30))";
$result_createtemptable = mysqli_query($dbhandle, $query_createTemporaryTable);
$query_insertintotable = "INSERT INTO `temp_artist` (ArtistName, NAMEOfTheDVD)
VALUES ('R', 'SHAWooSHANK')";
$result_insertintotable = mysqli_query($dbhandle, $query_insertintotable) or die(mysqli_error($dbhandle));
$query_selecttemptable = "SELECT ArtistName, NAMEOfTheDVD FROM `temp_artist`";
$result_selecttemptable = mysqli_query($dbhandle, $query_selecttemptable) or die(mysqli_error());
while($row_selecttemptable = mysqli_fetch_array($result_selecttemptable)){
echo $row_selecttemptable['ArtistName'], ' - ',
$row_selecttemptable['NAMEOfTheDVD'], '<br />';
}
$delete_temptable = "DROP TABLE `temp_artist`";
$result_temptable = mysqli_query($dbhandle, $delete_temptable) or die(mysqli_error());
mysqli_close($dbhandle);