I have a problem connecting to sql database:
<?php
$servername = "localhost";
$username = "";
$password = "";
$myDB = "udemy";
// Create connection
$link = mysqli_connect($servername, $username, $password, $myDB );
if (mysqli_connect_error()){
die ("There was an error connecting to the database");
}
$query = "SELECT * FROM users";
if (mysqli_query($link, $query)){
echo "Query was successfull"
}
?>
Error is showing when I try to connect database named "udemy"...
Set username and password or add a new user in your mysql server and try with those user details.
To create new user :-
To create user in MySQL/MariaDB 5.7.6 and higher, use CREATE USER syntax:
CREATE USER 'new_user'#'localhost' IDENTIFIED BY 'new_password';
then to grant all access to the database (e.g. my_db), use GRANT Syntax, e.g.
GRANT ALL ON my_db.* TO 'new_user'#'localhost';
Where ALL (priv_type) can be replaced with specific privilege such as
SELECT, INSERT, UPDATE, ALTER etc
Then to reload newly assigned permissions run:
FLUSH PRIVILEGES;
Now set newly created username and password in your code.
Hope it helped.
Related
I want to create a table in a MySQL Database with PHP. This is my try:
$dbhost = 'rdbms.strato.de';
$dbusername = 'Userxxx';
$dbuserpass = 'Passwordxxx';
$dbname = 'DBxxx';
$link_id = mysql_connect ($dbhost, $dbusername, $dbuserpass);
echo "success in database connection.";
if (!mysql_select_db($dbname)) die(mysql_error());
echo "success in database selection.";
$result = "CREATE TABLE address_book (first_name VARCHAR(25), last_name VARCHAR(25), phone_number VARCHAR(15))";
if (mysql_query($result)){
echo "TABLE created.";
}
else {
echo "Error in CREATE TABLE.";
}
But this give me the error
success in database connection.Access denied for user XXX to database XXX
I search a lot but find no successfull solution. Have anyone an idea?
Try connecting without involving PHP:
mysql -u Userxxx -h rdbms.strato.de -pPasswordxxx
If this doesn't work, and I suspect it won't, contact your database administrator to reset your password.
If the password isn't the problem, the remote machine may not be ready to accept your connection. See Access remote database from command line for more on this.
If you can get in, then try to access the database you want:
USE DBxxx;
SHOW TABLES;
Thing is, PHP isn't your problem here; it's the connection. So that's what to investigate.
...and when you get that established, you might look into updating your code; mysql_connect and mysql_select_db are deprecated.
I need some clarification about your hostname,username,password,database name.How ever following code will works in my local server.If you have any doubt about my code please share your host name,password,dbname,etc.
$dbhost = 'localhost';
$dbusername = 'root';
$dbuserpass = '';
$dbname = 'test';
$link_id = mysqli_connect ($dbhost, $dbusername, $dbuserpass,$dbname);
echo "success in database connection.";
$result = "CREATE TABLE address_book (first_name VARCHAR(25), last_name VARCHAR(25), phone_number VARCHAR(15))";
if (mysqli_query($link_id,$result)){
echo "TABLE created.";
}
else {
echo "Error in CREATE TABLE.";
}
I want to Create mysql user and database using php script
how to create mysql user using mysql_query()
<?php
mysql_query("CREATE USER 'demodemodemo'#'localhost' IDENTIFIED BY 'jayul321';");
?>
this is not working..
PHP script to create MySQL database, add user and grant privileges.
## make sure you connect first to mysql with a super user (ex: root)
mysql_connect('localhost', 'root', 'password');
$dbName = 'database_name'; // new database name
$dbUser = 'db_user'; // new user name
$dbPass = 'db_pass'; // new user password
$queries = array(
"CREATE DATABASE `$dbName` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci",
"CREATE USER '$dbUser'#'localhost' IDENTIFIED BY '$dbPass'",
"GRANT USAGE ON * . * TO '$dbUser'#'localhost' IDENTIFIED BY '$dbPass' WITH MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0",
"GRANT SELECT , INSERT , UPDATE, DELETE ON `$dbName` . * TO '$dbUser'#'localhost'",
"FLUSH PRIVILEGES"
);
foreach($queries as $query) {
echo 'Executing query: "'.htmlentities($query).'" ... ';
$rs = mysql_query($query);
echo ($rs ? 'OK' : 'FAIL') . '<br/><br/>';
}
I'm trying to DROP database and its user using PHP.
Though I could able to DROP database but failed to DROP user.
Following command which I have tried,
$cpanel_user = 'abc';
$cpanel_user_password = 'xyz';
$conn = mysql_connect($dbhost = 'localhost', $cpanel_user , $cpanel_user_password );
$sql = 'DROP DATABASE dbname';
$retval = mysql_query( $sql, $conn ); // works perfect
$drop_user = "DROP USER 'username'#'localhost'";
$retval = mysql_query( $drop_user , $conn );
Unfortunately it showing "Access denied; you need (at least one of) the CREATE USER privilege(s) for this operation".
The user which is use for connection is cPanel user so must be having full privileges.
I refereed other threads like, MySQL: Check if the user exists and drop it but no luck.
Do I missing anything?
You should grant 'CREATE USER' Privileges to the user you used to connect to the database.
As your code, you should run this statement by the 'root' user:
GRANT CREATE USER ON *.* TO abc;
I want to know how to connect to a MySql database in a remote server from windows 7 .I want to give an external connection from Administrative panel.How to give connection to the database ? I have a php coding to insert and retrieve values from the remote database .But before that i need to connect to the remote database manually ? Pls can you help me out with this .#
<?PHP
$user_name = "fbapp";
$password = " 123";
$database = "fb_db";
$server = "mysql.dfw1.stabletransit.com";
$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);
if ($db_found) {
$SQL = "INSERT INTO Sample(Firstname, Lastnamme)
VALUES (35,67)");
$result = mysql_query($SQL);
mysql_close($db_handle);
echo "Records added to the database";
}
else {
echo "Database NOT Found ";
mysql_close($db_handle);
}
?>
Simply create a mysql user and allow the same from your host to connect. So create user at
example1.com MYSQL and allow your new user to connect from your example2.com.
Then allow a firewall rule on mysql port from that very host
open command prompt
mysql -u user_name -ppassword -h ip-adress
use ip-adress of machine where mysql is running
I am trying to connect to my database and when I run the code, I get an error. Can anybody tell me what is wrong also any errors in my PHP code? Thanks.
Error: No database selected
PHP Code:
include('.conf.php');
$prof = $_GET['profile'];
$prof = addslashes(htmlentities($prof));
$query = "SELECT * FROM aTable where id = '".mysql_real_escape_string($prof)."'";
$info_array = mysql_query($query, $con) or die (mysql_error()).;
while($row = mysql_fetch_array( $info_array ))
{
echo $row['num1'];
echo "</br>";
echo $row['num2'];
echo "</br>";
echo $row['num3'];
echo "</br>";
echo $row['num4'];
};
mysql_close($con);
.conf.php file:
<?php
$conf['db_hostname'] = "xxx";
$conf['db_username'] = "xxx";
$conf['db_password'] = "xxx";
$conf['db_name'] = "xxx";
$conf['db_type'] = "mysql";
$con = mysql_connect('xxx', 'xxx', 'xxx') or die (mysql_error());
$db = mysql_select_db("aTable", $con);
?>
I had that problem and solved it with prefixing the table name with the database name, so
select * from database.table
Unless you have the password incorrect and need to fix it, run a GRANT statement to grant access to your database user:
GRANT ALL PRIVILEGES ON aTable.* TO xxx#localhost IDENTIFIED BY 'password_for_xxx';
The above grants all privileges. It's often better to restrict to only what's needed. For example, if you only intend to SELECT, but not modify data,
GRANT SELECT ON aTable.* TO xxx#localhost IDENTIFIED BY 'password_for_xxx';
Update
Since you have identified the database name as dedbmysql, change your mysql_select_db() call to:
$db = mysql_select_db("dedbmysql", $con);
Following this, the GRANT statements above are probably unnecessary, as your host may have already worked out the correct database permissions.