Connecting to a mysql database from php, error but no error shown?

≯℡__Kan透↙ 提交于 2021-01-27 18:55:56

问题


so I'm making a super simple site just for practice where you type something in an then its displayed on the page, but it wont work and i think iv narrowed it down to the begining where it connects to the database just

$server    ="localhost";
$username  ="root";
$password  ="********";
$database  ="user_accounts"; 
$connect=mysqli_connect($server,$username,$password,$database);

if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

Im running my server using Apache/2.2.22 (Debian), PHP 5.4.4-14 and mysql Ver 14.14 Distrib 5.5.31, for debian-linux-gnu (armv7l) using readline 6.2 and I've noticed that even when I type the password incorrectly it doesn't 'echo' the error line, so my question is do I connect to localhost like that or did i do something else wrong in there?


回答1:


Try something like this:

if ($mysqli->connect_errno) {
    printf("Failed to connect: %s\n", $mysqli->connect_error);
    exit();
}



回答2:


Could you Try this

<?php
$mysqli = @new mysqli('localhost', 'root', '*******', 'user_accounts');

if ($mysqli->connect_errno) {
    die('Connection Error: ' . $mysqli->connect_errno);
}
?>



回答3:


Connect like below

<?php
$link = @mysqli_connect('localhost', 'root', '*******', 'user_accounts');

if (!$link) {
    die('Connect Error: ' . mysqli_connect_errno());
}
?>

Below is create user in mysql script on your request

User newuser to connect with no password (which is insecure), include no IDENTIFIED BY clause:

CREATE USER 'newuser'@'localhost';

To assign a password to new created user newuser, use IDENTIFIED BY with the plaintext password value:

CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'mypass';

To avoid specifying the plaintext password if you know its hash value (the value that PASSWORD() would return for the password), specify the hash value preceded by the keyword PASSWORD:

CREATE USER 'newuser'@'localhost'
IDENTIFIED BY PASSWORD '*90E462C37378CED12064BB3388827D2BA3A9B689';

Then you can GRANT access of what ever you want to that newuser

GRANT SELECT,INSERT,UPDATE,DELETE ON user_accounts.* TO 'newuser'@'localhost';

You can use this link to generate the hash code.



来源:https://stackoverflow.com/questions/16683337/connecting-to-a-mysql-database-from-php-error-but-no-error-shown

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!