MYSQL query with $_GET from URL

MYSQL query with $_GET from URL

James PayneJames Payne Posts: 15Questions: 0Answers: 0
edited December 2010 in General
Hello,

I'm using server-side processing and all is working well apart from one thing. I need to get a variable from the URL and include it in my MYSQL query like so…

[code]$sQuery = "
SELECT SQL_CALC_FOUND_ROWS contacts_id, ".str_replace(" , ", " ", implode(", ", $aColumns))."
FROM $sTable
WHERE clienttype = ".$_GET['advert_type']."
$sOrder
$sLimit
";
[/code]

When I try this it gives me a JSON error. Using Firebug it tells me the html error is

[code]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 'ORDER BY contacts_id asc LIMIT 0, 10' at line 4[/code]

Obviously the $_GET part of the query is not working as would normally. Is there any special type of syntax I need to use to access URL variables in the processing script?

Thanks,

James

Replies

  • James PayneJames Payne Posts: 15Questions: 0Answers: 0
    I have managed to sort this out by using javascript to get the variable and pass it to the processing page via the url like…

    [code]$(document).ready(function() {
    var advert_type = $.url.param("advert_type");
    $('#view_records_by_advert_type').dataTable({
    "sAjaxSource": "includes/datatable_form_parsers/view_records_by_client_type_parse.php?advert_type="+advert_type;
    });
    });
    [/code]

    I then use $_GET in the processing file and it works if I overwrite the WHERE part of the MYSQL query but I need to keep it as it filters my results correctly. If I replace it with my own the filtering obviously stops working. Is there a way to include my own WHERE query and keep the existing filtering?

    Here is my existing WHERE code…

    [code] $sWhere = "";
    if ( $_GET['sSearch'] != "" )
    {
    $sWhere = "WHERE (";
    for ( $i=0 ; $i
  • Jr0xJr0x Posts: 29Questions: 0Answers: 0
    Have you echoed out the sQuery to see what is it like?
  • James PayneJames Payne Posts: 15Questions: 0Answers: 0
    Hi Jr0x,

    yes I have and I've just this minute sorted it using that method. I had to recreate the MYSQL query as follows…
    [code]
    $sWhere = "";
    if ( $_GET['sSearch'] != "" )
    {
    $sWhere = "WHERE clienttype='$advert_type' AND contacts_id LIKE '%".mysql_real_escape_string( $_GET['sSearch'] )."%' OR clienttype='$advert_type' AND companyname LIKE '%".mysql_real_escape_string( $_GET['sSearch'] )."%' OR clienttype='$advert_type' AND businesstype LIKE '%".mysql_real_escape_string( $_GET['sSearch'] )."%' OR clienttype='$advert_type' AND sales_contact LIKE '%".mysql_real_escape_string( $_GET['sSearch'] )."%'";

    } else {$sWhere = "WHERE clienttype='$advert_type'";}
    [/code]

    It means I've got to hard code the columns I need but at least it's working!

    Thanks,

    James
This discussion has been closed.