LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-28-2013, 08:26 AM   #1
unclesamcrazy
Member
 
Registered: May 2013
Posts: 189

Rep: Reputation: 1
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.
 
Old 06-28-2013, 11:37 AM   #2
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060
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.
 
1 members found this post helpful.
Old 07-01-2013, 05:32 AM   #3
eklavya
Member
 
Registered: Mar 2013
Posts: 622

Rep: Reputation: 136Reputation: 136
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.
 
Old 07-02-2013, 01:36 AM   #4
unclesamcrazy
Member
 
Registered: May 2013
Posts: 189

Original Poster
Rep: Reputation: 1
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
 
Old 07-02-2013, 04:04 PM   #5
Habitual
LQ 5k Club
 
Registered: Jan 2011
Location: Nowhere near you, thank God.
Distribution: OSX Sierra
Posts: 8,573
Blog Entries: 14

Rep: Reputation: Disabled
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.

Last edited by Habitual; 07-02-2013 at 04:05 PM.
 
Old 07-02-2013, 05:27 PM   #6
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060
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?).
 
Old 07-03-2013, 03:53 AM   #7
unclesamcrazy
Member
 
Registered: May 2013
Posts: 189

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by Habitual View Post
[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 View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
mysql with php for upload download files to database sandeepc04 Linux - Newbie 6 05-22-2013 10:39 AM
Database Connectivity MughalShahzad Programming 1 07-03-2010 02:03 AM
Apache doesn't execute PHP files? dbc001 Linux - Software 2 01-28-2006 03:29 PM
database connectivity through C program junaid18183 Programming 1 06-11-2005 06:13 PM
Execute terminal commands from .desktop files tuculover Linux - Newbie 6 10-22-2004 06:49 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:43 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration