/    Sign up×
Community /Pin to ProfileBookmark

No database selected

Sir, I have these codes

[code=php]<?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]

Code says:
No database is selected.

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

What i am doing wrong?

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@sathish-wdDec 11.2013 — if you are going to use "mysql_connect"

[I]

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

$connect = mysql_select_db($con,$db_name);

[/I]


if you are going to use mysqli_connect

[I]

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

[/I]
×

Success!

Help @tqmd1 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.1,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...