Worth2Read
Home

Forum

PHP Tutorials
Blogs
Children
Article Directory

Contact Us

Disclaimer
Reliable $1 Web Hosting by 3iX
 

If you have any doubts with your PHP code please discuss it in the PHP Forum

Connecting to MysQL Database :

$dbh=mysql_connect ("server name ", "username",
"PASSWORD") or die('Cannot connect to the database because: ' . mysql_error());

mysql_select_db ("databasename");


The following code is more clear :

$user_name = "username";
$password = "password";
$database = "database name ";
$server = "server name ";
$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);

if ($db_found) {

// do somehing

}

else{
print "Database NOT Found ";
mysql_close($db_handle);
}

 

Display the contents of the table :

$result = mysql_query( "SELECT * FROM tablename " )
or die("SELECT Error: ".mysql_error());

$num_rows = mysql_num_rows($result);

print "There are $num_rows records.<P>";

print "<table width=500 border=1>\n";

while ($get_info = mysql_fetch_row($result)){
print "<tr>\n";

foreach ($get_info as $field)
print "\t<td>$field</td>\n";

print "</tr>\n";

}
print "</table>\n";

Insert values into the table :

$sql = "INSERT INTO tablename(field1, field2 ) VALUES('value1','value2')" ;

$results = mysql_query ( $sql);

Functions in PHP :

Example :

functions.php

<?php

function databaseConnection($dbName)
{
$db = mysql_connect ( "localhost" , "username" , "password" )or die('Cannot connect to the database because: ' . mysql_error());
mysql_select_db ($dbName);
}

?>

Include the above file in the page you want to call this function using

include("functions.php");

Call the function using


databaseConnection("databaseName");

 

Copyright © 2008 www.worth2read.org. All rights Reserved. Use of this Site is subject to our Privacy Statement
Untitled Document
PHP 6 and MySQL 5 for Dynamic Web Sites: Visual QuickPro Guide
Programming PHP
Beginning PHP and MySQL: From Novice to Professional, Third Edition (Beginning from Novice to Professional)
Wicked Cool PHP: Real-World Scripts That Solve Difficult Problems
PHP and MySQL Web Development (3rd Edition) (Developer's Library)
PHP & MySQL For Dummies 3rd edition (For Dummies (Computer/Tech))
PHP Solutions: Dynamic Web Design Made Easy (Solutions)
Build Your Own Website The Right Way Using HTML & CSS
Learning Web Design: A Beginner's Guide to (X)HTML, StyleSheets, and Web Graphics
Sams Teach Yourself HTML and CSS in 24 Hours (7th Edition) (Sams Teach Yourself)