Thursday, March 15, 2012

PHP Tutorial :: Part 1 :: Intro to PHP

  • How to create a php file
    • name the file xxx.php (xxx being whatever you want)
    • enclose any php coding on the page with <?php to start and ?> to end
  • How to insert text or html in a part of the php file that is enclosed in the <?php and ?> tags
    • echo "putwhateveryouwantinhere";
    • for example:: 
      • echo "<center>Here is some text and <img src='blah.jpg'> that was an image</center>";
  • How to include another php file inside your php file
    • you would want to do this if there is a bit of code you use over and over on different pages
    • you will either use include or require - require will cause it to fail if it cant find it
    • for example
      • include "includes/header.php";
      • require "includes/navigation.php";
  • What is a php variable
    • a php variable is a variable that you apply a value to and use in equations, allowing for dynamic websites
    • a php variable is denoted by the $ sign in front of a word
    • for example
      • $age
      • $name
      • $username
  • How to declare or assign a php variable
    • you will use the equal
    • for example
      • $age = "23";
      • $username = "cooldude23";
  • How to pull information from a database
    • first you have to connect to the database
      • $dbh=mysql_connect ("localhost", "insertdatabaseusername", "insertpassword") or die ('I cannot connect to the database because: ' . mysql_error());
      • mysql_select_db ("insertdatabasename");
    • next you have to throw a query at the database
      • $query = "SELECT * FROM tablename WHERE username='sally'";
        $result = mysql_query($query)
        or die ("Couldn't execute query.");
    • next you have to format the way the query will show up, like in this example with a while loop
      • while ($row = mysql_fetch_array($result))
    • next you will want to print what you want from this loop
      • echo "<h2>Rows :: $variablefromdb</h2><br><br>";
    • finally you will want to close the while loop
      • }
    • that will print all of the variablefromdb values from that table when the username is sally

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...
Web Design & Marketing - Click and get an instant quote on your project