0

I know that this question has been asked probably a million times, but none of them have solved this problem. I am using PHP to connect to my localhost databse with the PHP script show below:

<?php


// CONNECT TO THE DATABASE
$DB_NAME = 'users';
$DB_HOST = 'localhost';
$DB_USER = '********';
$DB_PASS = '********';
$mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}

// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword']; 

// A QUICK QUERY ON A FAKE USER TABLE
$query = "SELECT * FROM `members` WHERE `username`='$myusername' and `password`='$mypassword'";
$result = $mysqli->query($query) or die($mysqli->error.__LINE__);

// GOING THROUGH THE DATA
if($result->num_rows == 1) {
header("location:login_success.php");
}
else {
header("location:fail.php");    
}
// CLOSE CONNECTION
mysqli_close($mysqli);

?>

This is able to connect to the database, but it is unable to find the database 'users'. You can see from the image listed below, that the database exists, but it can't find it.

https://i.stack.imgur.com/A5uuL.png

Any help that will resolve this would be greatly appreciated!

Synposis
  • 19
  • 1
  • 6
  • 2
    Your user has permission to access to users DB? – Emilio Gort Jul 07 '13 at 07:09
  • 2
    mysql_* == deprecated. – Daniel Kmak Jul 07 '13 at 07:09
  • 2
    [either use PDO or mysqli since mysql_* api is deprecated](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php/14110189#14110189) – NullPoiиteя Jul 07 '13 at 07:10
  • 1
    IIRC session_register() also is goign to be deprecated soon, but can't confirm this claim now, altough the manual says it exists upt to v. 5.4 (well, this might be a yes). Use $_SESSION['var'] to create a session variable – Damien Pirsy Jul 07 '13 at 07:13
  • I have edited my post with new code using mysqli, but im getting this error now: Warning: mysqli::mysqli(): (HY000/1049): Unknown database 'users' in E:\Sites\checklogin.php on line 9 Connect failed: Unknown database 'users' – Synposis Jul 07 '13 at 07:16
  • my impression is you dont have acces to users db or you are connecting to another db server, check your credentials – Emilio Gort Jul 07 '13 at 07:30
  • I'm confused: 'it can connect' but 'it cannot connect' – Strawberry Jul 07 '13 at 07:34
  • It can connect, but can't find the database. Emilio, it is localhost, and all of my credentials are right. – Synposis Jul 07 '13 at 07:39

2 Answers2

0

you can try this in your phpmyadmin

GRANT SELECT, INSERT, DELETE ON database TO username@'localhost' IDENTIFIED BY 'password';

then try to run your php code.

Dr_Dang
  • 452
  • 6
  • 15
  • I have access to the database, i made sure of this before posting here. The command has been run, however, i might need to re run with the password. Will try now! EDIT: Adding IDENTIFIED BY didn't help – Synposis Jul 07 '13 at 07:21
  • sometimes GRANT command changes works after flushing preveliges. run this -- **FLUSH PRIVILEGES;** – Dr_Dang Jul 07 '13 at 07:27
  • Like i said above, it connects, but can't find my database on the localhost – Synposis Jul 07 '13 at 07:41
0

You are mixing the OOP interface to mysqli with the procedural interface.

Try this:

$mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);

if ($mysqli->connect_errno) {           // Changed to OOP version
  printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
  • It was worth a shot, but it still won't find the database in mysql. It doesn't see it at all. http://i.imgur.com/bHj9XeA.png – Synposis Jul 07 '13 at 13:36
  • If you guys want to see for yourself what happens, please go to www.whoerfastdl.com (My website) and login with the credentials username: john - password: 1234 – Synposis Jul 07 '13 at 14:14
  • I trawled through some results on a search for the error code. I found this link: [http://forums.sugarcrm.com/f3/unknown-database-cron-php-77061/](http://forums.sugarcrm.com/f3/unknown-database-cron-php-77061/). This guy inadvertently had two instances of MySql running, and his PHP script was connecting to the wrong one. There's a fair amount of detail about the process he went through to fix it. –  Jul 07 '13 at 16:38
  • OK, in at the movies right now, but i will look into it when I get home – Synposis Jul 07 '13 at 19:02
  • Good work man. Worked like a charm! Thank you very much for all the help. – Synposis Jul 07 '13 at 23:25