SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    NOT Like Query Doesn't Work

    I just upgraded to PHP 5.2 and the following query doesn't seem to work anymore. I just get an error that there is an error in the syntax. Any clue why, or how to fix it?

    Code:
    try{			
    	$db = DB::getInstance ();
    
    	$sql = "SELECT *
    		FROM view_names
    		WHERE name NOT Like :myname
    		";
    
    		$stmt = $db->query($sql);
    		$stmt->bindParam (':myname', $name, PDO::PARAM_STR );
    		$stmt->execute();
    		$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
    			
    		return $result;
    	}catch(PDOException $e){ 
    		echo $e->getMessage(); 
    	}
    When I use phpMyAdmin the query works, but it doesn't work in the code. I checked and the $name variable does have a value.

    I get this error - "SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ':myname' at line 50"

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,495
    Mentioned
    161 Post(s)
    Tagged
    4 Thread(s)
    What is the exact error you get?

  3. #3
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ':myname' at line 50

    Line 50 is - $stmt = $db->query($sql);

  4. #4
    SitePoint Guru
    Join Date
    Sep 2008
    Posts
    977
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have only ever used LIKE and when doing so, you need to surround the value/variable with '%these jobbies%'

    Say for example you want to find a word that is like 'sday', well to find those which end in 'sday' you would use '%sday' so you would get back Tuesday, Wednesday Thursday.

    And if you want to find words beginning with 'Mon%', the % goes at the end, where #mon' would join with 'ney' or 'day' giving money and monday, for example.

    like this
    Code MySQL:
    select col_name 
    from table where col_name like '%sday'

    I imagine that not like would work similarly so give it a try, if you like.

    bazz

  5. #5
    SitePoint Guru
    Join Date
    Sep 2008
    Posts
    977
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh, I should've said that instead of the :myname, use 'myname' and add the % to the end if you want to find 'mynameis%' or some other words, where there are characters after the inputted word.

    I'm not sure I have explained that very well.

    bazz


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •