create database if not exists php mysql - php

I'm trying to create a database if it does not exist. Code is here:
<?php
// Connect to MySQL
$link = mysql_connect('host', 'user', 'pw');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
// Make studentdb the current database
$db_selected = mysql_select_db('StudentDB', $link);
if (!$db_selected) {
// If we couldn't, then it either doesn't exist, or we can't see it.
$sql = 'CREATE DATABASE StudentDB';
if (mysql_query($sql, $link)) {
echo "Database StudentDB created successfully\n";
} else {
echo 'Error creating database: ' . mysql_error() . "\n";
}
}
mysql_close($link);
?>
I'm getting this error:
Error creating database: Access denied for user 'myusernamehere' to database 'StudentDB'
the user has dba permissions...I'm guessing this is a permission error for my user....how can I give the user permission through the script and make this script work?

the users you are trying to use have no priviledges to use CREATE
try to switch to another user that have all the priviledges needed on manipulting the database or
add the needed priviledges to the user you are using

it happend to me and the following solved the problem:
first before using code to editing the database go to the main
localhost/index.php page then choose phpMyAdmin database manager
after than make sure that you are loged in as admin

Related

How to modify PHP password connection to the database?

I have only access to a source code (connect.php) from previous developer as
<?php $_F=__FILE__;$_X='Pz48P3BocA0KNG40X3M1dCgnZDRzcGwxeV81cnIycnMnLCdPZmYnKTsNCiRjMm4gPSBteXNxbF9jMm5uNWN0KCJsMmMxbGgyc3QiLCJyMjJ0IiwibW0xYWFhOSIpOw0KbXlzcWxfczVsNWN0X2RiKCJtNHRyMSIsICRjMm4pOw0KNGYgKCEkYzJuKQ0KICB7DQogIGQ0NSgnSzJuNWtzNCBnMWcxbCAhOiAnIC4gbXlzcWxfNXJyMnIoKSk7DQogIH0NCj8+';eval(base64_decode('JF9YPWJhc2U2NF9kZWNvZGUoJF9YKTskX1g9c3RydHIoJF9YLCcxMjM0NTZhb3VpZScsJ2FvdWllMTIzNDU2Jyk7JF9SPWVyZWdfcmVwbGFjZSgnX19GSUxFX18nLCInIi4kX0YuIiciLCRfWCk7ZXZhbCgkX1IpOyRfUj0wOyRfWD0wOw=='));?>
Then I googled there's tool to decode the above encrypted text which gives me
?><?php
ini_set('display_errors','Off');
$con = mysql_connect("localhost","root","mma2229");
mysql_select_db("mitra", $con);
if (!$con)
{
die('Koneksi gagal !: ' . mysql_error());
}
?>
All I need is just to change the password (example to "123") but I do not know where to start.
How can I set the my connect.php with my new password ?
Just replace the previous delevopers code:
<?php $_F=__FILE__;$_X='Pz48P3BocA0KNG40X3M1dCgnZDRzcGwxeV81cnIycnMnLCdPZmYnKTsNCiRjMm4gPSBteXNxbF9jMm5uNWN0KCJsMmMxbGgyc3QiLCJyMjJ0IiwibW0xYWFhOSIpOw0KbXlzcWxfczVsNWN0X2RiKCJtNHRyMSIsICRjMm4pOw0KNGYgKCEkYzJuKQ0KICB7DQogIGQ0NSgnSzJuNWtzNCBnMWcxbCAhOiAnIC4gbXlzcWxfNXJyMnIoKSk7DQogIH0NCj8+';eval(base64_decode('JF9YPWJhc2U2NF9kZWNvZGUoJF9YKTskX1g9c3RydHIoJF9YLCcxMjM0NTZhb3VpZScsJ2FvdWllMTIzNDU2Jyk7JF9SPWVyZWdfcmVwbGFjZSgnX19GSUxFX18nLCInIi4kX0YuIiciLCRfWCk7ZXZhbCgkX1IpOyRfUj0wOyRfWD0wOw=='));?>
With:
<?php
ini_set('display_errors','Off');
$con = mysql_connect("localhost","root","123"); // put your new password instead of 123
mysql_select_db("mitra", $con);
if (!$con)
{
die('Koneksi gagal !: ' . mysql_error());
}
?>
In your phpMyAdmin panel write query like this:
SET PASSWORD FOR 'root'#'localhost' = password('mynewpass')
Setting new user / change password in view
If you want to do it with clicking, go to phpMyAdmin, click on the top Users -> Edit Privileges and there you can change a password. In this view you can also add new user which can be used in your PHP code.

php can not select database that created by user or phpmyadmin

I creat a database in cpanel and write small php code.
<?php
$dbhost='localhost';
$dbuser='-------';
$dbpass='*******';
$db = mysql_connect($dbhost,$dbuser,$dbpass);
mysql_set_charset('utf8',$db);
if(!$db){
echo 'can not connect to server';
}
else{
$database = '--------_ps';
$select = mysql_select_db($database);
if($select){
echo 'database selected. <br/>';
}
else{
echo 'database not select. <br/>';
}
mysql_close($db);
}
?>
I see database not select, When I change --------_ps database to phpmyadmin data base 'Database operations
information_schema' echo database selected.
I am not beginner on php but I don't know any way to solve this.
Please help me.
Try this code -witch also provides some basic feedback as to what errors your script may encounter:
<?php
...
$conn = #mysql_connect($dbhost, $dbuser, $dbpass) or die("Could not connect to the Database Server");
mysql_set_charset('utf8', $conn );
mysql_select_db($database, $conn) or die("Could not find the Database");
...
?>
Now, the above script works. If you still encounter problems, can you please give some feedback with the error log of your web server?
One more thing: Make sure you have created the database correctly with the phpMyAdmin, AND also have set the user with the credentials you are using...

Could not connect: Access denied for user 'indysoft_admin'#'localhost

ii have made a Mysql database and and user for the same with all permissions Still am getting the following error.
Could not connect: Access denied for user 'indysoft_admin'#'localhost
i am using the following connection.php script for connection
<?php
$con = mysql_connect('localhost', 'indysoft_admin','');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
else{
mysql_select_db("indysoft_person", $con);
}
?>
You didnt create the User with the same credentials, like your passing. Check your Database for ur username and password. Maybe a misspelling?

Check MySql credentials before creating tables

I am creating an install php script that creates some tables in a database and creates a config file. Everything works but I don't know how to check if their host, username and password are correct. I currently check if the database is there or not.. any help would be appreciated. This is on a WAMP server using PHP 5.4.12. The variables come from a basic form.
session_start();
$_SESSION['dbdatabase'] = $_POST['dbdatabase'];
$_SESSION['dbhost'] = $_POST['dbhost'];
$_SESSION['dbusername'] = $_POST['dbusername'];
$_SESSION['dbpassword'] = $_POST['dbpassword'];
$conn=mysqli_connect($_SESSION['dbhost'],$_SESSION['dbusername'],$_SESSION['dbpassword']);
if(!mysqli_select_db($conn, $_SESSION['dbdatabase']))
{
die ('Database does not exists. Please create a database before installing Pazzilla BackOffice.');
}
else
{
The function you are looking for is mysqli_connect_error().
<?php
$link = mysqli_connect('localhost', 'invalid_user', 'password');
if (!$link) {
die('Invalid database credentials. Mysql said: ' . mysqli_connect_error());
}
?>

mysql not connect with php scripts?

<?php
$cons=mysql_connect("localhost","root","");
mysql_select_db("infogallery") or die('Not Connected to the data base:'.mysql_error());
?>
I write above code for connection with mysql but when i run this scripts..nothing display on the brouser...what can i do for the connection with mysql....
If nothing is displayed, then it means it succeeded. Add more code which queries the database and displays some results.
Don't connect as the root account. Create an account specifically for playing around with.
Once you've done that, modify your code as follows:
$cons = mysql_connect('localhost', 'username', 'password');
if ($cons === FALSE) {
die("Failed to connect to MySQL: " . mysql_error());
}
mysql_select_db(etc.....);
You don't check if the connection failed, then try to do a database operation on that potentially failed connection. The or die(...) you have will only show the error caused by the select attempt, and the error message from the failed connection will be lost.
I like to just do
mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("infogallery") or die(mysql_error());
echo "So far, so good.";
How about something like the following:
<?php
try {
$cons = mysql_connect("localhost","username","password");
} catch ($e) {
die('Failed to connect to the database: ' . mysql_error());
}
try {
mysql_select_db("infogallery", $cons);
} catch ($e) {
die('Failed to select the database: ' . mysql_error());
}
?>

Categories