1

I have reinstalled the newer versions of XAMPP, PHP, MySQL and also Elclipse Mars and XDebug. The database is accessible by PHPMySdmin.

In PHP I connect the user to the server

    $x = mysql_connect('localhost', 'user');
    echo "Connect=($x)<BR>";
    if (!$x) echo "Connect failed";

This connects fine when I don't use the user's password. It fails when I do.

Then I try to select the database with

    $db = 'database';
    $b = mysql_select_db($db, $x);

This fails. So does

    $b = mysql_query("USE $db", $x);

I have granted users all permissions.

Obviously I am missing something. Do I have to associate the user to the database in phpMyAdmin? How? Something else?

My configuration.

XAMPP 3.2.2 PHP 5.5.30 Eclipse Mars (4.5.1) XDebug 2.3.3

Thank you.

Isaac Bennetch
  • 11,830
  • 2
  • 32
  • 43
  • Yes, you need to have a mysql user that has the appropriate privileges for the database. You should also use mysqli since mysql is depreciated. – Tristan Dec 13 '15 at 02:52
  • I just connected with 'root' rather than 'user'. I was able to select 'database'. So my problem is figuring out PhpMyAdmin to set up the user correctly. – user3637858 Dec 13 '15 at 17:31
  • **Warning**: 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 Dec 14 '15 at 20:06

2 Answers2

1

I suggest to try the first example of the mysql_error manual:

http://php.net/manual/en/function.mysql-error.php

Then you should receive the error message and know more about what's going on.

Santy
  • 387
  • 2
  • 7
  • Santy, to strip my code to the bare essentials, I didn't post the mysql_error() calls. It said the user didn't have permission to access the database. I'll look into mysqli. – user3637858 Dec 13 '15 at 16:53
  • mysqli would indeed be a wiser choice today. – Santy Dec 13 '15 at 17:10
0

You can allocate privileges to a mysql user for a specific database in PHPMyAdmin with the following steps:

  1. Click on the Users tab
  2. Click on Edit Privileges for the user you want to change/add
  3. Click the Database'button' (at the top in recent versions of PHPMyAdmin, or in older versions you select the database from the dropdown half way down the page)
  4. Select the database from the list and click Go
  5. Choose the appropriate privileges
  6. Make sure you save by clicking Go, then try your code again.
Tristan
  • 3,301
  • 8
  • 22
  • 27