Chapter 4 - Build Your Own Database Driven Web Site Using PHP & MySQL

I am having a very challenging time with Chapter 4 of this book. If you are someone that was once in my shoes, are there any tips you can give me? Specifically with the last couple of examples of this chapter. I am brand new to PHP and want very badly to be great at it! Just have to wrap my head around it all. If you could connect me with any resources you may have used to help or anything at all like that, I would be very grateful, thanks!

  • David.

Can you give an example where you get stuck? I don’t have this book, so chapter 4 doesn’t tell me much.

There are different versions of the book so it would be better if you could be more specific :slight_smile:

It’s the 4th edition book. I will post the places I am having problems with when I get home (in transit right now). Thanks for your willingness to help so far though! I believe there is actually a link somewhere on the sitepoint website that shows the entire fourth chapter. Can’t find it right now that easily though. Thanks again, will post more once I get home.

Can someone help me understand exactly what “ENT_QUOTES” is used for? I know that it is used with htmlspecialchars to convert single and double quotes to … Something? Thanks!

If ENT_QUOTES is not set, double quotes (") are changed by its entity: &quot. Else, they are ignored and not included in the result

If ENT_QUOTES is set, single quotes (') and double qoutes(") are changed by its entities: ’ and "

As an example:

$new = htmlspecialchars("<a href='test'>Test</a>", ENT_QUOTES);
echo $new; // the result is <a href=&#039;test&#039;>Test</a>

So just to clarify… does this mean that all double quotes will be turned into a single quote, and all single quotes will be turned into " ?? Or do the single quotes just stay as single quotes? Lastly, what does the “ENT” techcnially stand for? “enter” ? “Entry” ? sorry, i’m a total n00b with this!

Double quotes and single quotes are not turned to anything but their entities.

That means that each time you have a double symbol, it will appear &quote; instead, and when you have a single quote, it will appear & #039;.

By using entities, you don’t have to worry about the special meaning that these characters have for PHP or HTML. The user reading the page will see the quote with no problem at all.

In the example I gave you, if you execute it in your own computer and look at the view source, you will see that the correct result is:

<a href=& #039;test& #039;>Test</a>

note: I’ve added unnecessary white spaces so the entity is not parsed (by the browser) and show a single quote instead.

What the user reading the page will see is:
[noparse]<a href=‘test’>Test</a>[/noparse]

That is, exactly the text I wrote. By changing the characters with special meaning and substitute them with their entities, I was able to force the browser to show this string as regular text, instead of having the browser creating a proper link.

Just in case, an entity is just an alias for a character. The browser recognizes that alias and shows the right character or text in its place. Entities should always start with & and end with ;

I hope this is clear.

That helps a lot, thank you so much for taking the time to help me! If I have more questions I will post them.

By the way, if I have additional questions, should I start a new thread or just carry on with this one?

Unless it is related to what we just talked about, you should start a new thread. Although many of your questions may have been answered and it may be worth it to do a search first, and if they’re related to this book, it may be worth to check the PHP forum as well :slight_smile:

Hey I can really understand where you are coming from with trying to get chapter 4 down. I am readng the 4thedition and am also struggling with Chapter 4. I am having a hard time getting past this piece of code:

$result = mysqli_query($link, ‘SELECT joketext FROM joke’);
if (!$result)
$error = 'Error fetching jokes: ’ . mysqli_error($link);
include ‘error.html.php’;
while ($row = mysqli_fetch_array($result))
$jokes = $row[‘joketext’];
include ‘jokes.html.php’;

Are you at this point and do you understand what’s goig on in the code???

I don’t have the fourth version but a previous one, where mysqli was not used… but I think I can explain what’s going on and I hope this will help you. I’ll try to be detailed although I’m sure that there are things that you already know

$result = mysqli_query($link, 'SELECT joketext FROM joke');

Open a connection to the database and table using the info kept in the variable $link, and execute the query ‘SELECT joketext FROM joke’

if (!$result)

If there is no result at all

$error = 'Error fetching jokes: ' . mysqli_error($link);
include 'error.html.php';

Then keep the details and the error message in a variable called $error. The function mysqli_error() will have the specifics details.
then, use the HTML template error.html.php to display the error on screen (remember that Kevin likes to keep the HTML in one page, with as little php as possible, if any, and the php functions in other and code in other).

while ($row = mysqli_fetch_array($result))
$jokes[] = $row['joketext'];

If you get to this point, it is because you do have at least one record. So while there are records to be read, fetch record by record and add it to the array $jokes. In this way, if you what to get access to the very first record, you only it will be kept in $jokes[0], if I want the second, in $jokes[1] and so on.

include 'jokes.html.php';

The HTML to display the jokes is jokes.html.php, so I need to include it to handle this particular job.