Jump to content

Recommended Posts

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...

Link to comment
https://forums.phpfreaks.com/topic/86300-help-with-select-null/
Share on other sites

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'";
}
?>

Link to comment
https://forums.phpfreaks.com/topic/86300-help-with-select-null/#findComment-440880
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/86300-help-with-select-null/#findComment-441382
Share on other sites

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)){

Link to comment
https://forums.phpfreaks.com/topic/86300-help-with-select-null/#findComment-441641
Share on other sites

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) {
}

Link to comment
https://forums.phpfreaks.com/topic/86300-help-with-select-null/#findComment-441722
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.