Jump to content

Get MySQL path as a $var


robembra

Recommended Posts

Hi,

 

Basically I want to get the mysql path into a $var. I am having problems getting the path for mysql as a $var. I have tried the following ways:

$output = shell_exec('which mysql');
echo "<pre>$output</pre>";

$output = shell_exec('whereis mysql');
echo "<pre>$output</pre>";

 

'which mysql' = blank $var.

 

'whereis mysql' = a path but its to a gzip file, I cant seem to get the first part like you see if you ran in ssh.

 

Please any help would be greatful.

 

Thanks

Link to comment
Share on other sites

Yes, the nature of the function is to return output into a variable if specified. All $var = shell_exec... will do is return true or false if it returned data or came up with an error.

This works for me:

 

<?php
system("which mysql", $opt);
echo $opt;
?>

 

Thanks for the quick reply, but that outputs "1"  :confused:

 

Find the output status to find error?

$command = 'which mysql';
$shellOutput = shell_exec($command.' > /dev/null; echo $?');
echo trim($shellOutput); 

 

Also I'd recommend using a PATH to the exeutable file (/usr/sbin/...) instead of just naming it.

Link to comment
Share on other sites

Yes, the nature of the function is to return output into a variable if specified. All $var = shell_exec... will do is return true or false if it returned data or came up with an error.

This works for me:

 

<?php
system("which mysql", $opt);
echo $opt;
?>

 

Thanks for the quick reply, but that outputs "1"  :confused:

 

Find the output status to find error?

$command = 'which mysql';
$shellOutput = shell_exec($command.' > /dev/null; echo $?');
echo trim($shellOutput); 

 

Also I'd recommend using a PATH to the exeutable file (/usr/sbin/...) instead of just naming it.

 

This did not work for me either and the output was the same: 1

 

:(

Link to comment
Share on other sites

$output = null;
exec('whereis mysql', $output);
echo "<pre>" . var_export($output, TRUE) . "</pre>\\n";

 

How does this work out, does it still return 1? Also, if safemode is enabled on your PHP installation, you cannot use this function.

Link to comment
Share on other sites

$output = null;
exec('whereis mysql', $output);
echo "<pre>" . var_export($output, TRUE) . "</pre>\\n";

 

How does this work out, does it still return 1? Also, if safemode is enabled on your PHP installation, you cannot use this function.

 

This outputted:

 

array (

  0 => 'mysql: /usr/local/man/man1/mysql.1.gz',

)

\n

 

Thats as close I have got myself, can't seem to get the bit before the above that reads:

 

/usr/local/bin/

 

I have looked at my settings and safe mode is off.

Link to comment
Share on other sites

$sql = mysql_query('SELECT @@basedir as dir');
$row = mysql_fetch_array($sql);
echo $row['dir'];

 

I found a small update to the query... but as for your question I don't know...

 

This outputted:

 

/usr/local/

 

At least I'm a step closer I just need "bin" on the end  :-[ 

 

Would mysql always be installed */bin/mysql is that why its output is /usr/local/ or am I not getting the correct output???

 

Thanks again

Link to comment
Share on other sites

I made i backup script a while back and now that I have shared it with serveral people it wont work on different hosts (mysqldump). The main one 2 things I need are:

 

php path

mysql path

 

I think i have done this by using values from phpinfo, but it goes as far as "/usr" for php & "/usr/local" for mysql.

 

Its "/usr/bin/php" & "/usr/local/bin/mysql" on my hosting. So would all installs of php & mysql be in "*/bin/"???

 

If so I will do it this way. I just thought it would be a built in function to get path to php and mysql.

 

 

Thanks again

Link to comment
Share on other sites

Yes it would. Sorry, I knew what you meant.

 

There is of course no absolute guarantee of this, but it is extremely common practice and unless some weird distro is moving stuff around for no good reason, I would say its quite reliable. Ive not ran into any binaries outside of */bin directories in years of using Linux.

Link to comment
Share on other sites

$sql = mysql_query('SELECT @@basedir as dir');
$row = mysql_fetch_array($sql);
echo $row['dir'];

 

I found a small update to the query... but as for your question I don't know...

 

This outputted:

 

/usr/local/

 

At least I'm a step closer I just need "bin" on the end  :-[ 

 

Would mysql always be installed */bin/mysql is that why its output is /usr/local/ or am I not getting the correct output???

 

Thanks again

 

Yes mysql's startup is kinda hinkey stuff -- it's just scripts, but they do append a /bin onto the end of basedir.  It's kind of silly that there isn't a system variable in mysql that shows you the path to the binary, but ... oh well. 

 

Of course it's not really clear why you need to know this.  For example, there could easily be a php install that has no mysql binaries available, and only the client librariy, because the webserver is connecting to a mysql server on a  different host.

 

*edit* ok I saw you posted about this while I was writing this.  Not sure how your backup script approaches things -- if it just calls mysqldump, then there's nothing you can do if the mysqldump isn't there, nor is there much you can do if the process running apache doesn't have permission to get to it or run it.

Link to comment
Share on other sites

Well thanks for the help thats great.

 

I can always add user input for path if $vars I got from the phpinfo() are wrong.

 

You would think there would be a function or defined $var for those things. These is for everything else  :D

 

Thanks again!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.