a database connection

Hi
I’m trying to establish connection with a database located on my machine.
I’m using netbeans and wamp server.
The error msq which I’m getting here is “No database selected”.
There is login.php file containing following code:
[embed=425,349][/embed]
then the file includes following code
[php]
$servername=“localhost”;
$conn= mysql_connect()or die(mysql_error());
mysql_select_db(“topdoctor”,$conn);
$sql=“insert into users (username,email,pwd)values(’$_GET[username]’,’$_GET[mail]’,’$_GET[pwd]’)”;
$result=mysql_query($sql,$conn) or die(mysql_error());
[/php]
the database name is topdoctor and the table is called users

Stay away from mysql_query types use PDO or MySQLi

[php]<?php
$db_myHost = “localhost”;
$db_myUser = “dbuser”;
$db_myPassword = “dbpass”;
$db_myDatabase = “topdoctor”;

$dbconn = new PDO(‘mysql:host=’.$db_myHost.’;dbname=’.$db_myDatabase, $db_myUser, $db_myPassword);

try
{
$dbPDO = new PDO(‘mysql:host=’.$db_myHost.’;dbname=’.$db_myDatabase, $db_myUser, $db_myPassword);
$dbPDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e)
{
//echo "Error!: " . $e->getMessage() . "

die();

}

?>[/php]

Have a look at PDO. You’ll need to rewrite your queries (give it a try and if you struggle, post back and we’ll help you out) but I guess that’s better than waking up one day to all of your queries failing due to mysql_query being dropped completely from PHP in future updates: http://de1.php.net/mysql_query

his 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.

Thanks for the replay.
This is just a school project but I’m trying to use PDO as you have suggested.
This database doesn’t have any username and a password (there are blank) so I have changed these variables to empty.
[php]$db_myUser = “”;
$db_myPassword = “”;[/php]
but now I’m getting following error

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [1044] Access denied for user ''@'localhost' to database 'topdoctor'' in E:\install\wamp\www\proces.php on line 12
PDOException: SQLSTATE[HY000] [1044] Access denied for user ''@'localhost' to database 'topdoctor' in E:\install\wamp\www\proces.php on line 12

There must be a username/password somewhere… That’s why it’s saying “Access Denied” as it’s an incorrect username/password.

You will have a file somewhere with your database connection in it.

I’m using wamp server with sql where the admin name is root and i’ve created password for that by editing mysql.user root entry a changed that password to manox and updated my php
[php]if($_GET[“username”] && $_GET[“mail”] && $_GET[“pwd”] && $_GET[“pwdw”] )
{
if($_GET[“pwd”]==$_GET[“pwdw”])
{
$db_myHost = ‘127.0.0.1’;
$db_myUser = ‘root’;
$db_myPassword = ‘manox’;
$db_myDatabase = ‘topdoctor’;

    try
    {
    $dbPDO = new PDO("mysql:host=$db_myHost;dbname=$db_myDatabase");

    }
    catch  (PDOException $e)
    {
    echo "Error!: " . $e->getMessage();

    die();
    }
print "<h1>you have registered sucessfully</h1>";

print "<a href='index.php'>go to login page</a>";
}

else print “passwords doesnt match”;
}
else print"invaild data";[/php]
but still the same error

Error!: SQLSTATE[42000] [1044] Access denied for user ‘’@‘localhost’ to database ‘topdoctor’

I believe the problem is with no username sent to database as the quotes are empty access denied for user ‘’@‘localhost’

the line [php](‘mysql:host=’.$db_myHost.’;dbname=’.$db_myDatabase, $db_myUser, $db_myPassword);[/php]
was missing user and pwd and that was the issue so all sorted :slight_smile:
Thanks for suggestions

No problem. How are you running your queries? Are you using PDO or still the mysql_query?

[php]$sth = $dbconn->prepare(“SELECT username FROM users WHERE password = :password”);[/php]

That’s how your queries should look…

Sponsor our Newsletter | Privacy Policy | Terms of Service