PHP SQL临时表将无法正常工作
I'm trying to understand how the temporary tables work. But even when I copy and paste it from my tutorials, I don't get the expected result.
See the code underneath for more info. At the moment the .php file returns a blank page (no errors), while i would expect it to be:
1 Row inserted.
I looked for the error code but found out that temporary tables don't have an error code.
Does someone know what I'm doing wrong?
<?php
$link = mysqli_connect("localhost","xxx","xxx","xxx");
/* check connection */
if (mysqli_connect_errno())
{
printf("Connect failed: %s
", mysqli_connect_error());
exit();
}
mysqli_query($link, "CREATE TEMPORARY TABLE myCity (Name CHAR(30)");
$city = "'s Hertogenbosch";
$city = mysqli_real_escape_string($link, $city);
/* this query with escaped $city will work */
if (mysqli_query($link, "INSERT INTO myCity (Name) VALUES ('$city')"))
{
printf("%d Row inserted.
", mysqli_affected_rows($link));
}
mysqli_close($link);
?>
我正在尝试了解临时表的工作原理。 但即使我从我的教程中复制并粘贴它,我也没有得到预期的结果。 p>
请参阅下面的代码以获取更多信息。 目前.php文件返回一个 空白页面(没有错误), 我希望它是: p>
插入1行。 p> blockquote>
我查找了错误代码,但发现临时表没有错误代码。 p>
有人知道我做错了吗? p>
&lt;?php $ link = mysqli_connect(“localhost”,“xxx”,“xxx”,“xxx”); / *检查连接* / 如果( mysqli_connect_errno()) { printf(“连接失败:%s ”,mysqli_connect_error()); exit(); } mysqli_query($ link,“CREATE TEMPORARY TABLE myCity( 名称CHAR(30)“); $ city =”'s Hertogenbosch“; $ city = mysqli_real_escape_string($ link,$ city); / *这个带有转义$ city的查询将起作用* / if(mysqli_query($ link,“INSERT INTO myCity(Name)VALUES('$ city')”)) { print f(“%d行插入。 ”,mysqli_affected_rows($ link)); } mysqli_close($ link); ?&gt; code> pre>
this works for me
<?php
ini_set("display_errors",1);
error_reporting(E_ALL);
$mysqli = new mysqli("localhost", "root", "admin123", "test");
if (mysqli_connect_errno()) {
printf("Connect failed: %s
", mysqli_connect_error());
exit();
}
$mysqli->query("CREATE TEMPORARY TABLE CITIES (
id int(11) NOT NULL,
name varchar(64) NOT NULL
)");
//$mysqli->autocommit(FALSE);
$mysqli->query("INSERT INTO CITIES VALUES ('1', 'Bavaria')");
$mysqli->query("INSERT INTO CITIES VALUES ('2', 'Havana')");
//$mysqli->commit();
if ($result = $mysqli->query("SELECT * FROM CITIES LIMIT 10")) {
printf("Select returned %d rows.
", $result->num_rows);
/* free result set */
$result->close();
}
//$mysqli->query("DROP TABLE CITIES");
$mysqli->close();
Remember Temporary tables only exist by session, sometimes we need create conventional table and truncate later... You would must think use DB lib like Doctrine DBAL or ADODb or something like that...
Looks like you have an error in your sql creation. You are missing a closing parenthesis: ")"
CREATE TEMPORARY TABLE myCity (Name CHAR(30)
Adrien.