我在php中执行查询时遇到问题。我的php代码中有以下查询:
$nome = pg_escape_string($_POST['cnome']);
$obtem_idb = "SELECT idb FROM banda WHERE nome = $nome";
echo("$nome");
$idb = pg_query($connection, $obtem_idb);
if(!$idb){
die("Error in SQL query: " . pg_last_error());
} else {
echo("o idb que vem da query é $idb");
}
我打印的名称是正确的,但在尝试执行查询时,出现以下错误:
Error in SQL query: ERROR: column "anthrax" does not exist LINE 1: SELECT idb FROM banda WHERE nome = Anthrax ^
有人能帮忙吗,我好像找不到错误。
当我将变量$nome放入单引号中时,它的值更改为'Resource id#2',我得到以下错误:
Error in SQL query: ERROR: insert or update on table "edicao" violates foreign key constraint "edicao_idb_fkey" DETAIL: Key (idb)=(Resource id #2) is not present in table "banda".
最佳答案
你忘了在价值上加引号了:
$obtem_idb = "SELECT idb FROM banda WHERE nome = '$nome'";
^-----^--- must be quoted