0

This is my php code.And i get no database selected error while running it in the localhost.kindly help me please.

<?php
$hostname_localhost ="localhost";
$database_localhost ="mydatabase";
$username_localhost ="root";
$password_localhost ="";
$localhost = mysql_connect($hostname_localhost,$username_localhost,$password_localhost)
or
trigger_error(mysql_error());

mysql_select_db($database_localhost, $localhost);

$username = $_POST['username'];
$password = $_POST['password'];
$query_search = "select * from tbl_user where username = '".$username."' AND password = '".$password. "'";
$query_exec = mysql_query($query_search) or die(mysql_error());
$rows = mysql_num_rows($query_exec);
//echo $rows;
 if($rows == 0) { 
 echo "No Such User Found"; 
 }
 else  {
    echo "User Found"; 
}
?>
3
Contributors
3
Replies
14
Views
2 Years
Discussion Span
Last Post by lps
0

http://php.net/manual/en/function.mysqli-connect.php

$db_username        = 'PASSWORD'; //database username
$db_password        = ''; //dataabse password
$db_name            = 'DATABASE_NAME'; //database name
$db_host            = 'localhost'; //hostname or IP


$mysqli_conn = new mysqli($db_host, $db_username, $db_password,$db_name); //connect to MySql

//Output any connection error
if ($mysqli_conn->connect_error) {
    die('Error : ('. $mysqli_conn->connect_errno .') '. $mysqli_conn->connect_error);
}
0

I will post the code I posted on similar thread asked here on https://www.daniweb.com/web-development/php/threads/492929/problem-with-my-login-page-facing-with-a-blank-page

<?php
if(isset($_POST['username'])){
    $servername = "localhost";
    $dbname = "test";
    $dbusername = "root";
    $dbpassword = "";
    try {
        $conn = new PDO("mysql:host=$servername;dbname=$dbname", $dbusername, $dbpassword);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $username = $_POST['username'];
        $password = $_POST['password'];
        $password = md5($password);
        $sql = "SELECT Password FROM Users WHERE Username = '$username'";
        $result = $conn->query($sql);

        if ($result->rowCount() > 0){
            foreach($result as $row) {
                if ($password == $row['Password']){
                    echo "Password correct!";
                    //header("Location: admin-area.php");
                }
            }
            echo "Password incorrect!";
        }else{
            echo "Username incorrect!";
        }
    } catch(PDOException $e) {
        echo $sql . "<br>" . $e->getMessage();
    }
    $conn = null;
}
?>
<html>
<head>
    <title>Login Page</title>
</head>
<body>
<form method="post">
Username: <input type="text" name="username"><br>
Password : <input type="text" name="password"><br>
<input type="submit" value="submit">
</form>
</body>
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.