MySQL PHP example

PHP-MySQL really simple example


<?php
// Connecting, selecting database
$link = mysql_connect("localhost", "_USERNAME_", "_PASSWORD_")
   or die('Could not connect: ' . mysql_error());
echo 'Connected successfully';
mysql_select_db("_DB_NAME_") or die('Could not select database');

// Performing SQL query
$query = 'SELECT * FROM _TABLE_NAME_ ';
echo 'Query successfully';

$result = mysql_query($query) or die('Query failed: ' . mysql_error());
echo 'Result successfully';

// Printing results in HTML
// say this table has following fields: sitename, url and info

while ($line = mysql_fetch_assoc($result)) {
          echo "t<li><a href="{$line[url]}">{$line[sitename]}</a>: {$line[info]}</li>n";
}

// Free resultset
mysql_free_result($result);

// Closing connection
mysql_close($link);
?>

php.net’s mysql function page

Also found cool page for php Form Values reference.

Related posts:

  1. PHP memo to myself
  2. blog machine
  3. MySQL
  4. PHP Form
  5. Very simple php snippet
This entry was posted in Linux, Mac, and LAMP Open Source. Bookmark the permalink. Trackbacks are closed, but you can post a comment.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

  • Welcome to Daigo’s Daily Digital Diorama. I blog about... whatever that comes to my mind.
  • Change color

    Purple Red Green Blue No Style

  • Archives

  • Calendar

    December 2004
    M T W T F S S
    « Nov   Jan »
     12345
    6789101112
    13141516171819
    20212223242526
    2728293031  
  • Categories