2015-12-26 89 views
2

當我試着使用此代碼,以主持人的我(int) user id from $_SESSION['user']['id']值:SQLSTATE [42000]:語法錯誤或訪問衝突:1064(bindvalue爲整數)

$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; 
    } 
} 

我收到此錯誤信息。

二是埃恩FOUT gebeurd:SQLSTATE [42000]:語法錯誤或訪問 衝突:1064您的SQL語法錯誤;檢查對應於你的MySQL服務器版本正確的語法使用 附近的手動 「:用戶ID」在1號線

+0

你缺少空間... – SMA

+0

我在哪裏缺少空間? – OriginalJef

回答

0

什麼查詢在MySQL中執行時,它返回到你嗎?您的列數據類型是否與要綁定它的值相同?

嘗試以下操作:

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會話變量,第一是$_SESSION['user'],第二個是$_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> 

這是我的結果(。它的作品):

enter image description here

+0

現在錯誤顯示「Er en e fout gebeurd:SQLSTATE [42000]:語法錯誤或訪問衝突:1064您的SQL語法有錯誤;檢查與您的MySQL服務器版本對應的手冊, ''在第1行爲null和user_id ='1'「 – OriginalJef

+0

在mysql中嘗試它,而不是='1'try = 1 – droidnation

+0

什麼是列數據類型?請提供給我們一些關於你的mysql表 – droidnation

0

這是一個愚蠢的錯誤。它通過設置$results = $stmt_notes_select_where 來修復。就是這樣。

相關問題