Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php SQLSTATE[42000]:语法错误或访问冲突:1064(bindvalue为整数)_Php_Mysql - Fatal编程技术网

Php SQLSTATE[42000]:语法错误或访问冲突:1064(bindvalue为整数)

Php SQLSTATE[42000]:语法错误或访问冲突:1064(bindvalue为整数),php,mysql,Php,Mysql,当我试图用以下代码强制执行来自$\u会话['user']['id']的int user id的值时: $userId = ''; if (isset($_SESSION['user'])) { $userId = $_SESSION['user']['id']; //56) uitlezen van de sessievariabele... // TODO: 57) statement voor een select uit te voeren... user en deleted... $

当我试图用以下代码强制执行来自$\u会话['user']['id']的int user id的值时:

$userId = '';
if (isset($_SESSION['user'])) {
$userId = $_SESSION['user']['id']; //56) uitlezen van de sessievariabele...

// TODO: 57) statement voor een select uit te voeren... user en deleted...
$sql_notes_select_where
    = 'SELECT '
    . '`note_id` AS `id`,'
    . '`note_title` AS `title`,'
    . '`note_content` AS `content`'
    . 'FROM `notes` '
    . 'WHERE '
    .       '`note_deleted` is null and'
    .       '`user_id`=:userid'
;


try {
    // 58) Connectie openen met db...
    $db = getDbConnection();
    /**
     * Zie ook: http://courses.olivierparent.be/php/databases/pdo-php-data-objects/
     */
    if ($stmt_notes_select_where = $db->prepare($sql_notes_select_where)) {
        // TODO: 59) Binding uitvoeren user...
        $stmt_notes_select_where->bindValue(':userid', $userId);
        $stmt_notes_select_where->execute();
    }
    // TODO: 60) Het opvragen van het aantal rijen...
    //$results = ...;
    $results = $db->query($sql_notes_select_where);


    // 61) Het sluiten van de connectie met de db.
    closeDbConnection($db);
} catch (PDOException $e) {
    switch ($e->getCode()) {
        case '23000':
            $error = "Er bestaat al een gebruiker met <strong>{$_POST['email']}</strong> als e-mailadres.";
            break;
        default:
            $error = 'Er is een fout gebeurd: ' . $e->getMessage();
            break;
    }
}
我收到了这个错误消息

Er甚至是fout-gebeurd:SQLSTATE[42000]:语法错误或访问 违反:1064您的SQL语法有错误;检查手册 对应于MySQL服务器版本的正确语法 在第1行附近使用“:userid”


在MySQL中执行查询时,查询返回给您什么?您的columns数据类型是否与要绑定的th值相同

请尝试以下操作:

SELECT note_id AS `id`,
    note_title AS `title`,
    note_content AS `content`
    FROM `notes` 
    WHERE 
   note_deleted is null and
    user_id= :userid
;
编辑 设置2个会话变量,第一个是$\u session['user'],第二个是$\u session['id of your user']登录时

<?php
$DB_host = "localhost";
$DB_user = "your user";
$DB_pass = "your pass";
$DB_name = "your database";
//try to put the id in the session
//Then
$userId = $_SESSION['id'];
 $conn = new PDO("mysql:host={$DB_host};dbname={$DB_name}",$DB_user,$DB_pass);
         $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
?>
    <body>
    <?php 
            $selectAll = "SELECT note_id AS id,
        note_title AS title,
        note_content AS content
        FROM notes 
        WHERE 
       note_deleted is null and
        user_id= :id";
            $stmtAll=$conn->prepare($selectAll);
            $stmtAll->bindValue(':id', $userId);
            $execAll=$stmtAll->execute();
            $result=$stmtAll->fetchAll(); 
            foreach($result as $rows){
              var_Dump($rows) ?>
    <table>
    <tr>
    <td>
    <?php       echo $rows['title'];
    ?>
    </td>
这是我的结果,它起作用了:


这是一个愚蠢的错误。通过设置$results=$stmt\u notes\u select\u where可以修复此问题
就是这样。

您缺少空间…我在哪里缺少空间?现在错误显示Er是een fout gebeurd:SQLSTATE[42000]:语法错误或访问冲突:1064您的SQL语法有错误;检查与您的MySQL服务器版本相对应的手册,以了解在第1行的“`is null and user_id='1”附近使用的正确语法在MySQL中重试,而不是使用='1'try=1,您的列数据类型是什么?请向我们提供一些关于mysql工作时的信息。这就像他把:userid作为一个字符串,因为它在会话中被声明为int。您的注释\u deleted是timestamp吗?