0

I try to connect to my database that I have in my Mysql Workbench from my php application. I have already connected to this database from my other Java application, using JDBC, like following:

public class TestConnect {

    static String USERNAME = "root";
    static String PASSWORD = "mypassword123";
    static String CONN_STRING = "jdbc:mysql://localhost:3306/sources?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
    static Connection CON = null;
    
    public static void main(String[] args) throws SQLException, IOException, ParserConfigurationException, SAXException {
        Connection con = null;
        try {
            con = DriverManager.getConnection(CONN_STRING, USERNAME, PASSWORD);
            CON = con;
        } catch (SQLException e) {
            System.err.println(e);
        }
    }
}

This works. Now I want to connect to the same database from my website, that I'm creating using php. So what am I trying so far is the following:

database.php

<?php
$host = 'localhost';
$username = 'root';
$password = 'mypassword123';
$dbname = 'testDB';

$mysqli = new mysqli($host,$username,$password,$dbname);
if ($mysqli->connect_errno) {
    echo '<strong>Could not connect to server.</strong>';
} else {
    $mysqli->query("SET CHARACTER SET 'utf8'");
}

However, when I open my website (on localhost), I only get 'Bad Gateway' warning. Can you tell me what I'm doing wrong or what am I missing?

enneenne
  • 209
  • 1
  • 8
  • 3
    `Mysql Workbench` is a tool NOT a database. MySQL is a database – RiggsFolly Jan 19 '21 at 12:24
  • Is your database on the same machine as your website code? – RiggsFolly Jan 19 '21 at 12:27
  • Yes, everything is on the same machine. Ok, it's a tool, I meant I want to connect to my Mysql database. – enneenne Jan 19 '21 at 12:32
  • If you're just getting started with PHP and want to build applications, I'd strongly recommend looking at various [development frameworks](https://www.cloudways.com/blog/best-php-frameworks/) to see if you can find one that fits your style and needs. They come in various flavors from lightweight like [Fat-Free Framework](https://fatfreeframework.com/) to far more comprehensive like [Laravel](https://laravel.com/). These give you concrete examples to work from and guidance on how to write your code and organize your project's files. – tadman Jan 19 '21 at 12:34
  • 1
    Tip: A lot of problems can be detected and resolved by [enabling exceptions in `mysqli`](https://stackoverflow.com/questions/14578243/turning-query-errors-to-exceptions-in-mysqli) so errors resulting from simple mistakes made aren’t easily ignored. Without exceptions you must pay close attention to return values, many of these indicate problems you must resolve or report to the user. Exceptions allow for more sophisticated flow control as they can “bubble up” to other parts of your code where it’s more convenient to handle them. – tadman Jan 19 '21 at 12:35
  • "Bad Gateway" is primarily an issue with your PHP installation and has nothing to do with MySQL. Can you confirm your PHP situation is operational first? Does a script with nothing more than `` do anything? – tadman Jan 19 '21 at 12:35
  • It does have with MySql, I finally resolved my issue with "Bad Gateway", however now when I try to include my database.php file, I get this error: `Warning: mysqli::mysqli(): (HY000/1045): Access denied for user 'root'@'localhost' (using password: YES) in C:\USBWebserver\root\TestConnect\database.php on line 7` – enneenne Jan 19 '21 at 12:42
  • Ok well that just means you used the wrong password. but your web app should not be logging in as root anyway, that's a security mistake. Create a separate account for the application which only has the permissions it actually needs in order to operate – ADyson Jan 19 '21 at 12:54
  • I don't think my password is problem (I type exactly the same as in JDBC, where it works). Might this have something with my mysql database not existing in phpmyadmin or me trying to view my page using usbwebserver? – enneenne Jan 19 '21 at 13:16
  • `I don't think my password is problem`...well "Access denied" tells you quite clearly that it is. The computer doesn't lie. It's unclear what phpmyadmin would have to do with this. I don't know how you've configured that, so I can't comment or relate it to this situation. And I've never heard of usbwebserver or know what it does. Feel free to add more context. Anyway...I can only think that either a) you're incorrect about the password, or b) you're somehow connecting to a different instance of mysql. Is the Java code and the PHP code both running on the same PC? – ADyson Jan 19 '21 at 13:26
  • Yes, I have Netbeans IDE with my running java application. There, I connect to my mysql database using JDBC. Using exactly the same username and password, I try to connect to the database from Phpstorm from my php file. The phpstorm project is located in my root folder inside usbwebserver, which I also run at the same time. – enneenne Jan 19 '21 at 13:39
  • And is usbwebserver running on the same machine as the java? Does usbwebserver create a separate instance of mysql perhaps? Like I said, please don't assume that we know what this product is or what it does – ADyson Jan 19 '21 at 14:30
  • I found it anyway btw. https://www.usbwebserver.net/webserver/ says it comes with its own version of mysql, and it even tells you what the password is for it – ADyson Jan 19 '21 at 17:45

0 Answers0