PHP/MySQL Database/MySQL Connection

Материал из Web эксперт
Перейти к: навигация, поиск

Connect to MySQL

<?
$connection = @mysql_connect("mysql153.secureserver.net", "wbex", "password") or die(mysql_error());
$dbs = @mysql_list_dbs($connection)or die(mysql_error());
$db_list ="<ul>";
$i =0;
while ($i < mysql_num_rows($dbs)){
     $db_names[$i] = mysql_tablename($dbs,$i);
     $db_list .= "<li>$db_names[$i]";
     $i++;
}
$db_list .="</ul>";
?>
<HTML>
<HEAD>
<TITLE>MySQL Databases</TITLE>
</HEAD>
<BODY>
<P><strong>Databases on localhost</strong>:</P>
<? echo "$db_list"; ?>
</BODY>
</HTML>



Connect To MySQL Database

<?php
//
$hostname="mysql153.secureserver.net";
$username="wbex";
$password="password";
$dbname="wbex";
$usertable="Employee";
$yourfield = "FirstName";
mysql_connect($hostname,$username, $password) OR DIE ("Unable to connect to database! Please try again later.");
mysql_select_db($dbname);
$query = "SELECT * FROM $usertable";
$result = mysql_query($query);
if($result) {
    while($row = mysql_fetch_array($result)){
        $name = $row["$yourfield"];
        echo "Name: ".$name;
    }
}
?>



Listing the Databases Available in a Connection

<html>
<head>
<title>Listing the databases available in a connection</title>
</head>
<body>
<?php
$user = "wbex";
$pass = "password";
$link =  mysql_connect( "mysql153.secureserver.net", $user, $pass );
if ( ! $link )
    die( "Couldn"t connect to MySQL" );
$db_res = mysql_list_dbs( $link );
$num = mysql_num_rows( $db_res );
for( $x = 0; $x < $num; $x++ )
    print mysql_tablename( $db_res, $x )."<br>";
mysql_close( $link );
 ?>
</body>
</html>