So I connect to my MySQL database using the following code:
function dbConnect($h,$u,$p,$n) {
if (!$con = #mysql_connect($h,$u,$p)) {$err = err("There is a problem connecting to the database. ".mysql_error());}
else if (!#mysql_select_db($n,$con)) {$err = err("The database \"{$n}\" could not be found. Check your spelling, make sure the database exists, and that your database credentials allows you access to this database.");}
return (isset($err)) ? $err : "";
}
The problem is, if they put in a wrong username, mysql_connect will not see anything wrong with it and try to connect to the database, which outputs an error for the mysql_select_db().
So I found this link. Now normally removing the "any" user would be doable but I'm creating a phpMyAdmin-like tool and will be connecting to different databases with all types of configurations. How can I tell which username can actually connect? And how can I differentiate between the database name not existing and the username not having access to it?
use mysql_errno()
see error codes here
Related
I am making a small database application for the experience, but I am having a problem with actually accessing the database from my PHP code. I've tried accessing the file name, accessing the separate mysql file. But whatever I do, I get an error message
<?php
$password = md5("passhash");
$user = array("name"=> "JohnDoe", "password" => $password, "email" => "email#email.com");
try {
$db = new PDO("mysql:host=localhost; dbname=users.ibd; port=8889", "root", "root");
...
}
Unable to connectSQLSTATE[HY000] [1049] Unknown database 'users.ibd'
Do you guys have any experience with this error message and what I could possibly be doing wrong when it comes to declaring the database name in my PDO initialization?
please check on your db name... if exist on your list of database.
Try to check out on mysql console with command SHOW DATABASES;
You should connect with the table which apears on mysql console, not with the file on mysql folder.
I looked. The .ibd was actually throwing off what was database name. Thanks Guys for the help.
The line I'm trying to use is :
use foodorder;
I get a reply specifying:
#1044 - Access denied for user 'u548475702_foody'#'localhost' to database 'foodorder'
P.S. I'm not using the database with a hostname of localhost, I'm using mysql.hostinger.in as specified in their website.
My php code is:
$dbc = mysqli_connect('mysql.hostinger.in','u548475702_foody','not the actual password','u548475702_food')
or die("Error connecting to database!");
I've also ensured that the associated user has all the required permissions:
So why am I still getting this message?
I have one file on one web serve and my phpmyadmin SQL databases on a seperate server and am currently trying to connect to my external serve but it doesnt seem to work.
I keep getting the error message:
'Access denied for user ''#'localhost' (using password: NO)'
and am not sure what this means.
Any help would be greatly appreciated or even an article to get started on figuring this out since I cant seem to find anything myself.
Thank you
EDIT --- PDO SCRIPT ---
My PDO Script is the generic one from w3, I have place holders for security reasons.
<?php
$servername = "externalIP";
$username = "username";
$password = "password";
try {
$conn = new PDO("mysql:host=$servername;dbname=Reservations", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
?>
you have to find your phpmyadmin page where you login using also a password.
Try this one :if you have not assign password into database for specific user then you just have to apply 'root' as user and leave blank password field and also set your hostname as 'localhost' or 127.0.0.1
also remember that you have to start first apache and mysql services before access.
If you are using remote server as database then not required.in that case you have to apply your remote Ip address and also remember that in that remote server if you have not specific user then leave password as blank. one another way to debug mysql connection into your remote server creating mysql connection file with its required paramter to check that your remote server ok or not.
This error will come most probably because of user permission issue. Make sure you have privilege to that user for that database.
I've created a simple script using PHP and MySQLi. The purpose is to create a new user on a MySQL server, however I get this error message: "user create query failed:Access denied; you need (at least one of) the CREATE USER privilege(s) for this operation."
$db = "xsxx";
$user = $_POST["user"];
$pass = $_POST["pass"];
$con=mysqli_connect('127.0.0.1',"root","",$db);
$cruser= "CREATE USER '".$user."'#'localhost' IDENTIFIED BY '".$pass."';";
#mysqli_query($con, $gruser) or die(mysqli_connect_error());
if(mysql_query($cruser)){
echo 'user created<br/>';
} else{
echo 'user create query failed:'.mysql_error().'<br/>';
}
mysqli_close($con);
Anyone know what I'm doing wrong? If it's important, I'm using XAMPP for the MYSQL server. Thanks in advance.
You are mixing the old and new style mysql calls (mysql vs mysqli).
The mysql_query call you are issuing will use whatever username and password was specified during the mysql_connect call, and importantly, not the username and password you specified in the mysqli_connect call.
If you are not issuing a mysql_connect() yourself (it's not shown in the code you posted) then the mysql_query will call mysql_connect with no arguments which leads to a blank username and password.
By whichever method it's being called, the mysql_connect user does not have the "CREATE USER" privilege and so the error message is generated.
You can use SHOW GRANTS FOR 'someuser'#'localhost'; to check what a user's permissions are.
You can also use the GRANT statement to add additional permissions to users.
An example of adding the create user permission would be:
GRANT CREATE_USER ON *.* TO 'someuser'#'localhost''
One way to resolve the issue, assuming root has all permissions, is to change your mysqli_connect to a mysql_connect such as: mysql_connect('127.0.0.1','root','')
Okay I'm having trouble connecting my database to my host..
Im using myPHPAdmin and I made a database on my host
the problem is in my php code where I define my connection to the host
everything worked fine when I did this on the localhost.
But now that I want to use it on my personal domain it wont work
it doesnt access the data in the database.
Im new at this so dont shoot me ;)
$connect = mysql_connect("host", "user", "pass");
so for my host/domain I enter my domain the-bloodgod.com ?
what do I enter for user and pass? is it just the login I normally use to access the Cpanel? Or do I have to create special permissions in myphpadmin?
also on my hosts myphpadmin it shows that it stores all created tables in the_bloodgod_com database collection
so would this be correct if I put it the code bellow?
mysql_select_db("the_bloodgod_com");
$sql="SELECT * FROM tablename";
MOST hosting companies you have to use "localhost" as the host name for your database connection code. PHP:
$link = mysqli_connect("localhost","username","password","database") or die("Error " . mysqli_error($link));