0

Sir I have following codes

<?php

// Connection variables
$host     = "localhost";
$username = "root";
$password = "";
$db_name  = "phonebook";
$con      = mysql_connect($host, $username, $password, $db_name);

if (!$con) {
    echo ('<script type="text/javascript">alert("Error Connecting to database")' . mysql_connect_error($con) . '</script>');
}

$result   = "";
$username = "";
$password = "";

function clean($str)
{
    $cstr = trim($str);
    $cstr = addslashes($str);
    $cstr = htmlspecialchars($str);
    return $cstr;
}

if (isset($_POST['login'])) {
    session_start();

    $myusername = clean($_POST['username']);
    $mypassword = clean($_POST['password']);

    if (empty($myusername) || empty($mypassword)) {
        echo '<script>alert("User name or password must not be empty")</script>';
    } else {
        $query = "SELECT * FROM admin where username ='" . $myusername . "' and password='" . $mypassword . " ' ";
        $result = mysql_query($query) or die(mysql_error());
    }

    if (!$result) {
        echo ('<script type="text/javascript">alert("username, password query is not working")' . mysql_error($con) . '</script>');
    }

    $count = mysql_num_rows($result);

    if ($count == 1) {
        session_register("myusername");
        $_SESSION['myusername'] = $myusername;
        header("location: welcome.php");
    } else {
        $error = "Your Login Name or Password is invalid";
    }
}
?>

Code says:
No database is selected.

Database image is as follows:
[IMG]http://i43.tinypic.com/2j1rdco.jpg[/IMG]

3
Contributors
3
Replies
22
Views
3 Years
Discussion Span
Last Post by TheBludger
0

You must select a database using the mysql_select_db() function. So, replace this line

$con = mysql_connect($host, $username, $password, $db_name);

by the following

$con = mysql_connect($host, $username, $password);
mysql_select_db($db_name, $con);

Note, however, that PHP's mysql_* functions will be deprecated in the future, and that any further use of it is disencouraged.

Edited by minitauros

0

Still learning myself, but the following may help.
The Syntax of your mysql_connect seems to be wrong.
try mysql_connect($host, $username, $password)
Then you need a seperate statement to select the database.
mysql_select_db($db_name)

Note that mysql_connect is deprecated in PHP 5.5.0 you should consider using mysqli_connect()

0

Still learning myself, but the following may help.

Ah the light bulb moment. You have left the "i" out of mysqli_connect statement

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.