SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2010
    Posts
    79
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Newbie going crazy - variables.

    Hi, Please indulge a stupid question.

    HTML Code:
    			$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

  2. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    SitePoint Enthusiast
    Join Date
    Sep 2010
    Posts
    79
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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'];
    }

  4. #4
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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:
    PHP Code:
    $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>';

    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  5. #5
    SitePoint Enthusiast
    Join Date
    Sep 2010
    Posts
    79
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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
    HTML Code:
    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!

  6. #6
    SitePoint Guru
    Join Date
    Aug 2009
    Posts
    669
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dibley try using [php] tags instead. It will show your code nicely formatted and coloured which helps to local faults.
    I'll do anything to avoid working on my own code

    Are you using: if (isset($_POST['submit'])) ?
    IE has a bug and does not always send the value.


Bookmarks

Posting Permissions

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