Jump to content

Beginner question - what is this code doing?


Guest netsp

Recommended Posts

Hi everyone,

 

I am trying to learn php with a book* and I've been stuck on the database section for a while now. It uses object oriented "approach" to connecting with a database. I had some trouble with this (the kilerphp tutorial on OOP PHP helped a lot in getting to understand the fundamentals). However, even after taking a detour to learning a little about OOP, I am having trouble understanding the code used in the example:

 

1 -        if($statement = $link->prepare($sql)) 
2 -        { 
3 -          $statement->bind_param('i', $_POST['artist']); 
4 -          $statement->execute(); 
5 -          $statement->bind_result($album); 
6 -          while($statement->fetch()) { 
7 -          printf("Album: %s<br />", $album); 
8 -        } 
9 -        $statement->close(); 
10-        }

I understand (after a lot of head scratching) that line 1 is instantiating an object. IE $link->prepare($sql) returns an object which is stored in the $statement variable. What I don't understand is why or how this is a conditional.

 

Q1) HOW I thought that conditionals needed to have true/false as input. Why does it accept an object?

Q2) WHY I tried removing the if statement (removing the if() while leaving the line instantiating the $statement object and trying to execute all those other lines outside of the conditional. It didn't work (blank screen). Why not? If the conditional is executing, what is the difference between that and having the code run without an executable. IE, why is the above any different from.

 

if('123' = '123')
{
echo "the conditional is executing";
}

vs

echo "the conditional is executing";

 

 

 

Can someone perhaps write this code in a more understandable way

 

*PHP for Absolute Beginners, Jason Lengstorf

Link to comment
Share on other sites

Q1:

$statement isn't a boolean - it simply accepts whatever $link->prepare returns. $link->prepare returns an object if $link->prepare works, false if it doesn't work. You could drop the conditional entirely (plus additional coding changes to account for this), and use

 

$statement = $link->prepare($sql);

 

but then you would have no way of catching any mysqli errors and, in the case of an error, the user may be confused by output they don't expect (either an error message they don't understand or no output at all) without any indication of what is the problem. Technically, that code sample really should include an "else" part that displays an error to the user (also, in the current code sample, if $statement returns false, I think the next line " $statement->close(); " may cause an error since $statement will hold false, not an object, so that should be placed within the if statement):

 

1 -        if($statement = $link->prepare($sql)) 
2 -        { 
3 -          $statement->bind_param('i', $_POST['artist']); 
4 -          $statement->execute(); 
5 -          $statement->bind_result($album); 
6 -          while($statement->fetch()) { 
7 -          printf("Album: %s<br />", $album);
8 -          $statement->close();  
9 -        } 
10-        else
11-        {
12-          echo "Database error!"; // display error message that the user should understand
13 -       }

Q2:

A blank page indicates an error of some kind. I really can't tell you what went wrong (you'd need to provide a code sample showing how you removed the if statement) but perhaps you forgot a ";" or too few/too many brackets ("{" or "}"). I don't think the error was specifically caused by removing the if statement, since the code should work without the if statement, but perhaps you removed the if incompletely, or in the process caused an error of some kind.

 

You really should turn error reporting on while you are doing development (at least on your local machine) to help catch errors. That is done either by including these two lines at the top of your PHP code:

 

ini_set('display_errors', '1');
error_reporting(E_ALL);

or by editing your PHP.ini file to set the correct variables. A Google search should show you what you need to do.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...