scalar2 Posted December 11, 2016 Posted December 11, 2016 Hi, i try to connect to my MySQL-Database with this PHP-Code: <?php if(isset($_GET["value"])){ $value = $_GET["value"]; $conn = new SQLConnection(); $conn->insertData($value); } class SQLConnection{ private $servername = "localhost"; private $username = ""; private $password = ""; private $dbname = ""; function __construct(){ } function insertData($value){ $conn = new mysqli($servername,$username,$password); if($conn->connect_errno) echo "Couldn't connect to database. " . $conn->errno . $conn->connect_errno; else{ $conn->close(); } } } ?> I created a second MySQL user explicitly for this database and i am 100% sure that i use the correct name / password. But i always get: Couldn't connect to database. 1045[/size] I found this in the error log: PHP Warning: mysqli::mysqli(): (HY000/1045): Access denied for user ''@'localhost' (using password: NO) in /home/scalar/public_html/file.php on line 17PHP Warning: SQLConnection::insertData(): Couldn't fetch mysqli in /home/scalar/public_html/file.php on line 19 Quote
wolstech Posted December 12, 2016 Posted December 12, 2016 The only reasons for #1045 are either the DB is not assigned to the DB user, or the username and password are wrong. If you're sure the password is right, make sure the username is in the form scalar_dbuser (all database users will start with your account name and a _ ), and make sure the DB has been assigned to the user. Quote
scalar2 Posted December 12, 2016 Author Posted December 12, 2016 Thanks for your reply, i shortended my code a bit: <?php if(isset($_GET["value"])){ $value = $_GET["value"]; $conn = new SQLConnection(); $conn->insertData($value); } class SQLConnection{ public $servername = "localhost"; public $username = "scalar_sql"; public $password = ""; public $dbname = "scalar_measurements"; function __construct(){ } function insertData($value){ $link = mysqli_connect($servername,$username, $password, $dbname); if (!$link) { die('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error()); } echo 'Success... ' . mysqli_get_host_info($link) . "\n"; mysqli_close($link); } } ?> As you can see here, i created a new user and assigned it to the database but i still get the same error, i don't know what i am doing wrong In particular i don't understand why it is Access denied for user ''@'localhost'" , why doesn't the real username show up? Quote
Krydos Posted December 12, 2016 Posted December 12, 2016 The problem is with how you're passing the variables in. Check out http://scalar.heliohost.org/sql2.php?value=1 1 Quote
scalar2 Posted December 12, 2016 Author Posted December 12, 2016 Oh, thanks alot! Could you explain why it doesn't work if i just pass the variables, please? Quote
Krydos Posted December 12, 2016 Posted December 12, 2016 I'm not completely sure, but it probably has something to do with the way you're calling your functions and the way you're assigning values to the variables. The way I troubleshoot something like this is to echo variables at various places in your code, and make small changes at a time. PHP errors tend to not be very informative. 1 Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.