So I managed to fix it myself, unsure how tho, this is my final code:
//create user
$con=mysqli_connect("localhost","root","******");
$crearuser="CREATE USER 'phpuser'#'localhost' identified by '******'";
if (mysqli_query($con,$sql))
{
echo "It Works";
}
else
{
echo "Nope";
}
Like I said, im unsure of how I fixed it, but this and the grant part (not going to paste, so much code) are fully working, thank you all :)
Just reference to it: http://dev.mysql.com/doc/refman/5.1/en/adding-users.html
$createuser = "CREATE USER 'phpuser'#'localhost' IDENTIFIED BY 'some_pass';";
Your Script runs fine on my side. I think you have problem in connecting with database
$con = mysqli_connect("localhost","root","******","mysql");
Because during the editing process you changed your password parameter 3 times. And you are not sure about your password.
Try to figure out your mysqli_connect() parameters.
This question will help you too..How to find out the username and password for mysql database
Related
I can't figure out why I'm getting the Commands out of sync; you can't run this command now error. I can't seem to find a way to fix this.
As you can probably see from the code, I need to execute TWO SQL statements. One to check if the password is correct, and the other, which runs only if the previous one returns true, to store all user data in session variables.
Here is my code:
$check_pw_query = $conn->query("CALL checkPassword('{$username}', '{$password}')");
$check_pw_fetched = $check_pw_query->fetch_assoc();
$check_pw_query->close();
if($check_pw_fetched['password_correct']) {
unset($check_pw_fetched);
if(!$get_user_data = $conn->query("CALL getUserData('{$username}')")/*->fetch_assoc()*/) {
echo $conn->error;
}
$_SESSION["user_username"] = $username;
$_SESSION["user_id"] = $get_user_data["id"];
$_SESSION["user_name"] = $get_user_data["name"];
$_SESSION["user_email"] = $get_user_data["email"];
$_SESSION["user_type"] = $get_user_data["type"];
$conn->close();
unset($conn);
send_home(false);
}
Help is much appreciated.
Thank you
Ok, as there were no real helpfully answers, I'll answer it myself, to hopefully help someone else with the same problem.
The problem is that, whenever you call a stored procedure, it will output one more result that defined anywhere. So if your procedure returns 1 value, php will get 2. The last one is only there to tell you that there are no more values. BUT you have to deal with that one as well to free the connection ($conn in my example).
So, that you do is, simply, add this to your code, right after the $check_pw_query->close(); :
$conn->next_result();
And there you have it. It will now work perfectly fine.
An application was handed to me by an old colleague to manage. However, whenever I try to run it, I keep getting this error. Please any help would do.
Sometimes Access denied Exception Error because your mysql credentials are invalid.
Secondly, from my experience i observed that this also happens because you did not set password to your database connectivity. eg
private $host = "localhost";
private $db_name = "db_dbtest"; // Database name
private $username = "db_user"; // your database username
private $password = "db_password"; // Your password
public $conn;
}
Try and set password to your database connectivity. I had such experience and after changing my Collation to utf8_general_ci on the Operations tab, this could not solve my problem. I thought of adding password to my database connection and immediately it connected.
You can tesrun this and see if it helps.
You're getting Access denied Exception Error because your mysql credentials are invalid.
This is due to the wrong sql information provided by you.Changing it to the right one can solve this error .
I was getting this same error code. I'm using phpMyAdmin and added the database with the import of a sql file. The website was using all the same code that had worked before. To fix this error go inside phpMyAdmin and click on the database then operations tab. Change the collation of my database to "ut8_general_ci". Afterwards it was able to verify the username/password access to the database.
Also you might want to make sure you added the user accounts tab the username,password and privileges to access the database.
I know this is an old question, but for me I had to set my password in Sequel Pro to the password that was listed in my .env config file. Hope this helps someone!
Well it means the SQL credentials you are using are wrong there is nothing much we can do, you need to use the correct ones or use the root account to change the user's password or rights.
You have to specify the port you are using for your MySQL. for me, I was changed the port of MySQL like 3307.
example:localhost:3307
The first thing you should check is your credentials. I thought i had root as username and password but when i looked in the file C\xampp\phpMyAdmin\config.inc.php under
$cfg['Servers'][$i]['password'] = 'WhateverPassword';
I saw that i had a different default password. You should use or change the password that is in that file when trying to connect to your database.
Make sure you have the right credentials and these are being passed to the database connection command.
Also make sure to either wrap the credentials in single quotes ' or escape them with backslash \, as there are some special characters not recognised. Hope this helps someone. Here's a reference:
String Literals
The site you got handed has the login credentials for your colleague's machine/site. Depending on the site, different files need to be changed. Example, Wordpress usually has the settings in a file called wp-config.php
Until you find this file, maybe you can change your XAMPP user login to match. This article on SO tells how to change SQL login credentials:
How do I use MySQL through XAMPP?
I'm having a problem with a PHP-Script, where I want to check, if the MySQL-Logindata are valid. I found somewhere the mysql_ping() function, but it doesn't work as it should, because it returns in every case true, even if the inserted data are totally wrong. How do I solve it?
$verbindung = mysql_connect($_POST["mysql_host"], $_POST["mysql_user"],
$_POST["mysql_pwd"]);
if (!mysql_ping($verbindung)) {
$break = true;
}
else {
// Check here also via SQL, if database $_POST["mysql_db"] exists
}
This already is more complex than it needs to be and should do the correct thing. The reason why all usernames work quite likely is that MySQL by default has an anonymous user ''#'localhost' which accepts any username. Probably you want to remove that user. (DROP USER ''#'localhost', be sure you can login as other user before doing that, use SHOW GRANTS to see which user you are using)
For simplification mind that the connect cal will fail if there is something wrong, so you wont need that ping call. ping can be used if you have a longer living connection and you want to check whether the connection is still working.
A simple form for the check might look like this:
$verbindung = new mysqli($_POST["mysql_host"], $_POST["mysql_user"], $_POST["mysql_pwd"]);
if (!$verbindung) {
echo "Wrong settings";
}
Mind that I changed to the mysqli insterface. the old mysql extension in PHP providing the mysql_* functions is deprecated and shouldn't be used anymore.
I actually found out a nicer solution with the mysqli_connect_errno() function and a requirement of inputs from the database name and the user name.
$verbindung = #mysqli_connect((!empty($_POST["mysql_host"]) ? $_POST["mysql_host"] : 'localhost'),
$_POST["mysql_user"], $_POST["mysql_pwd"], $_POST["mysql_db"]);
if (mysqli_connect_errno() || empty($_POST["mysql_user"]) || empty($_POST["mysql_db"])) {
$break = true;
}
This is the code that connects to my SQL database. I'm new with this stuff and it seems to be semi-working but certain features on my website still don't work.
<?php
$con = mysql_connect("localhost","username","password");
$select_db = mysql_select_db('database1',$con);
/*$con = mysql_connect("localhost","username2","password2");
$select_db = mysql_select_db('database2',$con);*/
?>
This is the site in question: http://tmatube.com keep in mind the credentials above are filled in with what the programmer used for testing on his own server... ;) unfortunately I don't have access to him for support anymore.
Anyway, here's my thoughts on how this code needs to be edited maybe someone can chime in and let me know if I'm correct in my assumptions:
<?php
$con = mysql_connect("localhost","username1","password1"); -------------<<< leave this line
$select_db = mysql_select_db('DATABASE_NAME_HERE',$con);
/*$con = mysql_connect("localhost","DB_USERNAME_HERE","DB_PASSWORD_HERE");
$select_db = mysql_select_db('DATABASE_NAME_HERE',$con);*/
?>
Ok - now on to a few problems I noticed...
What does this do? /* code here */? It doesn't work at all if I leave that bit in.
Why is it connecting to database twice? and is it two separate databases?
$select_db = mysql_select_db('DATABASE_NAME_HERE',$con); <<<---- single '
When I tried to see if that line was correct the examples I saw had quotes like this
$select_db = mysql_select_db("DATABASE_NAME_HERE",$con); <<<---- double "
Which one is right?
He didn't leave it out. What he did was leave the database to be connected using the root, which has no password. The other connection (which is commented out) is using another user, rajvivya_video, with a password defined.
In testing it MIGHT be okay to connect to root and leave it without password, but even that is not recommended, since its so easy to work with a user and password defined (besides root).
Here is php mysql connect with mysqli:
<?php
$link = mysqli_connect("myhost","myuser","mypassw","mybd");
?>
No difference here with ' or ". (Anyway use mysqli and you can the wanted db as 4th parameter.) php quotes
/* comment */ is a commented out so the php does not care what is inside so only 2 first rows of are affecting (they are same mysql database on the local machine and 2 different user + password combinations). Comment in general are used to explain the code or removing part of the code with out erasing it. php commenting
Just to get started, and thinking I needed a "database," I did this:
$db = new PDO("java:comp/env/jdbc/mysql");
$stmt = $db->query("CREATE DATABASE kitty_db");
To see if it worked I commented out the above and then wrote:
$link = mysql_connect('localhost:3306', 'me', 'blah');
$db_list = mysql_list_dbs($link);
while($row = mysql_fetch_object($db_list)) {
echo $row->Database ."<BR>";
And I saw that my new database was there:
information_schema
mysql
kitty_db
performance_schema
test
And so my first question is, did I even need to make a new database next to mysql just to get started on something? I don't recall ever having to do that a couple of years ago (7 actually) when I was setting up MySQL before (sans via PHP).
Anyway, I'm wondering why I can't create a table now. If kitty_db isn't a good idea, let's take it out. But I may be having trouble putting a TABLE 'milk_bowl' (with an index or key or whatever 'bowl_name' field).
Thanks for any help. Things have gotten more complex since I just opened up a command line in MySQL almost a decade ago and just issued simplistic queries.
And so my first question is, did I
even need to make a new database next
to mysql just to get started on
something? I don't recall ever having
to do that a couple of years ago (7
actually) when I was setting up MySQL
before (sans via PHP).
This question is kind of vague. You are asking about needing to make a database to start on something? Without know more about your something, I don't really think that question is answerable. Creating the database is usually the first step when setting up a database. You might find this helpful when getting started.
Anyway, I'm wondering why I can't
create a table now. If kitty_db isn't
a good idea, let's take it out. But I
may be having trouble putting a TABLE
'milk_bowl' (with an index or key or
whatever 'bowl_name' field).
From the above link (adapted to your example):
<?php
$con = mysql_connect("localhost:3306","me","blah");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
// Create database
if (mysql_query("CREATE DATABASE kitty_db",$con))
{
echo "Database created";
}
else
{
echo "Error creating database: " . mysql_error();
}
// Create table
mysql_select_db("kitty_db", $con);
$sql = "CREATE TABLE milk_bowl
(
bowl_name varchar(15),
)";
// Execute query
mysql_query($sql,$con);
mysql_close($con);
?>
Thanks for any help. Things have
gotten more complex since I just
opened up a command line in MySQL
almost a decade ago and just issued
simplistic queries.
I don't think that's changed all that much. You can still use the command line to connect to your mysql db and issue queries directly. PHP just lets you do it through a browser/scripts.
This is not a direct answer to your question, but it might solve your problem... have you tried using an application such as MySQL Administrator or MySQL Query Browser? I came back to MySQL recently after a very long hiatus as well and found them both very helpful.