1

On my local phpmyadmin, I have created two databases with the names:

  • fast
  • financialanalysisautodb

This is how they appear on the phpmyadmin: enter image description here

From my php application I tried accessing the databases as below:

// Create connection
 $fastdb = new mysqli("localhost", "root", "", "financialanalysisautodb") or die(mysql_error()); 
 if ($fastdb->connect_error) {
     die("Database connection failed: " . $fastdb->connect_error);
 }

The above code works fine if I access financialanalysisautodb. However, if I try to access the "fast" db:

 // Create connection
 $fastdb = new mysqli("localhost", "root", "", "fast") or die(mysql_error()); 
 if ($fastdb->connect_error) {
     die("Database connection failed: " . $fastdb->connect_error);
 }
 

I get the following error message:

Warning: mysqli::__construct(): (HY000/1049): Unknown database 'fast' in C:\xampp\htdocs\AutomatedFinancialAnalysis\fastlogin.php on line 19 Database connection failed: Unknown database 'fast'

Can someone help me solve this problem? Thank you in advance.

  • is in the same page the two connection? – Simone Rossaini Jul 09 '20 at 08:22
  • `mysql_error()` is not going to help you here because a) it's the wrong API, and b) it doesn't deal with connection errors. https://www.php.net/manual/en/mysqli.connect-error.php would help. But...since you're already getting a warning then I'd say mysqli is already set up to throw errors and warnings when things go wrong, and therefore you don't actually need the `or die...` bit at all. Instead of blindly copying bits of code whose purpose you don't fully comprehend (I assume you don't, otherwise you'd have been unlikely to use it), examine your setup and work out what you actually _need_. – ADyson Jul 09 '20 at 08:30
  • Sounds like the user might not have proper privilege to see the `fast` database, but then again that would be rather weird for the `root` user. Check what users have what privileges on the database, and from what connecting hosts. – CBroe Jul 09 '20 at 08:40
  • @Simone Rossaini No they are both in two different pages. financialanalysisautodb is on the page that has the form and fast is on the page that processes the form. – user2749166 Jul 10 '20 at 01:55

0 Answers0