Link to home
Start Free TrialLog in
Avatar of Sohel Rana
Sohel RanaFlag for Australia

asked on

Command for running .php file in ssh

Hi:

I have a script .php file which will update the mysql table. Can I run this file in ssh? I tried by run sql_update.php but it say command not found.

Any other command ?

rsdn
ASKER CERTIFIED SOLUTION
Avatar of sunnycoder
sunnycoder
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Sohel Rana

ASKER

Hi sunnycoder:

Thanks for your reply. Now the script is executing but it shows error. Although you answer my question but if you can solve it I will increse the points to 500.

Here is the code inside script sql_update.php:

<html>

<head>

</head>
<b>Tableinstaller SQL</b><p>

This program will help you getting exact the same databaselayout that is correct.
This is accomplished by connecting to our server and compare your database with yours. If there is any errors (difference,
either in column size, fieldtype or that the table or field do not exist, this program will try to fix it. It capable of constructing
in a hole databasesite, with primary keys and everything!<p>

Timeout is set to 500 seconds. If you have a special slow connection or slow computer, you can increase the timelimit
inside this script. Or you can download the database structure to your machine to get it correct.

<?

// Set your timelimit below. 500 sek is default
set_time_limit(0);


function update()
{
include "db.php";

$t = 0;

// Where to get the file. If problems, use file('database.txt'); after you have downloaded
// http://domain.com/classifieds/backend/database.txt and put it in the same
// directory as this script.

$content_txt = file('database.txt');


$l = count($content_txt);
$count = 0;

include "config/general.inc.php";


while ($count < $l)
{
      $content_txt[$count] = ereg_replace("category:","$cat_tbl:", $content_txt[$count]);
      $content_txt[$count] = ereg_replace("ad:","$ads_tbl:", $content_txt[$count]);
      $content_txt[$count] = ereg_replace("user:","$usr_tbl:", $content_txt[$count]);
      $content_txt[$count] = ereg_replace("picture:","$pic_tbl:", $content_txt[$count]);
      $content_txt[$count] = ereg_replace("payment:","$payment_tbl:", $content_txt[$count]);
      $content_txt[$count] = ereg_replace("banners:","$banner_tbl:", $content_txt[$count]);
      $content_txt[$count] = ereg_replace("advertisers:","$adv_tbl:", $content_txt[$count]);
      $content_txt[$count] = ereg_replace("favourites:","$fav_tbl:", $content_txt[$count]);
      $content_txt[$count] = ereg_replace("template:","$template_tbl:", $content_txt[$count]);

      //print "Now checking if line $count $content_txt[$count] is present in database...<br>";

      // Loop through "database"

      $result = mysql_list_tables($datab);

      while ($row_table = mysql_fetch_row($result))
      {


            $sql = "SHOW INDEX FROM $row_table[0] FROM $datab";
            //print $sql;
            $result_index = mysql_query($sql);
            $row_index = mysql_fetch_array($result_index);
            $key = $row_index['Column_name'];
            //print "<hr>$sql Indeks er $key<hr>";



            $result3 = mysql_query("SHOW FIELDS FROM $row_table[0] from $datab");
            $i = 0;

            while ($row = mysql_fetch_array($result3))
            {
                   $field = $row['Field'];
                  $type = $row['Type'];


                  if ($field <> $key)
                  {

                        $key_w = "none";
                  }
                  else
                  {
                        $key_w = $key;
                  }


                  $line = $row_table[0] . ":" . $field . ":" . $type . ":" . $key_w . "\r\n";

                  if ($content_txt[$count] == $line)
                  {
                        //print "We have match <br>$content_txt[$count] === $line<p>";
                        $m = 1;

                  }


            }




      }



      // No match!!
      if (!$m)
      {
            list($tablename,$fieldname,$type,$key) = split(":", $content_txt[$count]);



            $key = trim($key);


            $debug = 0;


            // First try to create table
            $sql = "CREATE TABLE `$tablename` (`del` INT);";
            $create = mysql_query($sql);

            if ($create)
            {
                  if ($debug)
                  {
                        print "Table created with $sql<br>";
                  }
            }
            else
            {
                  // Then try to create columns with keys
                  $string = "ALTER TABLE `$tablename` ADD `$fieldname` $type";

                  if ($key <> 'none')
                  {
                        $string = $string . " NOT NULL FIRST;";

                  }

                  $alter_add_field = mysql_query($string);
                  //$alter_add_field2 = mysql_query($string_2);

                  if ($alter_add_field)
                  {
                        if ($debug)
                        {
                              print "Column added with $string<br>";
                        }

                  }
                  else
                  {
                        // Then try just to modify field length and add primary keys


                        $string = "ALTER TABLE `$tablename` ADD PRIMARY KEY(`$key`)";
                        $primary_mod = mysql_query($string);

                        $string_d = "ALTER TABLE `$tablename` CHANGE `$fieldname` `$fieldname` $type DEFAULT '0' NOT NULL AUTO_INCREMENT";
                        $primary_mod_d = mysql_query($string_d);


                        if ($primary_mod)
                        {
                              if ($debug)
                              {
                                    print "Primary key added with $string AND modified with $string_d<br>";
                              }
                        }
                        else
                        {
                              $sql = "ALTER TABLE `$tablename` CHANGE `$fieldname` `$fieldname` $type";
                              $mod = mysql_query($sql);

                              if ($mod)
                              {
                                if ($debug)
                                {
                                    print "Field lengt/type done with with $sql<br>";
                                }
                              }
                        }

                  }

            }





      }

      $m = 0;

      print "<p><p>";
      // next field
      $count++;

}


if ($error)
{
      print "This program needs to be run again to do more changes. Just hit the REFRESH button in your browser.";
      $error = 0;
}
mysql_free_result($result);
}

while ($teller < 4)
{
      $teller++;
      if ($debug)
      {
      print "<hr>Run $teller:<p>";
      }
      update();
      if ($debug)
      {
      print "</hr>";
      }


}

?>

---------
rsdn
oh sorry forget to tell the error :

It says :

/home/sara/public_html/sara/admin/sql_update.php: line 1: syntax error near unexpected token 'newline'

rsdn
I am not too familiar with php ... but here is a guess
start your script with
#!/usr/bin/php -q
<?php

php TA might be a better place to ask this