PHP/MySQL Database/mysql select db
Содержание
Create guestbook table
<source lang="html4strict">
<html>
<head> <title>Create guestbook table</title> </head> <body> <?php $conn = @mysql_connect("localhost", "userName", "password") or die("Could not connect to database"); $rs = @mysql_select_db("my_database", $conn) or die("Could not select database"); $query = "id int(4) auto_increment, name varchar(50),"; $query.= "email varchar(50), comments text, "; $query.= "time timestamp(14), primary key(id)"; $sql = "create table guestbook($query)"; $rs = @mysql_query ($sql); if( $rs ) {$msg = "
Created guestbook table
";} else {$msg = "
Could not create guestbook table";
$msg.= "
Does it already exist?
";
} echo($msg); ?> </body>
</html>
</source>
mysql_select_db.php
<source lang="html4strict">
$mysqli = new mysqli("localhost", "root",""); $mysqli->select_db("book") or die("Can"t select db!");
</source>
mysql_select_db(): select database
<source lang="html4strict">
Its syntax is: int mysql_select_db (string database_name [, int link_id]) <? @mysql_connect("localhost", "web", "asdf") or die("Could not connect to MySQL server!"); @mysql_select_db("company") or die("Could not select company database!"); ?>
</source>
Opening a Connection and Selecting a Database
<source lang="html4strict">
<html> <head> <title>Opening a Connection to a Database</title> </head> <body>
<?php $user = "root"; $pass = ""; $db = "mydatabase"; $link = @mysql_connect( "localhost", $user, $pass ); if ( ! $link ) {
die( "Couldn"t connect to MySQL: ".mysql_error() );
}
print "Successfully connected to server
\n\n";@mysql_select_db( $db )
or die ( "Couldn"t open $db: ".mysql_error() );
print "Successfully selected database \"$db\"
\n";
mysql_close( $link );
?>
</body> </html>
</source>
Using MySQL with PHP
<source lang="html4strict">
<?
$connection = mysql_connect("localhost", "phpuser", "password"); if ($connection) { $db = mysql_select_db("phpdb"); if (!$db) print "Failed to select "phpdb".\n"; } else { print "Failed to connect to database.\n"; }
?>
</source>