Topshed Posted January 16, 2008 Share Posted January 16, 2008 I have a Select WHERE query that I cannot get to work with a NULL in a VARCHAR field I input 2 variables $modnum = 12204; $suff = 'NULL' . . . . . . $db="SELECT * FROM $tble WHERE model = $modnum AND sufix = '$suff'"; Sufix can have from Null to 6 chars in the varchar field, If I put in a valid input like A, DL, EX, it works fine but if I leave it blank it dies I have tried all sorts of inputs like $suff = 'NULL' $suff = '' $suff = ' ' $suff = ' ' I am still new to this stuff and a google does not find me an answer Please help me someone Thanks Roy... Quote Link to comment https://forums.phpfreaks.com/topic/86300-help-with-select-null/ Share on other sites More sharing options...
nikefido Posted January 16, 2008 Share Posted January 16, 2008 make an if statement for your query so the WHERE $suff = NULL part is NOT included if it is null: <?php $db = "SELECT * FROM $tble WHERE model = $modnum "; if ($suff == 'null') { //don't add the AND statement. } else { $db .= "AND sufix = '$suff'"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/86300-help-with-select-null/#findComment-440880 Share on other sites More sharing options...
Topshed Posted January 16, 2008 Author Share Posted January 16, 2008 Thanks for the reply but it still bombs with a NULL $modnum = 11101; $suff = 'NULL' $db = "SELECT * FROM $tble WHERE model = $modnum "; if ($suff == 'null') { //don't add the AND statement. } else { $db .= "AND sufix = '$suff'"; } if ($result = mysqli_query($connect,$db)) { if (mysqli_num_rows($result)) { while ($row = mysqli_fetch_assoc($result)){ What am I missing regards Roy Quote Link to comment https://forums.phpfreaks.com/topic/86300-help-with-select-null/#findComment-441382 Share on other sites More sharing options...
Ken2k7 Posted January 17, 2008 Share Posted January 17, 2008 I don't think NULL is equal to null. $modnum = 11101; $suff = 'NULL' $db = "SELECT * FROM $tble WHERE model = '$modnum' "; if (strtolower($suff) == 'null') { //don't add the AND statement. } else { $db .= "AND sufix = '$suff'"; } if ($result = mysqli_query($connect,$db)) { if (mysqli_num_rows($result)) { while ($row = mysqli_fetch_assoc($result)){ Quote Link to comment https://forums.phpfreaks.com/topic/86300-help-with-select-null/#findComment-441641 Share on other sites More sharing options...
Nhoj Posted January 17, 2008 Share Posted January 17, 2008 I have a Select WHERE query that I cannot get to work with a NULL in a VARCHAR field I input 2 variables $modnum = 12204; $suff = 'NULL' . . . . . . $db="SELECT * FROM $tble WHERE model = $modnum AND sufix = '$suff'"; Sufix can have from Null to 6 chars in the varchar field, If I put in a valid input like A, DL, EX, it works fine but if I leave it blank it dies I have tried all sorts of inputs like $suff = 'NULL' $suff = '' $suff = ' ' $suff = ' ' I am still new to this stuff and a google does not find me an answer Please help me someone Thanks Roy... $stuff = NULL; No quotes bud... Also, for the if if ($stuff == NULL) { } or you can do if (!$stuff) { } Quote Link to comment https://forums.phpfreaks.com/topic/86300-help-with-select-null/#findComment-441722 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.