Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Regular Coder
    Join Date
    Nov 2010
    Location
    Washington DC
    Posts
    338
    Thanks
    22
    Thanked 1 Time in 1 Post

    run php script multiple times with different variables

    Hello:

    I need some assistance with the following script configuration. The goal is to run a cronjob with my initial script which would run a mysql query and pull some account ids, it then would create an array of those values and through a for() loop execute a second script with each individual value (account id ) passed on to the secondary script. The secondary script needs to perform specific tasks which are totally dependent upon the account id. Below is what I have.

    first Script

    PHP Code:
    <?php
    //*
     
    $link mysql_connect('****''****''*****');
        if(!
    $link) {
            die(
    'Failed to connect to the server: ' mysql_error());
        }
        
        
    //Select database
        
    $db mysql_select_db('*****');
        if(!
    $db) {
            die(
    "Sorry I am Unable to select database");
        }

    $get_acctids="select accountid from company_info"
    $results mysql_query($get_acctids) or die(mysql_error());

    $new_array[] = $row;
    while( 
    $row mysql_fetch_assoc$results)){
    $acct_data[] = $row['accountid'];
    //print_r($acct_data);

    }

    $key $acct_data;
    //echo "<br /><br />";
    //print_r($key);

    foreach($key as $value){
        
    $dr[] = "accountid = '$value' "// Build array of strings
    }
    /* for testing purpose, uncomment
    echo "<br /><br />";
    print "<pre>";
    print_r($dr);
    print "</pre>";
    echo "<br /><br />";
    */

    function include_get_params($file) {
      
    $parts explode('?'$file);
      if (isset(
    $parts[1])) {
        
    parse_str($parts[1], $output);
        foreach (
    $output as $key => $value) {
          
    $_GET[$key] = $value;
        }
      }
      include(
    $parts[0]);
    }

    $sql "select * from company_info WHERE ";
    $sql .= implode(' OR '$dr); // convert to string joined by ' OR ' and add to end of $sql
    // Instead of using a loop and running a query for 'Cable Guy'
    // and another query for 'John Doe', the following query fetches both rows.
    $result mysql_query($sql);
     
    $numrows=mysql_num_rows($result);
    //echo $numrows;
    while($row mysql_fetch_array($result))
      {
          echo 
    "<br /><br />";
          
          
    //echo $row['co_name']."--".$row['accountid']. "--".$row['co_website']."<br />";
             // echo $row[0]."--".$row[5]. "--".$row['co_website']."<br />";
        
          
          
    for ($i=0$i<1$i++) {
      if (isset(
    $row['accountid']) ) {
    //including the secondary script
       
    include_get_params('purge_queueList_daily.php?accountid='.$row['accountid']);
     } else {
       break;
     }
    }
               
     
    //mysql_close($db);

    }


    ?>
    Now, secondary script function would be to use each account id and query a corresponding database with the id as the required variable and thus purge a specified table within the script.

    Secondary script:

    PHP Code:

    <?php
    $club_id 
    =$_GET['accountid'];// This is from the first script and will be required in the included file below (datalogin.php
    include 'datalogin.php';
    $link mysql_connect(DB_HOSTDB_USERDB_PASSWORD);
        if(!
    $link) {
            die(
    'Failed to connect to server: ' mysql_error());
        }
        
    //Select database
        
    $db mysql_select_db(DB_DATABASE);
        if(!
    $db) {
            die(
    "Unable to select database");
        }
        echo 
    $db." <br />";
        
    //echo $db;
    $todayis=date("Y-m-d");//date and time
    $qry "DELETE FROM queue_sys where todays_date NOT LIKE '%$todayis%'";// remove players from the list from previous days
    $result=mysql_query($qry);        

    if(
    $result)
    {
    $message"<br /><br /><p><font size=5 color=blue>Queue table Has been Purged and Updated!</font>";
    }
    else
    {
                    
    $message="Purge failed!";        
             }
       
    // echo $message;
    ?>
    I'm afraid my logic may be wrong. I'm unable to complete the task of purging the specified table in the secondary script. I would really appreciate some assistance in getting this resolved!

    Best,
    Mossa
    Last edited by mbarandao; 05-10-2013 at 07:18 PM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Are these scripts entirely internal on the same server? If so, does secondary script have any standalone purpose, or is it used entirely for the benefit of the first script?

    Also, I'm not sure I understand the need for the secondary script to be chained to the first. The only dynamic usage of the data in there is that of $todayis. So $_GET['accountid'] isn't actually used anywhere in here. Is it used for the datalogin.php file?

    Edit:
    nvm, I see it in the comments now.
    What's the purpose of the datalogin.php file?
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    Regular Coder
    Join Date
    Nov 2010
    Location
    Washington DC
    Posts
    338
    Thanks
    22
    Thanked 1 Time in 1 Post
    Fou-Lu, Thanks for the reply!

    All files are on the same server. The secondary script purpose is to perform the purge function but dependent upon the accountid.

    Datalogin gets the accountid from any file that connects to it and assign the appropriate db information based on the provided account id.

    The content of the datalogin.php is something like this....

    PHP Code:
    if($accountid_var =="425786" || $club_id=="425786")//SMARTSPIN -- $club_id variable is passed from weblink folder for website integration
    {

                                                                        
    /*------------------*/
                                                                    
                                                                    //CHANGE THE VARIABLES HERE FOR PASSWORD AND DATABASE USER NAME
                                                                    
    $account_pass ="****";
                                                                    
    $account_user ="****";
                                                                    
    $account_mster_db ="****";
                                                                    
    $account_db ="****";
                                                                    
    $club_accountid_raw =""//club account id needs to be hard coded for auto membership reminder
                                                                    
    $cal_user_name "****";
                                                                    
    $countries_db ="****";
                                                                    
    $countries_db_user $cal_user_name;
                                                                    
    //FAQ
                                                                    
    $admin_username ="****";
                                                                    
    $admin_password ="*****";
                                                                    
                                                                    
    /*------------------*/
    define("DB_HOST""localhost");
    define("DB_USER"$account_user);
    define("DB_PASSWORD"$account_pass);
    define("DB_DATABASE"$account_db);
    }

    elseif
    {
    //different database info

    Last edited by mbarandao; 05-10-2013 at 08:31 PM.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    What exactly is it that changes with the datalogin.php? Do you actually login to a different database foreach of the accountid specified? Unless the database itself changes, there really isn't a need for executing this multiple times as the table itself will sweep everything with the query you currently have.

    Nonetheless, the functionality itself makes more sense grouped into an actual function. In order to call and actually populate a new $_GET value (although you can of course fudge it), you'd have to use a socket or curl connection back to your own server, but if you create a function of it you can then issue a call to a function from either the standalone usage of the purge script, or from a scheduled task version of the purge.


    So really the only question is, does the database itself actually change (connection wise I mean)? If the answer is no, than you can do all of this purging using a single query for the SQL.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #5
    Regular Coder
    Join Date
    Nov 2010
    Location
    Washington DC
    Posts
    338
    Thanks
    22
    Thanked 1 Time in 1 Post
    I'm afraid my intended purpose is not being explained correctly.

    The account id is what changes in the datalogin.php. This variable is passed to the file with every access to it. Using this accountid variable, datalogin.php uses if and else condition to determine which database information to send back.

    Now, the first scripts performs a query to first determine the number of accounts with databases there exist, it then gets all account ids and makes it available.

    For better explanation, let me annotated the files structure a bit. We're dealing with three files (or scripts)

    file 1: determines total accounts with databases and gets all accountids and arrange them into an array

    file 2: gets each account ids (in a for() loop) and contacts datalogin.php (file 3) for the appropriate database and connections strings. Once the appropriate information is retrieved it performs a purge.

    file 3: is the datalogin.php. This file holds all database information and conntection strings. Using if and else conditional statements, it assigns the appropriate variables based on the account id.

    I'm able to fully execute the concept if the purging file is hard coded with an accountid, but I'm trying to get this done dynamically with all available accountids.

    I trust this makes a little more sense....

    Best,
    Mossa
    Last edited by mbarandao; 05-10-2013 at 09:08 PM.

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    So the actually database itself is changing though yes? So this call here: define("DB_DATABASE", $account_db); does actually provide different $account_db, is that correct?
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #7
    Regular Coder
    Join Date
    Nov 2010
    Location
    Washington DC
    Posts
    338
    Thanks
    22
    Thanked 1 Time in 1 Post
    Yes, that is correct! Each account has its own database and thus connection string variable values. And all of that is predicated on the account id variable.

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Okay, then that makes more sense.
    Because of the way you have built the datalogin.php, you have no alternative but to spawn the multiple curl or socket connections for each request, or to use http wrappers with fread or even include with an http wrapper (fread will require allow_url_fopen, and include will require allow_url_include). That means you'll need to burn up some bandwidth to do it. All the above though should request through the web server since they will be either using curl/sockets (on port 80), or using http wrappers (which automatically do that for you).

    What is the include_get_params function do? Includes by themselves do not support the use of querystrings, so I'm curious the purpose of that one. Perhaps that already does what you are needing, but just not working properly?

    BTW, based on what you have here, the only thing that would prevent you from using functions and dynamically respawning the datalogin.php is this here:
    PHP Code:
    define("DB_HOST""localhost");
    define("DB_USER"$account_user);
    define("DB_PASSWORD"$account_pass);
    define("DB_DATABASE"$account_db); 
    Defined constants like function and classes can be declared only once. If it weren't for these, you could include this script as many times as you wanted to, and it would acquire the scope and variables at its inclusion point. So you could simply change a variable and then call include again and it will reflect that new variable (at least it should; I don't think I've *ever* included the same file more than once).
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    mbarandao (05-11-2013)

  • #9
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,090
    Thanks
    51
    Thanked 506 Times in 493 Posts
    Quote Originally Posted by Fou-Lu View Post
    What is the include_get_params function do? Includes by themselves do not support the use of querystrings, so I'm curious the purpose of that one. Perhaps that already does what you are needing, but just not working properly?
    From what I understand it's supposed to let you call / include a file in a url / querystring format. The function breaks it up, includes the appropriate file and populates the $_GET array manually which is then available to the included file.

    Gotta be honest I don't like that method / idea but I have to admit.. it's certainly a clever and inventive way of getting around the lack of querystring support. Like you though I did think it may prove troublesome but I gotta be honest I also didn't quite understand this help request so stayed out of it.
    My helpful sig is on vacation trying to loose some weight. It got a bit fat and caused a few problems but it will be back at some point!

  • #10
    Regular Coder
    Join Date
    Nov 2010
    Location
    Washington DC
    Posts
    338
    Thanks
    22
    Thanked 1 Time in 1 Post
    curl or socket! I definitely need some help here. I'm not very familiar.
    Last edited by mbarandao; 05-11-2013 at 02:01 AM.

  • #11
    Regular Coder
    Join Date
    Nov 2010
    Location
    Washington DC
    Posts
    338
    Thanks
    22
    Thanked 1 Time in 1 Post
    My experience with curl or socket is quite lacking, but here is what I have conjured up. I definitely need some guidance with it.

    the first script.

    PHP Code:
    <?php
    //* grabbing all available accountids
     
    $link mysql_connect('***''*****''*****');
        if(!
    $link) {
            die(
    'Failed to connect to the server: ' mysql_error());
        }
        
        
    //Select database
        
    $db mysql_select_db('******');
        if(!
    $db) {
            die(
    "Sorry I am Unable to select database");
        }
    $get_acctids="select accountid from company_info"
    $results mysql_query($get_acctids) or die(mysql_error());

    $new_array[] = $row;
    while( 
    $row mysql_fetch_assoc$results)){
    $acct_data[] = $row['accountid'];

    }

    $key $acct_data;

    foreach(
    $key as $value){
        
    $dr[] = "accountid = '$value' "// Build array of strings
    }

    $sql "select * from company_info WHERE ";
    $sql .= implode(' OR '$dr); // convert to string joined by ' OR ' and add to end of $sql
    // Instead of using a loop and running a query for 'Cable Guy'
    // and another query for 'John Doe', the following query fetches both rows.
    $result mysql_query($sql);
     
    $numrows=mysql_num_rows($result);
    //echo $numrows;
    while($row mysql_fetch_assoc($result))
      {
    $url 'https://*****/*****/purge_queueList_daily.php?accountid='.$row['accountid'].'';
      
    call_curl($url);
         

    }
    function 
    call_curl($url){
         
    $curl curl_init();
         
    curl_setopt_array($curl, array(
         
    CURLOPT_URL => $url,
         
    CURLOPT_TIMEOUT => '5'
         
    ));
         
    $resp curl_exec($curl);
         
    curl_close($curl);
        }

    ?>
    My secondary script for purging task same as before with a modification in connection link as per Fou-Lu advise

    PHP Code:
    <?php
    $club_id 
    =$_GET['accountid'];// This is from the first script and will be required in the included file below (datalogin.php)
    include 'datalogin.php';
    $link mysql_connect('localhost',$account_user$account_pass);
        if(!
    $link) {
            die(
    'Failed to connect to server: ' mysql_error());
        }
        
        
    //Select database
        
    $db mysql_select_db($account_db);
        if(!
    $db) {
            die(
    "Unable to select database");
        }
        echo 
    $db." <br />";
        
    //echo $db;
    $todayis=date("Y-m-d");//date and time
    $qry "DELETE FROM queue_sys where todays_date NOT LIKE '%$todayis%'";// remove players from the list from previous days
    $result=mysql_query($qry);        

    if(
    $result)
    {
    $message"<br /><br /><p><font size=5 color=blue>Queue table Has been Purged and Updated!</font>";
    }
    else
    {
                    
    $message="Purge failed!";        
             }
       
    // echo $message;
    ?>
    At the moment the secondary is executed the number of available accountids (in this test 11 times), but the tables are not purged.

    Any thoughts?

  • #12
    Regular Coder
    Join Date
    Nov 2010
    Location
    Washington DC
    Posts
    338
    Thanks
    22
    Thanked 1 Time in 1 Post
    On a second test, the concept is actually working!

    Thanks for the guidance Foul_lu

    Mossa

  • #13
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,090
    Thanks
    51
    Thanked 506 Times in 493 Posts
    Quote Originally Posted by mbarandao View Post
    On a second test, the concept is actually working!
    By setting the timeout property of curl, you can also implement a type of multithreading. As each request times out curl is then able to call another url and another and another in a loop etc. That means you can effectively run multiple scripts at the same time (though the only way to pass back the results is via a table or session - again using another loop until you get all the results you need).
    My helpful sig is on vacation trying to loose some weight. It got a bit fat and caused a few problems but it will be back at some point!

  • #14
    Regular Coder
    Join Date
    Nov 2010
    Location
    Washington DC
    Posts
    338
    Thanks
    22
    Thanked 1 Time in 1 Post
    tangoforce, thanks for the additional suggestion! If you may, could you provide an example of how this would actually be constructed if I wanted the following scripts to be executed with the same curl script:

    PHP Code:
    //first script
    $url 'https://*****/*****/purge_queueList_daily.php?accountid='.$row['accountid'].'';

    //second script

    $url 'https://*****/*****/cronJob_modifyStatus_phaseOne.php?accountid='.$row['accountid'].'';

    //third script
    $url 'https://*****/*****/cronJob_membReminders_phaseOne.php?accountid='.$row['accountid'].''
    At the moment, I have three cron jobs on each above scripts which then undergoes the entire process as covered in this entire post thread.

    If I understand your post correctly, you are suggesting that those jobs could be consolidated into one cron job/one curl multithreading. I trust my understanding is correct!

    Mossa--
    Last edited by mbarandao; 05-11-2013 at 01:10 PM.

  • #15
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,090
    Thanks
    51
    Thanked 506 Times in 493 Posts
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, 3);

    Thats the function and parameters you would need. The rest of it is just calling curl with each url in a loop. After 3 milliseconds curl will abort that http connection and then (in your loop) move onto the next url.

    In each script that curl calls, you must use the ignore_user_abort() function so that the script will continue to run after the connection is closed.

    Effectively something like this:
    PHP Code:
    $Url[] = 'http://www.domain1.com';
    $Url[] = 'http://www.domain2.com';
    $Url[] = 'http://www.domain3.com';

    // create a new cURL resource
    $ch curl_init();
    curl_setopt($chCURLOPT_CONNECTTIMEOUT_MS3);

    foreach(
    $Url as $Key => $Value)
       {
       
    // set URL and other appropriate options
       
    curl_setopt($chCURLOPT_URL$Value);

       
    // Call url
       
    curl_exec($ch);
       }

    // close cURL resource, and free up system resources
    curl_close($ch); 
    Then on each script / page it calls you just need to include this to ensure that once the calling http connection times out, the script continues to run:
    PHP Code:
    ignore_user_abort(true);//Use this to ensure script can carry on running

    //Do other stuff below
    sleep(30);
    log_something_to_database(); 
    curl also has the ability to call multiple urls internally - not something I've ever bothered using but it does support it:
    http://uk3.php.net/manual/en/functio...multi-exec.php
    My helpful sig is on vacation trying to loose some weight. It got a bit fat and caused a few problems but it will be back at some point!

  • Users who have thanked tangoforce for this post:

    mbarandao (05-11-2013)


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •