robembra Posted January 3, 2010 Share Posted January 3, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/187001-get-mysql-path-as-a-var/ Share on other sites More sharing options...
The Little Guy Posted January 3, 2010 Share Posted January 3, 2010 This works for me: <?php system("which mysql", $opt); echo $opt; ?> Quote Link to comment https://forums.phpfreaks.com/topic/187001-get-mysql-path-as-a-var/#findComment-987486 Share on other sites More sharing options...
robembra Posted January 3, 2010 Author Share Posted January 3, 2010 This works for me: <?php system("which mysql", $opt); echo $opt; ?> Thanks for the quick reply, but that outputs "1" Quote Link to comment https://forums.phpfreaks.com/topic/187001-get-mysql-path-as-a-var/#findComment-987487 Share on other sites More sharing options...
The Little Guy Posted January 3, 2010 Share Posted January 3, 2010 it outputs just one? cause mine did this: /usr/bin/mysql 0 Quote Link to comment https://forums.phpfreaks.com/topic/187001-get-mysql-path-as-a-var/#findComment-987490 Share on other sites More sharing options...
oni-kun Posted January 3, 2010 Share Posted January 3, 2010 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" 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. Quote Link to comment https://forums.phpfreaks.com/topic/187001-get-mysql-path-as-a-var/#findComment-987493 Share on other sites More sharing options...
robembra Posted January 3, 2010 Author Share Posted January 3, 2010 I trully wish it would output: /usr/bin/mysql 0 but it just shows: 1 Any ideas ???? Quote Link to comment https://forums.phpfreaks.com/topic/187001-get-mysql-path-as-a-var/#findComment-987496 Share on other sites More sharing options...
robembra Posted January 3, 2010 Author Share Posted January 3, 2010 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" 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 Quote Link to comment https://forums.phpfreaks.com/topic/187001-get-mysql-path-as-a-var/#findComment-987505 Share on other sites More sharing options...
The Little Guy Posted January 3, 2010 Share Posted January 3, 2010 did you try "where" and "whereis"? if you did, and get 1, then maybe your host is disabling "execution functions" due to security reasons, that is if you are using a hosting company... Quote Link to comment https://forums.phpfreaks.com/topic/187001-get-mysql-path-as-a-var/#findComment-987506 Share on other sites More sharing options...
robembra Posted January 3, 2010 Author Share Posted January 3, 2010 I have tried "which mysql" & "whereis mysql". I am using a hosting company, I have just tried and I can execute "mysqldump" with system(), so i dont think its disabled. Any more ideas??? Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/187001-get-mysql-path-as-a-var/#findComment-987510 Share on other sites More sharing options...
oni-kun Posted January 3, 2010 Share Posted January 3, 2010 $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. Quote Link to comment https://forums.phpfreaks.com/topic/187001-get-mysql-path-as-a-var/#findComment-987513 Share on other sites More sharing options...
robembra Posted January 3, 2010 Author Share Posted January 3, 2010 $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. Quote Link to comment https://forums.phpfreaks.com/topic/187001-get-mysql-path-as-a-var/#findComment-987522 Share on other sites More sharing options...
The Little Guy Posted January 3, 2010 Share Posted January 3, 2010 try this: $sql = mysql_query('SELECT @@datadir as dir'); $row = mysql_fetch_array($sql); echo $row['dir']; Quote Link to comment https://forums.phpfreaks.com/topic/187001-get-mysql-path-as-a-var/#findComment-987529 Share on other sites More sharing options...
robembra Posted January 3, 2010 Author Share Posted January 3, 2010 try this: $sql = mysql_query('SELECT @@datadir as dir'); $row = mysql_fetch_array($sql); echo $row['dir']; This returns: /var/db/mysql/ Will this work for mysql and mysqldump? Quote Link to comment https://forums.phpfreaks.com/topic/187001-get-mysql-path-as-a-var/#findComment-987531 Share on other sites More sharing options...
The Little Guy Posted January 3, 2010 Share Posted January 3, 2010 $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... Quote Link to comment https://forums.phpfreaks.com/topic/187001-get-mysql-path-as-a-var/#findComment-987532 Share on other sites More sharing options...
robembra Posted January 3, 2010 Author Share Posted January 3, 2010 $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 Quote Link to comment https://forums.phpfreaks.com/topic/187001-get-mysql-path-as-a-var/#findComment-987537 Share on other sites More sharing options...
robembra Posted January 3, 2010 Author Share Posted January 3, 2010 Still having problems with getting the full path to mysql as a $var does anyone else have any ideas how I can do this???? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/187001-get-mysql-path-as-a-var/#findComment-987732 Share on other sites More sharing options...
robembra Posted January 4, 2010 Author Share Posted January 4, 2010 *bump* Quote Link to comment https://forums.phpfreaks.com/topic/187001-get-mysql-path-as-a-var/#findComment-987948 Share on other sites More sharing options...
oni-kun Posted January 4, 2010 Share Posted January 4, 2010 *bump* Why not run: cd /usr/local MySQL status And find out yourself if it's there or not. I'm sure the basedir is the basedir as mentioned by The Little Guy. Quote Link to comment https://forums.phpfreaks.com/topic/187001-get-mysql-path-as-a-var/#findComment-987956 Share on other sites More sharing options...
trq Posted January 4, 2010 Share Posted January 4, 2010 Can I ask what you need the path to the mysql client for? Quote Link to comment https://forums.phpfreaks.com/topic/187001-get-mysql-path-as-a-var/#findComment-987958 Share on other sites More sharing options...
robembra Posted January 4, 2010 Author Share Posted January 4, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/187001-get-mysql-path-as-a-var/#findComment-987961 Share on other sites More sharing options...
trq Posted January 4, 2010 Share Posted January 4, 2010 So would all installs of php & mysql be in "*/bin/"??? Yes. Quote Link to comment https://forums.phpfreaks.com/topic/187001-get-mysql-path-as-a-var/#findComment-987962 Share on other sites More sharing options...
robembra Posted January 4, 2010 Author Share Posted January 4, 2010 So would all installs of php & mysql be in "*/bin/"??? Yes. Would this apply to all installs, i didnt just mean my host. Sorry I wasn't clear. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/187001-get-mysql-path-as-a-var/#findComment-987965 Share on other sites More sharing options...
trq Posted January 4, 2010 Share Posted January 4, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/187001-get-mysql-path-as-a-var/#findComment-987970 Share on other sites More sharing options...
gizmola Posted January 4, 2010 Share Posted January 4, 2010 $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. Quote Link to comment https://forums.phpfreaks.com/topic/187001-get-mysql-path-as-a-var/#findComment-987973 Share on other sites More sharing options...
robembra Posted January 4, 2010 Author Share Posted January 4, 2010 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 Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/187001-get-mysql-path-as-a-var/#findComment-987977 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.