Jump to content

NULL


Topshed

Recommended Posts

I have a problem with NULL

 

I set up 2 variables at the start of the script to find the  record I want to find.

Var 1  ($modnum) is a 5 digit numeric number (Int unsigned)

Var 2 ($suff) is a 2 digit Alpha code with most times is a NULL

 

the code look line this:-

$modnum =  10102;
$suff = NULL;
(or if $suff needs a value)
$suff = 'ZX';

I then do a select using this bit of code

 

$db = "SELECT * FROM $tble WHERE model = $modnum ";
   if (!$suff) {
    } else {
         $db .= "AND sufix = '$suff'";
    }

 

I believe I have  a problem with setting up my variable $suff

because if it is null, but there is another record with the same number AND a suff entry I end up with both records?

why would an entry with AB in suff be found when I am looking for a NULL

 

thanks

Topshed

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/92792-null/
Share on other sites

I think I understand what you're saying, try:

modnum =  10102;
$suff = ''; //initialize to null string
(or if $suff needs a value)
$suff = 'ZX';

$db = "SELECT * FROM $tble WHERE model = $modnum AND  sufix = '$suff'";

--in the case of $suff = '', the query string should end up looking something like this which should work:

SELECT * FROM $tble WHERE model = 10102 AND  sufix = ''

Link to comment
https://forums.phpfreaks.com/topic/92792-null/#findComment-475501
Share on other sites

Think I will back up a little here

 

Below is the code with the  table and sundry bits hidden

 

  <?php
$tble = 'aefe';

// next the inputs
$modnum = 10103;
$suff = '';
// End inputs

?>
<?php
nclude_once"../includes/My_conn.php";
    $connect = mysqli_connect($host,$account,$password) OR DIE("Error !! Unable to connect to database");
I    $db = mysqli_select_db($connect,"$dbname") OR DIE( "Unable to select database ");
    $db = "SELECT * FROM $tble WHERE model = $modnum ";
   if (!$suff) {
    } else {
         $db .= "AND sufix = '$suff'";
    }
if ($result = mysqli_query($connect,$db)) {  
	if (mysqli_num_rows($result)) {
			while ($row = mysqli_fetch_assoc($result)){
?>
<table width='1020' align='center' border='2' cellspacing='2' >
.....
.....
.....
.....
</table>
<br />
  <?php
		  }
  }
 };  //removing the ";" appears to make no differance to the working
?>
?>
<br />
</div>
</body>
</html>

 

There are five records where $modnum = 10103;

 

and the five $suff are = A,B,C,D and NULL

 

If my inputs are

$modnum = 10103;

$suff = 'A';  // or B C D

 

The code works fine However if my inputs are

$modnum = 10103;

$suff = '';

 

The code list's all five records so it appears the NULL

is not being picked up in the line

 

if (!$suff) {

    } else {

        $db .= "AND sufix = '$suff'";

 

Please someone Help

 

Thanks

Roy

 

Link to comment
https://forums.phpfreaks.com/topic/92792-null/#findComment-476380
Share on other sites

Wait......

What is this???

 

 if (!$suff) {
    } else {
         $db .= "AND sufix = '$suff'";
    }

 

Should be:

 

if ($stuff)        //read if NOT null then do
{
    $db .= "AND sufix = '$suff'";
}

 

Thats all, the code you have check if $stuff is not null it does nothing. and then in else (NULL) it adds the suffix

 

btw. you can make this check with sql:

 

SELECT * FROM $tble WHERE model = $modnum AND sufix IS NOT NULL;

 

Link to comment
https://forums.phpfreaks.com/topic/92792-null/#findComment-476524
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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