-1

I want to create a login system of my website,so I read this page

http://www.codingcage.com/2015/01/user-registration-and-login-script-using-php-mysql.html

and also I create a MySql database

and I got this message:"oops database selection problem ! -->

Access denied for user 'a9891486_UsersID'@'10.1.1.31' to database 'dbtest'"

and I know it is some thing wrong in my dbconnect.php

dbconnect.php code:

<?
if(!mysql_connect("mysql9.000webhost.com","Username","Password","dbtest"))
}
die('oops connection problem ! --> '.mysql_error());
}
if(!mysql_select_db("dbtest"))
}
die('oops database selection problem ! --> '.mysql_error());
}
?>

and I know what is "Username" and "Password"

THANK A LOT!!!!!!!!!!!!!!!!!!!

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Felix Fong
  • 969
  • 1
  • 8
  • 21
  • You probably didn't `GRANT` your user access to the created database, see the [mysql docs on adding users](http://dev.mysql.com/doc/refman/5.7/en/adding-users.html) (especially the [GRANT](http://dev.mysql.com/doc/refman/5.7/en/grant.html)-syntax). Also: The `mysql_*` functions are deprecated and removed in PHP7, you should look into alternatives such as [PDO](http://php.net/manual/en/book.pdo.php). – ccKep Jan 08 '16 at 11:23
  • @Schellingerht it's printing out the selection error, not the connection error, it's `mysql_select_db` that fails, not `mysql_connect`. Although there are probably some braces missing in his post, I give you that. – ccKep Jan 08 '16 at 11:25
  • 1
    I've run a website on that hosting party, you get grantrights and all that good stuff if I remember right. This might be an insultingly dumb question, but did you in fact create the schema? – Glubus Jan 08 '16 at 11:26
  • Please dont use the `mysql_` database extensions, it is deprecated (gone for ever in PHP7) Especially if you are just learning PHP, spend your energies learning the `PDO` or `mysqli_` database extensions, [and here is some help to decide which to use](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – RiggsFolly Jan 08 '16 at 11:31
  • `mysql_connect()` does not accept 4 parameters [The Manual is useful](http://php.net/manual/en/function.mysql-connect.php) Did you mean to be using the `mysqli_` database extension? Oh and see comment above. – RiggsFolly Jan 08 '16 at 11:34
  • Dont use tutorials that describe the use of the old deprecated `mysql_` database extension. Try this one instead (its just the first one on a google search, not a recomendation) http://codular.com/php-mysqli – RiggsFolly Jan 08 '16 at 11:37
  • Not Working,still displaying "oops database selection problem ! --> Access denied for user 'UserName'@'10.1.1.31' to database 'dbtest'" – Felix Fong Jan 09 '16 at 13:14

2 Answers2

0

Try this:

    <?
$conn = mysql_connect("mysql9.000webhost.com", "username", "password") or die(mysql_error());

//to select the targeted database
mysql_select_db("dbtest", $conn) or die(mysql_error());
    ?>
Manoj S Kadlag
  • 250
  • 2
  • 13
  • 1
    Why should the OP "try this"? A **good answer** will always have an explanation of what was done and why it was done that way, not only for the OP but for future visitors to SO that may find this question and be reading your answer. – RiggsFolly Jan 08 '16 at 11:32
  • Still "Access denied for user 'a9891486_UsersID'@'10.1.1.31' to database 'dbtest'" – Felix Fong Jan 08 '16 at 11:33
  • Then you need to Grant privileges to user > grant all privileges on DATABASE_NAME.* to USERNAME@localhost identified by 'PASSWORD'; > flush privileges; > \q using this commands through phpmyadmin – Manoj S Kadlag Jan 08 '16 at 11:35
0

Just a couple of points about the code

  • mysql_connect accepts only 3 parameters the dbname goes in the mysql_select_db()
  • your if statements have incorrect bracketting
  • its always safer to use <?php rather than <?

Suggested code changes

<?php
if(!mysql_connect("mysql9.000webhost.com", "Username", "Password"))
{
    die('oops connection problem ! --> '.mysql_error());
}

if(!mysql_select_db("dbtest"))
{
    die('oops database selection problem ! --> '.mysql_error());
}
?>

But again I have to say

Please dont use the mysql_ database extensions, it is deprecated (gone for ever in PHP7) Especially if you are just learning PHP, spend your energies learning the PDO or mysqli_ database extensions, and here is some help to decide which to use

Community
  • 1
  • 1
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149