0

I'm new with mysql and php so please bear with me. I'm trying to connect my first form to a table and I keep running into a new issue every time I "fix" something. I'm trying to test my form to make sure it connects before I move forward. This is the form:

<form action="demo.php" method="post">

<p>input 1: <input type="text" name="input1"/></p>

<input type="submit" value="Submit" />

</form>

and this is my "demo.php" file:

<?php

define('BD_NAME', 'DEMO');
define('DB_USER', 'DEMO');
define('DB_PASSWORD', 'PASSWORD');
define('DB_HOST', 'HOST');

$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);

if (!$link) {
    die('Could not connect: ' . mysql_error());
}

$db_selected = mysql_select_db(DB_NAME, $link);

if (!$db_selected) {
    die('Can\'t use ' . DB_NAME . ': ' . mysql_error());
}

echo 'Connected successfully';

mysql_close();
?>

I keep getting this error:

Can't use DB_NAME: Access denied for user 'USER'@'%' to database 'DB_NAME'

Again I'm new at this and I'd appreciate any help. Thanks!!

Yarseo
  • 47
  • 5
  • You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). – Quentin Aug 28 '14 at 18:59
  • Did you create user & password for your DB? If not so then use `define('DB_USER', 'root');` `define('DB_PASSWORD', '');` `define('DB_HOST', 'localhost');` – MH2K9 Aug 28 '14 at 19:03
  • @Quentin, I'm new and I'll be moving forward as I go. Thanks! I guess I should learn the latest but it's somewhat harder. – Yarseo Aug 28 '14 at 19:04

2 Answers2

1

You have a typo.

define('BD_NAME', 'DEMO'); should be define('DB_NAME', 'DEMO');

Notice the DB_NAME global variable that is not resolve :

Can't use >>>>DB_NAME<<<<: Access denied for user 'USER'@'%' to database 'DB_NAME'

It means that either DB_NAME is not declared OR that the value of DB_NAME is "DB_NAME".

Nico
  • 6,395
  • 4
  • 25
  • 34
  • Awesome @Nico !! I can't believe I missed it lol. Nice, it works now. I'm taking any advice and moving into a modern API as suggested. Cheers – Yarseo Aug 28 '14 at 19:06
  • @YarseoLLC np, have a nice day. Like Quentin said, you should use the latest and greatest :) – Nico Aug 28 '14 at 19:08
0

always use error_reporting(E_ALL) and display errors while coding to avoid these mistakes

vertazzar
  • 1,053
  • 7
  • 10