dtxooq1020 2013-12-05 17:09
浏览 146
已采纳

带有消息'SQLSTATE [42S22]的未捕获异常'PDOException':未找到列

I tried to run the following code but it returned this erros:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column ''1'' in 'field list'' in /home/cardg/cards/jogar.php:59 Stack trace: #0 /home/cardg/cards/jogar.php(59): PDOStatement->execute() #1 {main} thrown in /home/cardg/cards/jogar.php on line 59
Why this is happening?

<?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;

    ?>
  • 写回答

1条回答 默认 最新

  • du77887 2013-12-05 17:17
    关注

    Remove delimiters (backticks) around the placeholders:

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

    ... as these are placeholders, which values (bound to them by bindValue) will be escaped automatically. Otherwise, those values will be treated as a column names, causing the error.

    As a sidenote, you have a typo here:

    $usunivel - $usulinha['nivel'];
    

    ... it should be $usunivel = $usulinha['nivel']; most probably.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?