-1

I've been embedding some php in my shell scripts. Stuff like this works:

SUBJECT_URL=php -r "echo rawurlencode('$SUBJECT');"

and sets my string variable SUBJECT_URL to the converted value of $SUBJECT

However,

I am trying to use this command:

link=php -r "mysql_connect('localhost', 'root', 'mypassword');"

To test my connection to the SQL database. Unfortunately, the value for "link" is not getting set. Therefore I can't test it for success and can't issue the:

php -r "mysql_close($link);"

to release the resource.

Any help would be appreciated.

jim
  • 1
  • 1

2 Answers2

0

This is because mysql connection dies with php. Now if you really cannot just connect to mysql without using php from where you type, you could use something like :

php -r "echo @mysql_connect('localhost', 'root', 'mypassword')? 'connected' : 'not connected';"

You don't need to release the connection as php will do it for you when it exits.

fab2s
  • 838
  • 2
  • 8
  • 14
  • Thanks, I understand why I need the "echo" command in my top example, but don't understand why or how it works in (fab2s)'s example . Sorry that my shell programming is rusty, but what is the ? 'connected" : 'not connected" part doing? Looks like a conditional output? – jim Feb 27 '17 at 22:21
  • that's all you can do this way, find out if a connection can be established, but not manipulate it as it dies with php wich dies right after the ';'. Why not just writing your script in php if you need php to connect ? – fab2s Feb 28 '17 at 16:46
0

Using php -r is almost certainly the wrong approach; if you need to use PHP you'd be better off writing the whole script in PHP instead of a mix of Shell as well.

The reasons link=php -r "mysql_connect('localhost', 'root', 'mypassword');" doesn't work is that (a) mysql_connect returns a resource to the database connection, which is not something that can be coerced into a string, and (b) your one-line PHP script (mysql_connect('localhost', 'root', 'mypassword'); doesn't echo that resource anyway, i.e. it doesn't even attempt to coerce the resource into a string.

The mysql_connect function returns false on failure, so if you want your link variable to indicate success or failure you can assign a string to it accordingly. In a one-liner the ternary operator is a simple way to do that, as fab2s noted in his answer (echo @mysql_connect('localhost', 'root', 'mypassword')? 'connected' : 'not connected'). In that code the @ (silence operator) prevents a failed connection from raising a Warning from PHP, and the use of the ternary operator conditionally returns "connected" if the call returned something "truthy" and "not connected" if it returned false.

As an aside, note that you really shouldn't use the old MySQL extension any longer. In fact, it's generally best to use PDO:

<?php
$dbConn = new \PDO(
    'mysql:dbname=my_db;host=localhost;charset=utf8',
    'user',
    'password',
    [PDO::ATTR_ERRMODE => PDO::ERR_EXCEPTION]
);
Community
  • 1
  • 1
Richard Turner
  • 12,506
  • 6
  • 36
  • 37
  • On PHP 5.3 you can't use short array syntax, so that last parameter for the PDO constructor would be `array (PDO::ATTR_ERRMODE => PDO::ERR_EXCEPTION)`. However, I strongly recommend you upgrade to a more recent version of PHP; 5.3 is slow and insecure. – Richard Turner Feb 27 '17 at 23:30