LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Execute php files with database connectivity using terminal (https://www.linuxquestions.org/questions/linux-newbie-8/execute-php-files-with-database-connectivity-using-terminal-4175467706/)

unclesamcrazy 06-28-2013 07:26 AM

Execute php files with database connectivity using terminal
 
Hello Everyone,
I am using xampp for php project development.
I have a php script which I have to run on terminal. The file needs database connectivity. I have made the connection successfully. I am saying 'successfully' because when I run the script in browser, it runs successfully.
But when I run the script in terminal using,
Code:

# php /path/of/the/script.php
I am getting mysql_connect error
Code:

PHP Warning:  mysql_connect(): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) in /opt/lampp/htdocs/project_dir/php-script.php on line 9
It is trying to connect using default mysql's database but default mysql is stopped. XAMPP's mysql is started.
I think if it will connect from XAMPP's mysql database, my script will be run successfully.
I don't want to run following command on terminal.
Code:

# wget http://localhost/project_dir/php-script.php
Actually it is working fine but I want to run it using
Code:

# php script-name
Please help me to connect to the XAMPP's mysql instead of default mysql.

tronayne 06-28-2013 10:37 AM

Take a look at http://www.cyberciti.biz/faq/how-to-...-line-and-php/ which shows you how to connect PHP to a MySQL data base via command line.

The code looks like:
Code:

<?php
  $link = mysql_connect("localhost", "USERNAME", "PASSWORD");
  mysql_select_db("DATABASE");
  $query = "SELECT * FROM TABLE";
  $result = mysql_query($query);
  while ($line = mysql_fetch_array($result))
  {
      foreach ($line as $value)
      {
        print "$value\n";
      }
  }
    mysql_close($link);
?>

but you ought to read the entire article.

Hope this helps some.

eklavya 07-01-2013 04:32 AM

I think the solution is in your error.
Quote:

Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'
mysql socket file is created when mysql is started but it is very unstable like Francium. It is disappeared once you stop the mysql
As you said, you are using LAMPP, You will find your mysql socket file in /opt/lampp/var/mysql/mysql.sock

The mysql socket file is created in /var/run/mysqld, when default mysql is started. It will not be created here if you start the xampp's mysql.
so what can you do to create here. Start the default mysql but your database is in xampp's mysql so this method will not work.If you will try to copy the sock file from /opt/lampp/var/mysql to /var/run/mysqld, but it will not give you permission to copy because socket files can't be copied as I told you it is francium, then?

Make the link of socket file and put it in /var/run/mysqld as mysqld.sock. Now the error should be solved because it gets the socket file.
Try this, if you get any other error, share.

unclesamcrazy 07-02-2013 12:36 AM

Ya Eklavya Thanks for your solution. It worked.
I have tried various combinations and checked practical behaviour of mysql with web server.
It helped me a lot to understand the concepts behind these functionalities.

Now I can do following things because of my practicals.
1) I can connect apache2 web projects with lampp's mysql, I mean database will be in lampp's mysql but web server will be apache2.
2) I can connect vice versa too. It means I can connect lampp's web projects with apache2's mysql.
3) I can open lampp's mysql prompt runing $ mysql commang in terminal as well as
4) I can open apache2's mysql prompt runing same command.
5) I have understood that how to operate all installed mysqls when all are sharing same port 3306.

I have understood about sock file concept. It is very important file because of this file I can do all above tasks. I do not need to change any path or file in /usr/bin or /opt/lampp/bin, I just have to create sock file in correct directory accrding to requirement.
I just make a link of file and put it in desired directory. Now I can access my desired mysql database from everywhere (either it is browser or it is terminal(using shell scripts too))

Thanks

Habitual 07-02-2013 03:04 PM

PHP Code:

<?php
$link 
mysql_connect('server.com''mysql_user''mysql_password');
if (!
$link) {
    die(
'Could not connect: ' mysql_error());
}
echo 
'Connected successfully';
mysql_close($link);
?>


Code:

php -f test.php
Connected successfully

doh. that's what I get for not reading entirety of the replies.
Oh well, that ends well, I reckon. ;)

tronayne 07-02-2013 04:27 PM

It's pretty "normal" that MySQL is started at boot and continues running; there isn't much of a need to start and stop it all the time -- it doesn't eat resources, it doesn't do much of anything just sitting there.

A typical LAMP (Linux-Apache-MySQL-PHP) installation does not start and stop, it more or less runs 24/7 (doesn't do a heck of a lot of good if MySQL isn't running to try and access it with PHP, does it now?).

unclesamcrazy 07-03-2013 02:53 AM

Quote:

Originally Posted by Habitual (Post 4982804)
[PHP]
Connected successfully

Ya but it is connected when the default mysql is started and database is deployed on apache2's mysql.
It was not working before when apache2's mysql (Default mysql) was stopped and LAMPP's mysql was started. Then it was not possible to connect xampp's db using terminal.

Quote:

Originally Posted by tronayne (Post 4982847)
It's pretty "normal" that MySQL is started at boot and continues running; there isn't much of a need to start and stop it all the time --

But if I will not start & stop it all the time, how will I co ordinate between the two mysqls. Both are sharing same port that is 3306.
Some time I want to connect apache2 from XAMPP's mysql and some times XAMPP's htdocs projects with apache2's mysql (Default mysql).

If you have any easy way, please share.


All times are GMT -5. The time now is 07:29 AM.