Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Dec 2007
    Posts
    269
    Thanks
    28
    Thanked 0 Times in 0 Posts

    ternary error for SQL

    This is my code. if $_GET is not null everything is ok but if it s null i am giving following error . how can i correct it?

    Code:
    mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in sip.php on line 131
    Sayfalanacak Veri yok.
    PHP Code:
    $sira 'DESC';
    $agent  = (isset($_GET["agent"]))    ? $_GET["agent"]    :'';
    $cari    = (isset($_GET["cari"]))    ? $_GET["cari"]        :'';
    $urun    = (isset($_GET["urun"]))    ? $_GET["urun"]        :'';
    $telno    = (isset($_GET["telno"]))    ? $_GET["telno"]    :'';
    $tarih1    = (isset($_GET["date1"]))    ? $_GET["date1"]    :'';
    $tarih2    = (isset($_GET["date2"]))    ? $_GET["date2"]    :'';

    $kuladi $_COOKIE["kuladi"];
    $s1 "SELECT * FROM $tablo WHERE xcode > 0 
                                AND kuladi     = '"
    .$agent."'
                                AND urun     = '"
    .$urun."'
                                AND telno    = '"
    .$telno."'
                                AND cari    = '"
    .$cari."'
                                ORDER BY kayitzamani $sira"
    ;
                                
    $s2    "SELECT count(*) AS total FROM $tablo WHERE xcode > 0 
                                                AND kuladi     = '"
    .$agent."'
                                                AND urun     = '"
    .$urun."'
                                                AND telno    = '"
    .$telno."'
                                                AND cari     = '"
    .$cari."'"

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    This hasn't a thing to do with a ternary operator usage. Failed ternaries result in a syntax error.
    This issue is caused by a failed query. Execute mysql_query with an or die(mysql_error()); to fetch the cause of the failure.

  • #3
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    If amy of the $_GET fields you are using in your query are empty or contain invalid values then the query is either not going to work or will do something totally different from what you want. You need to validate the input and make sure the values are meaningful BEFORE you try to use them in the query.

    Note also that null in PHP is not the same as NULL in SQL.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #4
    Regular Coder
    Join Date
    Dec 2007
    Posts
    269
    Thanks
    28
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by felgall View Post
    If amy of the $_GET fields you are using in your query are empty or contain invalid values then the query is either not going to work or will do something totally different from what you want. You need to validate the input and make sure the values are meaningful BEFORE you try to use them in the query.

    Note also that null in PHP is not the same as NULL in SQL.
    $_get can has 2 different values empty or any value . i want it does not give error if it is empty

  • #5
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    What is the query supposed to do when the field is empty - return everything in the database or nothing or what?

    Also what about if a field contains SQL code and so gives the person access to the entire database? For example if $_GET['agent' contains "' x' OR 1=1; --" then all the data where xcode>0 would be returned regardless of what is in the other $_GET fields.

    You MUST validate the data before using it to access the database and you need to dynamically build the query based on how you want it to handle where fiends are left empty.
    Last edited by felgall; 02-21-2012 at 09:14 PM.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #6
    Regular Coder
    Join Date
    Dec 2007
    Posts
    269
    Thanks
    28
    Thanked 0 Times in 0 Posts
    this is query
    PHP Code:
    $query mysql_query($sqlStr.$limit$link);
    while(
    $row mysql_fetch_assoc($query)){ 

  • #7
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,865
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    Change it to
    PHP Code:
    $query mysql_query($sqlStr.$limit$link) or die(mysql_error(). '<br>'.$sqlStr.$limit); 
    Quote Originally Posted by php.net/manual/en/function.set-error-handler.php
    it is your responsibility to die() if necessary!
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)


  •  

    Posting Permissions

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