Workshop

The quiz and exercises are provided to help you solidify your understanding of the material covered today. Try to understand the quiz and exercise answers before continuing to tomorrow's lesson.

Quiz

1:Which of the following are correct tags for marking the beginning of a section of PHP in an HTML page:
  • <<

  • <php

  • <?

  • <!

A1: <php and <? are correct.
2:If you have an associative array called $arr with elements defined by
$arr = array ("J"=>"John", "S"=>"Susan", "T"=>"Thomas");

which of the following is correct syntax for referencing the first element?

  • $arr[“John”]

  • $arr[“J”]

A2: $arr["J"] is correct.
3:True or False: If you run a DELETE query like this:
$x = mysql_query ("DELETE FROM mytable");

the result in $x will be the number of rows deleted.

A3: False. $x would be TRUE if the SQL statement was executed successfully or FALSE if it failed. You would need to call mysql_affected_rows() to determine the number of rows deleted.
4:What statement could you use within a while loop if you want to terminate it prematurely?
A4: You could use break or continue. exit terminates the entire script.

Exercises

1:Write a PHP function for connecting to a MySQL database on localhost and returning the link identifier to the calling program. It should connect to MySQL and should make a persistent connection. Assume that the database user is called myuser and the password is mypass. Your script should print an appropriate error message (but suppress any server error messages) if the connection fails.
A1: Your code should look something like this:
function connect_to_database () {
    $link_id = @mysql_pconnect ("localhost", "myuser", "mypass");
    if (!$link_id) echo "Failed to connect to MySQL!<BR>
";
    return $link_id;
}

2:Write a section of PHP script that performs an UPDATE on a table articles, tests for the query being run successfully, and prints the number of rows that were affected.
A2: Your code should look something like this:
<?php
$sql = "UPDATE articles
        WHERE some condition
        SET some column=some_value";
if ($result = mysql_query ($sql)) {
    if ($aff = mysql_affected_rows ()) echo "$aff rows updated<BR>
";
    else echo "Nothing updated<BR>
";
} else echo "Query failed: ". mysql_error() ."<BR>
";
?>

3:Write a section of PHP script that performs a SELECT on a table called products. The table includes the columns name and price. Your query should retrieve all products whose price is less than 50. You should allow for an error condition and display the MySQL error if one occurs.
A3: Your code should look something like this:
<?php
$sql = "SELECT name, price
        FROM products
        WHERE price < 50";
if ($result = mysql_query ($sql)) {
    while ($row = mysql_fetch_array ($result)) {
        echo "Product: ".$row["name"]." price: $".$row["price"]."<BR>
";
    }
} else echo mysql_error() ."<BR>
";
?>

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.248.37