SQL query help (mysql database)

by 6 replies
7
Hey guys,
My background is ASP/MSSQL, so I'm a bit out of my depths with this one, and would certainly appreciate some help.

Can someone please tell me why this keeps throwing an error message?

$r2=query_wrapper("SELECT * FROM tblphotos WHERE tblph_shareID = ?", $share_ID " ORDER BY tblph_shareID DESC LIMIT 1");
#programming #database #mysql #query #sql
  • Hi John,

    Is ?", $share_ID " ASP specific?

    I thought in ASP you just need + to concat strings, like:

    $r2=query_wrapper("SELECT * FROM tblphotos WHERE tblph_shareID = " + $share_ID + " ORDER BY tblph_shareID DESC LIMIT 1");
  • Providing the actual "error message" would help alot.

    It depends on what the query_wrapper uses to query a database.

    With the ?, it looks like its using mysqli ...

    If your trying to bind parameters, your way off.

    PHP: mysqli_stmt::bind_param - Manual
  • Try this

    $r2=query_wrapper("SELECT * FROM tblphotos WHERE tblph_shareID="'. $share_ID.'" ORDER BY tblph_shareID DESC LIMIT 1");

    Not sure why there was "," before $share_ID but add it there if you think its required.
  • Not sure what the ? is for. Though I dont code in ASP the SQL should be the same. Are you looking for wildcard (%) ?


    $r2=query_wrapper("SELECT * FROM tblphotos WHERE tblph_shareID LIKE '%$share_ID ' ORDER BY tblph_shareID DESC LIMIT 1");
  • You have Written only LIMIT 1, in the end. Whereas, it should be LIMIT 0,1. Althought, Limit 1 would work well in MySQL. Some Databases Might Have Problems. Also, you have quotes at the wrong placed.

    So, your complete query will look like

    Code:
    =query_wrapper("SELECT * FROM tblphotos WHERE tblph_shareID = ? ,  ORDER BY tblph_shareID DESC LIMIT 0,1");
  • What does "query_wrapper" do? Does just quote the SQL (to prevent injects?) or can you use it for query formatting?

    Maybe it can do something like this:

    Code:
    =query_wrapper("SELECT * FROM tblphotos WHERE tblph_shareID = ? ORDER BY tblph_shareID DESC LIMIT 1",  share_ID);
    But would have to know what query_wrapper does and what arguments it takes.

Next Topics on Trending Feed