I am making a simple query but it is not working and I don't know why. I recently started becoming acquainted with PDO connections to databases.
Here is the code :
The Connections is :
define("HOST","localhost"); define("USER","root"); define("PASS","password"); define("BASE","portugalforcedb"); try{ $conexao = 'mysql:host='.HOST.';dbname='.BASE; $connect = new PDO($conexao, USER, PASS); $connect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); }catch(PDOException $erro){ echo $erro->getMessage(); }
then I create aa query that works like this :
try{ $query = $connect->query("SELECT N.id,N.titulo,N.texto,N.autor,N.data,J.imagem_noticia FROM noticias N JOIN jogo J ON N.jogo_id = J.id WHERE N.publicada =1 ORDER BY N.data DESC LIMIT 4"); }catch(PDOException $erro){ echo $erro->getMessage(); } while($dados = $query->fetch(PDO::FETCH_ASSOC)) {
but then I create another query in another page like this that doesn't work :
$id = $_GET['id'];
try{
$query = $connect->prepare("SELECT N.id,N.titulo,N.texto,N.autor,N.data,J.imagem_noticia FROM noticias N JOIN jogo J ON N.jogo_id = J.id WHERE N.publicada =1 AND id=numero");
$query->bindParam('numero',$id,PDO::PARAM_INT);
$query->execute();
}catch(PDOException $erro){
echo $erro->getMessage();
}
$dados = $query->fetch(PDO::FETCH_ASSOC);
and I tried:
$id = $_GET['id'];
try{
$query = $connect->query("SELECT N.id,N.titulo,N.texto,N.autor,N.data,J.imagem_noticia FROM noticias N JOIN jogo J ON N.jogo_id = J.id WHERE N.publicada =1 AND id=numero");
}catch(PDOException $erro){
echo $erro->getMessage();
}
while($dados = $query->fetch(PDO::FETCH_ASSOC))
but then this error appears :
SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous
At the end of query id=numero
. id needs table alias. It should be N.id or J.id
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.