PHP/MySQL Database/mysql select db

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

Create guestbook table

 
<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 = "<h3>Created guestbook table</h3>";
  }
  else
  {
    $msg = "<h3>Could not create guestbook table";
    $msg.= "<br>Does it already exist?</h3>";
  }
  echo($msg);
 ?>
 </body>
</html>



mysql_select_db.php

 
$mysqli = new mysqli("localhost", "root","");
$mysqli->select_db("book") or die("Can"t select db!");



mysql_select_db(): select database

 
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!");
?>



Opening a Connection and Selecting a Database

 
<html>
<head>
<title>Opening a Connection to a Database</title>
</head>
<body>
<div>
<?php
$user = "root";
$pass = "";
$db = "mydatabase";
$link = @mysql_connect( "localhost", $user, $pass );
if ( ! $link ) {
  die( "Couldn"t connect to MySQL: ".mysql_error() );
}
print "<h2>Successfully connected to server</h2>\n\n";
@mysql_select_db( $db )
  or die ( "Couldn"t open $db: ".mysql_error() );
print "Successfully selected database \"$db\"<br />\n";
mysql_close( $link );
?>
</div>
</body>
</html>



Using MySQL with PHP

 
<?
    $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";
    }
?>