這裏有什麼問題?我得到的表適用於罰款。我確定列名稱uid在那裏。但是當我試圖從查詢中獲得uid時,沒有任何東西可以回來。這是很好的,因爲表是空的。但是,我的INSERT INTO命令不起作用,因爲在INSERT INTO之後,我仍然沒有uid回來。使用Postgres 9.1.5。謝謝!Postgresql INSERT INTO使用PHP不工作
$query = "SELECT * FROM information_schema.tables WHERE table_name = 'usertable';";
$result = pg_query($dbconn, $query);
if (pg_num_rows($result))
{
echo "Table exists<br>";
checkForUserRow();
}
else
{
echo "Error on query, attempting to create table<br>";
$sql = "CREATE TABLE usertable (uid integer PRIMARY KEY, sign varchar(255));";
pg_query($dbconn, $sql) or die(pg_errormessage());
$result = pg_query($dbconn,$query);
if (pg_num_rows($result)) {
echo "Table created<br>";
checkForUserRow();
}
}
pg_close($conn);
function checkForUserRow()
{
$query = "SELECT uid FROM usertable WHERE uid = '123'";
$result = pg_query($dbconn, $query);
if(pg_num_rows($result))
{
echo "User DB row exists<br>";
}
else
{
echo "User row does not exist - attempt to add user to table<br/>";
$sql = "INSERT INTO usertable (uid) VALUES('123')";
pg_query($dbconn, $sql);
$result = pg_query($dbconn, $query);
if (pg_num_rows($result))
{
echo "User successfully added!<br/>";
}
else
{
echo "User not added :(";
}
}
就是這樣。感謝使用變量作爲params的額外提示。我是PHP新手,根本不理解範圍 – user659488