Difference between revisions of "PHP199"

From mi-linux
Jump to navigationJump to search
 
(15 intermediate revisions by 4 users not shown)
Line 1: Line 1:
 +
== Install MySQL & PHP on Windows==
 +
 +
If students require to install PHP & MYSQL on windows platform please email me at R.Kabul@wlv.ac.uk i will provide you with the required details.
 +
 +
== WAMP ==
 +
 +
If you are having problem installing PHP, Apache and MySQL, use WAMP. WAMP stands for Windows, Apache, MySQL and PHP. It is a single application that consists PHP, Apache and MySQL in a bundle.
 +
 +
The link: http://www.wampserver.com
 +
 +
All you have to do is download the WAMP5 installation file and install it. No hassle at all. Cheers!!
 +
 +
== Using mysql_num_rows() ==
 +
 +
This is a small tip I haven't seen on the Wiki which shows you how to print correct statements depending on the number of rows return from your MySQL queries.  The function '''mysql_num_rows()''' is the one to look for.
 +
 +
''Example''
 +
 +
<pre>
 +
<?php
 +
// Database connection here...
 +
 +
$sql = "SELECT title,author FROM comics';
 +
 +
$res = mysql_query($sql);
 +
 +
// An error occurred!
 +
if (!$res) {
 +
  echo mysql_error();
 +
}
 +
 +
$total = mysql_num_rows($res);
 +
 +
if ($total == 0) {
 +
  echo 'No rows were returned';
 +
} elseif ($total == 1) {
 +
  echo '1 row was returned';
 +
} else {
 +
  echo $total . ' rows were returned';
 +
}
 +
?>
 +
</pre>
 +
 
== Viewing contents of array ==
 
== Viewing contents of array ==
  
 
A good tip for checking the information returned from the database is to use &lt;pre&gt; and print_r() which outputs arrays in a more readable form...
 
A good tip for checking the information returned from the database is to use &lt;pre&gt; and print_r() which outputs arrays in a more readable form...
  
e.g.
+
''Example''
  
 
Presuming the following SQL was used on the comics database...<br />
 
Presuming the following SQL was used on the comics database...<br />
Line 10: Line 53:
 
This would return the title and author details for the first record in the table.
 
This would return the title and author details for the first record in the table.
  
If you then used mysql_fetch_array() to get the first record and output it's contents wrapped in &lt;pre&gt; you get a nicely formatted view of the array and it's contents.
+
If you then used mysql_fetch_array() to return the details as an array and output it's contents wrapped in &lt;pre&gt; you get a nicely formatted view of the array and it's contents.
  
 
<pre>
 
<pre>
<?
+
<?php
 
// Database connection here...
 
// Database connection here...
  
Line 19: Line 62:
  
 
$res = mysql_query($sql);
 
$res = mysql_query($sql);
 +
 +
// An error occurred!
 +
if (!$res) {
 +
  echo mysql_error();
 +
}
  
 
$row = mysql_fetch_assoc($res);
 
$row = mysql_fetch_assoc($res);
Line 36: Line 84:
 
</pre>
 
</pre>
  
Can be especially useful when using multidimensional arrays, or when you have alot of fields returned from the database.
+
Can be especially useful when using multidimensional arrays, or when you have a lot of fields returned from the database.
 +
 
 +
''Multidimensional Array Example''<br>
 +
 
 +
Using the earlier example of planets, this is how the $planet2 array would look using this method...
 +
 
 +
<pre>
 +
<?php
 +
      $planets2 = array
 +
      (
 +
        "Mercury"=> array("dist"=>0.39, "dia"=>0.38),
 +
        "Venus"  => array("dist"=>0.39, "dia"=>0.95),
 +
        "Earth"  => array("dist"=>1.0,  "dia"=>1.0, "moons"=>array("Moon")),
 +
        "Mars"  => array("dist"=>0.39, "dia"=>0.53, "moons"=>array("Phobos", "Deimos"))
 +
      );
 +
      echo '&lt;pre&gt;'; print_r($planets2); echo '&lt;/pre&gt;';
 +
?>
 +
</pre>
 +
 
 +
Would print...
 +
 
 +
<pre>
 +
Array
 +
(
 +
    [Mercury] => Array
 +
        (
 +
            [dist] => 0.39
 +
            [dia] => 0.38
 +
        )
 +
 
 +
    [Venus] => Array
 +
        (
 +
            [dist] => 0.39
 +
            [dia] => 0.95
 +
        )
 +
 
 +
    [Earth] => Array
 +
        (
 +
            [dist] => 1
 +
            [dia] => 1
 +
            [moons] => Array
 +
                (
 +
                    [0] => Moon
 +
                )
 +
 
 +
        )
 +
 
 +
    [Mars] => Array
 +
        (
 +
            [dist] => 0.39
 +
            [dia] => 0.53
 +
            [moons] => Array
 +
                (
 +
                    [0] => Phobos
 +
                    [1] => Deimos
 +
                )
 +
 
 +
        )
 +
 
 +
)
 +
</pre>

Latest revision as of 19:13, 19 February 2007

Install MySQL & PHP on Windows

If students require to install PHP & MYSQL on windows platform please email me at R.Kabul@wlv.ac.uk i will provide you with the required details.

WAMP

If you are having problem installing PHP, Apache and MySQL, use WAMP. WAMP stands for Windows, Apache, MySQL and PHP. It is a single application that consists PHP, Apache and MySQL in a bundle.

The link: http://www.wampserver.com

All you have to do is download the WAMP5 installation file and install it. No hassle at all. Cheers!!

Using mysql_num_rows()

This is a small tip I haven't seen on the Wiki which shows you how to print correct statements depending on the number of rows return from your MySQL queries. The function mysql_num_rows() is the one to look for.

Example

<?php
// Database connection here...

$sql = "SELECT title,author FROM comics';

$res = mysql_query($sql);

// An error occurred!
if (!$res) {
  echo mysql_error();
}

$total = mysql_num_rows($res);

if ($total == 0) {
  echo 'No rows were returned';
} elseif ($total == 1) {
  echo '1 row was returned';
} else {
  echo $total . ' rows were returned';
}
?>

Viewing contents of array

A good tip for checking the information returned from the database is to use <pre> and print_r() which outputs arrays in a more readable form...

Example

Presuming the following SQL was used on the comics database...

SELECT title,author FROM comics LIMIT 1

This would return the title and author details for the first record in the table.

If you then used mysql_fetch_array() to return the details as an array and output it's contents wrapped in <pre> you get a nicely formatted view of the array and it's contents.

<?php
// Database connection here...

$sql = "SELECT title,author FROM comics LIMIT 1';

$res = mysql_query($sql);

// An error occurred!
if (!$res) {
  echo mysql_error();
}

$row = mysql_fetch_assoc($res);

echo '<pre>'; print_r($row); echo '<pre>';

?>

Would print the following...

Array
(
    [title] => Elfquest
    [author] => Richard/Pini (Wendi)
)

Can be especially useful when using multidimensional arrays, or when you have a lot of fields returned from the database.

Multidimensional Array Example

Using the earlier example of planets, this is how the $planet2 array would look using this method...

<?php
       $planets2 = array
       (
         "Mercury"=> array("dist"=>0.39, "dia"=>0.38),
         "Venus"  => array("dist"=>0.39, "dia"=>0.95),
         "Earth"  => array("dist"=>1.0,  "dia"=>1.0, "moons"=>array("Moon")),
         "Mars"   => array("dist"=>0.39, "dia"=>0.53, "moons"=>array("Phobos", "Deimos"))
       );
       echo '<pre>'; print_r($planets2); echo '</pre>';
?>

Would print...

Array
(
    [Mercury] => Array
        (
            [dist] => 0.39
            [dia] => 0.38
        )

    [Venus] => Array
        (
            [dist] => 0.39
            [dia] => 0.95
        )

    [Earth] => Array
        (
            [dist] => 1
            [dia] => 1
            [moons] => Array
                (
                    [0] => Moon
                )

        )

    [Mars] => Array
        (
            [dist] => 0.39
            [dia] => 0.53
            [moons] => Array
                (
                    [0] => Phobos
                    [1] => Deimos
                )

        )

)