2013-12-19 12 views
0

Я получаю сообщение об ошибке: Нет базу данных выбранного Вот код:Ошибка подключения базы данных PHP

<?php 
    $host = "localhost"; 
    $user = "root"; 
    $password = ""; 
    $database_name = "Student"; 

    mysql_connect($host, $user, $password); 
    mysql_select_db($database_name); 
    ?> 

Этот код для класса PHP-connect.php и формы:

<!DOCTYPE html> 
    <html> 
    <body> 

    <?php 
    //load database connection 
    include("php-connect.php"); 
    $id = "1"; 
    $name = "Elena"; 
    $city = "Lahore"; 

    //Command to insert into table 
    $query = "INSERT INTO studentdata (id,name,city) VALUES ('$id','$name','$city')"; 

    //run the query to insert the person. 
    $result = mysql_query($query) OR die(mysql_error()); 

    //let them know the person has been added. 
    echo "Data successfully inserted into the database table ... "; 
    ?> 

    </body> 
    </html> 

Это код формы. Я испробовал много вещей, чтобы исправить эту ошибку, но она не работает. Есть ли проблемы с моей базой данных?

+0

Печать 'mysq l_error() 'после _connect. – mario

+1

mysql_ * устарел. Используйте mysqli или PDO. – skrilled

+0

Возможно, выбор базы данных не сработал? (Использовать 'mysql_error()' там также) –

ответ

0

Попробуйте это ...

<?php 
$host = "localhost"; 
$user = "root"; 
$password = ""; 
$database_name = "Student"; 

$mLink = mysql_connect($host, $user, $password) or die(mysql_error()); 
mysql_select_db($database_name , $mLink); 

другой файл

<?php 

require 'php-connect.php'; 


//...... etc.. 

, но прочитать этот первый:

This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information. Alternatives to this function include: mysqli_select_db() PDO::__construct() (part of dsn)

http://br.php.net/mysql_select_db

Смежные вопросы