Newbie going crazy - variables

Hi, Please indulge a stupid question.

			$paid = $row ['datepaid'];
		}
		echo 'Paid ='.$paid.'!';
		if ($privlink=='')
		{
			if ($paid=='') 
			{

				Option A			} 
			else 
			{
				Option B			}
		}

$paid is a variable set by contents of a mysql DB - which I am using to decide with PDF to output. The echo statment is purely to debug by outputting the value of $paid. the output from this echo was

‘Paid =!’

Therefore making me believe that $paid has no value.

Why is it then that my if statment is acting as if $paid has a value, or at least is not equal to nothing.

Can someone please enlightment me.

(i’d like the following to work - If $paid = ‘’ then option A else option B)

Many thanks in advance

What’s the code above it? It looks like $paid is only set under a certain condition or loop, which may be causing your problem.

It might not even be an empty string, it could be something else, theoretically, so var_dump($paid) to see what it actually is.

As for the if statement, it requires that $privlink == ‘’, so var_dump($privlink) to see what that is too.

doing the various var_dump()'s i am gettign the value of NULL for $paid and $privlink.

So would i be correct in thinking that altering $paid==‘’ to $paid=NULL ?

Edit:

Oh and the code above is:

	$sql = "select privlinkid, datepaid from property inner join invoice on invoice.id = property.id where property.id = '.$id.' ";
	
	$result = mysqli_query ($link, $sql);
	
	if (!$result)
	{
		$error = 'Error - Please enter a valid ID££. ' . mysqli_error($link);
		include 'error.html.php';
		exit();
	}
	while ($row = mysqli_fetch_array ($result))
	{
		$privlink = $row['$privlinkid'];
		$paid = $row ['datepaid'];
	}

Ok well what I’d recommend is a little restructure. Also your query quotes are a little messed up so I’ll fix that for you, which may resolve your issue of getting a null result too.

As it appears that you are only expecting one row, the while loop needn’t be used. Try the following:


$sql = "SELECT privlinkid, datepaid FROM property INNER JOIN invoice ON invoice.id = property.id WHERE property.id = '{$id}'"; //Capitalising the query's commands is a good practise because it helps your eyes separate the commands from the other stuff. Also, if the ID is an integer, remove the single quotes around it and cast the id to an integer before the query using: $id = (int)$id; - this will prevent SQL injection attacks if you haven't prevented them beforehand.
$result = mysqli_query($link, $sql);
if($result === false){
    $error = 'Error - Please enter a valid Property ID. ' . mysqli_error($link);
    include 'error.html.php';
    exit;
}
$fetch = mysqli_fetch_array($result);
if($fetch === false){ //The first row is false, therefore non-existent, therefore no rows have been returned. Look into the fetch_array function to see why this works.
    //do whatever you need to do if no results are returned:
}else{
    $privlink = $fetch['privlinkid'];
    $paid = $fetch['datepaid'];
    echo '<ul>';
    echo '<li>Paid: ' . $paid . '</li>'; //exclamation marks are fun and all, but not generally appropriate.
    echo '<li>PrivlinkID: ' . $privlink . '</li>';
    echo '</ul>';
}

Thanks for your replies Jake, and in a round about way it worked, as well as giving me some solid advise in the process thank you!

Your advise regarding the use of var_dump’s helped trememdeously, allowed me to isolate the fact I had neglected to set $id (or rather get and sanitise from the code on button press!)

Strangely

SELECT privlinkid, datepaid FROM property INNER JOIN invoice ON invoice.id = property.id WHERE property.id = NULL

didnt return any values, let alone the one i wanted!!! - head hangin in shame now!

Dibley try using

 tags instead. It will show your code nicely formatted and coloured which helps to local faults.