I was wondering if anyone would be able to tell me about whether it is possible to use PHP to check if a postgresql database exists?
I am writing a PHP script and I only want it to create the database if it doesn't already exist but up to now haven't been able to see how to implement it.
Thank you
I've tried
$cmd = 'psql -U postgres -c "SELECT schema_name FROM information_schema.schemata WHERE schema_name = 'portal';"';
exec($cmd);
I got
PHP Parse error: syntax error, unexpected 'portal'
You have a syntax error singe the single quote is used to initalize the statement.
$cmd = 'psql -U postgres -c "SELECT schema_name FROM information_schema.schemata WHERE schema_name = \'portal\';"';
exec($cmd);
You should use PDO instead of exec, but this would probably work for you.
You are confusing schema and database. in postgres it is schema what you call database in mysql. so if you want to find a database, better use pg_databases
$cmd = 'psql -U postgres -c "select \'already_there\' from pg_database where datname = \'db1\';"';
exec($cmd);
It's a PHP error, you have to escape '\'portal\''
Related
Referencing this post as well(How to create database using php shell_exec and sql command line)
I've followed the accepted answer in that post, but I am curious as to why the shell_exec is unable to run mysql code. It merely returns NULL.
The user already has sudo access, when i copy the following command line into the console log manually it works.
$cmd = escapeshellcmd('sudo mysql -u root -e "create database somedb"');
$test = shell_exec($cmd);
var_dump($test);
Edit 1:
Updated [username], if root may cause some issues
You will need to use the full path to mysql
Type whereis mysql to find it.
It will most likely be /usr/bin/mysql
Change your command to:
$cmd = escapeshellcmd('sudo /usr/bin/mysql -u [username] -e "create database somedb"');
You may need full path to sudo .. same principles will apply.
I have a PHP script that is called from PayPal IPN. When a user purchases something form my site, the IPN PHP script creates the user in the mysql database. At a later time, 24hrs, that user needs to be deleted from the database. I assume that using the "at" command would work. I believe the problem I am running into is regarding the parentheses in my command.
<?php
$email = "buyer#gmail.com";
$cmd = 'mysql -u username -pPassword -e `DELETE FROM db.users WHERE users.email = "$email";`|at tomorrow';
exec($cmd);
?>
If I run this page as-is, it throws the following error:
warning: commands will be executed using /bin/sh
sh: 1: DELETE: not found
mysql: option '-e' requires an argument
job 38 at Sun Feb 8 04:59:00 2015
That is what is leading me to believe there is a problem with my quotes and parentheses.
You should explain the problem you're running into. The first thing I note is that you use single quotes - those don't interpret the inner string so $email doesn't get replaced for the e-mail value.
Second - you're using backticks. The command gets executed in a bash shell, where the backticks around your query get interpreted as a shell command. Try this:
$cmd = "echo mysql -u username -pPassword -e \\'DELETE FROM db.users WHERE users.email = \\\"$email\\\"\\' |at tomorrow";
try this :
"mysql -u username -pPassword -e `DELETE FROM db.users WHERE users.email = '$email'`|at tomorrow";
ssh root#ip "mysql -uroot -p -e 'grant all on wordpress.* to firaswp#'ip' identified by 'password123';'"
however, when I input grant all on wordpress.* to firaswp#'54.87.224.199' identified by 'password123'; in mysql daemon it works fine..
does anyone know why this wouldn't be working? the syntax should be fine - if i remove "identified by 'password123', it works. Thanks!
I am using this, and it works for me:
ssh root#ip "mysql --user=user --password=pass database -e 'query'"
Your script also look working but don't use ; in command.
i am trying to create a database using shell_exec and mysql commands. I do not want to use php built-in mysql_query because of severals reasons. However, i cant get the below command to execute successful. Anyone can show me some light on what went wrong?
$test = shell_exec("mysql -u root -pmypassword create database db_hello;");
var_dump($test);
The correct syntax is like this:
mysql -u [username] -p -e "create database somedb"
Or
mysql --user=user_name --password=your_password -e "SELECT 1 FROM information_schema.tables"
Reference: http://www.electrictoolbox.com/run-single-mysql-query-command-line/
$cmd = escapeshellcmd('mysql -u [username] -p -e "create database somedb"');
$test = shell_exec($cmd);
var_dump($test);
The issue might be the path to mysql in your php script.
For me was : /usr/local/bin/mysqld (osx 10)
I've got a PHP command line program running. And I want to connect to a mysql shell straight from PHP. I've done this before in Python using os.execvp But I can't get the same thing to work in PHP.
I've tried the following functions:
system
passthru
exec
shell_exec
example:
system('mysql -u root -pxxxx db_name');
But they all seem to wait for mysql to exit and return something. What I really want is for PHP to launch the mysql shell and then exit it self. any ideas?
If you want shell commands to be interactive, use:
system("mysql -uroot -p db_name > `tty`");
That will work for most cases, but will break if you aren't in a terminal.
Give MySQL a script to run that's separate from the PHP script:
system('mysql -u root -pxxxx db_name < script.mysql');
In addition to what acrosman said, you can also use the -e switch to pass SQL from the command line.
$sql = ".....";
system("mysql -u root -pxxxx db_name -e \"$sql\"");
Also, I hope your not actually connecting to the database as root from a PHP application.