Php 未捕获异常';PDO异常';带有消息';SQLSTATE[42S22]:未找到列

Php 未捕获异常';PDO异常';带有消息';SQLSTATE[42S22]:未找到列,php,mysql,pdo,Php,Mysql,Pdo,我试图运行以下代码,但它返回此错误: 致命错误:未捕获异常“PDOException”,消息为“SQLSTATE[42S22]:未找到列:1054未知列“1”位于/home/cardg/cards/jogar.php中的“字段列表”中。php:59堆栈跟踪:#0/home/cardg/cards/jogar.php(59):PDOStatement->execute()#1{main}在第59行的/home/cardg/cards/jogar.php中抛出 为什么会发生这种情况? <?ph

我试图运行以下代码,但它返回此错误:
致命错误:未捕获异常“PDOException”,消息为“SQLSTATE[42S22]:未找到列:1054未知列“1”位于/home/cardg/cards/jogar.php中的“字段列表”中。php:59堆栈跟踪:#0/home/cardg/cards/jogar.php(59):PDOStatement->execute()#1{main}在第59行的/home/cardg/cards/jogar.php中抛出
为什么会发生这种情况?

<?php

 include('config.php');


$usuarion = $_SESSION['login'];
$senhan = $_SESSION['senha'];

// $attrs is optional, this demonstrates using persistent connections,
// the equivalent of mysql_pconnect
$attrs = array(PDO::ATTR_PERSISTENT => true);

// connect to PDO
$pdo = new PDO('mysql:host='.$dbservidor.';dbname='.$dbnome.'', $dbusuario, $dbsenha);

// the following tells PDO we want it to throw Exceptions for every error.
// this is far more useful than the default mode of throwing php errors
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

// prepare the statement. the place holders allow PDO to handle substituting
// the values, which also prevents SQL injection
$stmt = $pdo->prepare("SELECT estado,usuario1,usuario2,usunivel,id  FROM duelos WHERE estado=:estadox AND usuario1!=:usuario");

// bind the parameters
$stmt->bindValue(":estadox", 0);
$stmt->bindValue(":usuario", $usuarion);

// initialise an array for the results 
$duelos = array();
if ($stmt->execute()) {
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
        $products[] = $row;
        echo $row['usuario1'];
    }
}



$usuario = $pdo->query("SELECT id,apelido,usuario,nivel FROM usuarios WHERE usuario = '".$usuarion."' AND senha ='".$senhan."'");
$usulinha = $usuario->fetch(PDO::FETCH_ASSOC);
$usuarioid = $usulinha['id'];
$usunivel - $usulinha['nivel'];

    $sqlduelos = "SELECT COUNT(*) FROM duelos WHERE (estado = 1 AND usuario2 = 0)";

       if ($resl = $pdo->query($sqlduelos)) {
    /* Check the number of rows that match the SELECT statement */
  if ($resl->fetchColumn() > 0) {
  $msg = "True msg";
    }
    else{
        $msg = "false msg";

        $inid = $pdo->prepare("INSERT INTO `duelos`  (`usuario1`, `usunivel`)  VALUES (
    `:usua`,
    `:usuni`)");

    $inid->bindParam(':usua', $usuarioid);
    $inid->bindParam(':usuni', $usunivel);
    $inid->execute();

    }
}

// set PDO to null in order to close the connection
$pdo = null;

    ?>
删除占位符周围的分隔符(反勾号):

$inid = $pdo->prepare("INSERT INTO `duelos`  (`usuario1`, `usunivel`)  
   VALUES (:usua, :usuni)");
。。。由于这些是占位符,将自动转义哪些值(由
bindValue
绑定到它们)。否则,这些值将被视为列名,从而导致错误

作为旁注,这里有一个输入错误:

$usunivel - $usulinha['nivel'];

。。。它应该是
$usunivel=$usulinha['nivel']
最有可能。

删除
:usua
:usuni
周围的反引号。请添加注释以显示第59行在列表中的位置file@rsanchez错误更改为:致命错误:未捕获异常“PDOException”,消息为“SQLSTATE[23000]:完整性约束冲突:1048列/home中的“usunivel”不能为null…我的建议:您似乎没有掌握足够的数据库基础知识来完成此任务,我建议您先阅读一下。上面的错误只是意味着如果不为
usunivel
指定值,就无法在
duelos
中插入内容。您应该指定的内容取决于模式的设计者打算去那里做什么,但是由于模式指定了
NOT NULL
,所以应该去那里……第二个错误是:
$usunivel-$usulinha['nivel']
你可能是指
$usunivel=$usulinha['nivel'](等于,而不是负),但这并不意味着这条建议是错误的,因为它解决了问题中给出的问题,不是吗?然后,您会发现代码中的另一个错误——但关键是,这是另一个错误。虽然前一个问题确实很有趣(我记得当有人认为在占位符周围放置
是个好主意时,我回答了另一个问题),但后一个问题只是一个打字错误-应该通过正确设置
错误报告级别来发现。