query works in mysql(phpmyadmin) but not in php

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Tinus
    New Member
    • Jul 2010
    • 30

    query works in mysql(phpmyadmin) but not in php

    Hi

    This query works in mysql

    Code:
    SELECT id
    FROM FILE WHERE created = (
    SELECT max( created )
    FROM FILE )
    But not in php

    tried
    Code:
    $getid= " SELECT id FROM file WHERE created = (SELECT max(created) FROM file) ";
    and
    Code:
    $getid= " SELECT 'id' FROM 'file' WHERE 'created' = (SELECT max('created') FROM 'file') "
    ;

    but it does not work.

    please help.
    Last edited by Atli; Aug 18 '10, 04:40 PM. Reason: Please use [code] tags for when posting code.
  • zorgi
    Recognized Expert Contributor
    • Mar 2008
    • 431

    #2
    Since it works in phpmyadmin I can only assume its your PHP that creates the problem. Can you show us PHP code that handles the query. BTW you could also try

    SELECT id FROM file ORDER BY created DESC LIMIT 1

    Comment

    • Tinus
      New Member
      • Jul 2010
      • 30

      #3
      Code:
      $getid= "SELECT 'id' FROM 'file' ORDER BY 'created' DESC LIMIT 1 ";
      is giving
      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 'SELECT 'id' FROM 'file' ORDER BY 'created' DESC LIMIT 1' at line 3

      PHP

      Code:
      // Create the SQL query 
              $query = " INSERT INTO `file` (`name`, `mime`,`size`,`data`,`created` ) 
                           VALUES ('{$name}','{$mime}',{$size},'{$data}',NOW())"; 
              $result = $dbLink->query($query); 
              $getid= "SELECT 'id' FROM 'file' ORDER BY 'created' DESC LIMIT 1 ";
      		$theid=$dbLink->query($getid);
      Last edited by Atli; Aug 18 '10, 04:41 PM. Reason: Please use [code] tags for when posting code.

      Comment

      • zorgi
        Recognized Expert Contributor
        • Mar 2008
        • 431

        #4
        An identifier may be quoted or unquoted. If you are quoting identifier than use backtick (“`”) not single quotes.

        Comment

        • Tinus
          New Member
          • Jul 2010
          • 30

          #5
          Still getting
          Success! Your file was successfully added!Error! Query failed:

          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 'SELECT `id` FROM file ORDER BY created DESC LIMIT 1' at line 3

          query sqlq is no good. File 'C:\Documents and Settings\Admini strator\My Documents\Downl oads\shares.txt ' not found (Errcode: 2)

          With
          Code:
          $getid= "SELECT `id` FROM file ORDER BY created DESC LIMIT 1 ";
          		$theid=$dbLink->query($getid);
          and
          Code:
          $getid= "SELECT `id` FROM `file` ORDER BY `created` DESC LIMIT 1 ";
          Last edited by Atli; Aug 18 '10, 04:41 PM.

          Comment

          Working...