PHP Classes

Struggling passing database variables into class

Recommend this page to a friend!

      Danen MySQL Backup  >  All threads  >  Struggling passing database...  >  (Un) Subscribe thread alerts  
Subject:Struggling passing database...
Summary:Stuck...
Messages:7
Author:wayne b
Date:2016-04-04 18:18:04
 

  1. Struggling passing database...   Reply   Report abuse  
Picture of wayne b wayne b - 2016-04-04 18:18:04
Hi

Forgive my inability to work this out myself - still working on it :-(

I am trying to work out how to pass the connection credentials as variables from the doBackup.php

for example: doBackup.php

$host = "localhost";
$db = "database_name";
$username = "SomeUser";
$password = "SomePassword";

$B = new danenMysqlBackup();

And in the class php pass them into the __construct function

... I know it should be easy but for some reason I am struggling...

Many thanks in advance.

  2. Re: Struggling passing database...   Reply   Report abuse  
Picture of Gerry Danen Gerry Danen - 2016-04-04 18:30:50 - In reply to message 1 from wayne b
Hi Wayne,

Put that information in the last function in the class, getCredentials():

private function getCredentials()
{
// adapt to your environment -- FIXME
$credentials = array();
$credentials['hostname'] = 'localhost';
$credentials['username'] = 'myUsername';
$credentials['password'] = 'myPassword';
$credentials['database'] = 'myDatabase';
return $credentials;
}

  3. Re: Struggling passing database...   Reply   Report abuse  
Picture of wayne b wayne b - 2016-04-04 18:42:13 - In reply to message 1 from wayne b
Hi

Thanks for such a quick reply...

I foolishly Realised after posting about the function at the bottom.

So where I am now is trying to work out how to get variables from the doBackup file into the getCredentials function so the class can be used for several databases?

I am googling passing variables into a function and all I do is break the script.

  4. Re: Struggling passing database...   Reply   Report abuse  
Picture of wayne b wayne b - 2016-04-04 19:42:32 - In reply to message 2 from Gerry Danen
Hi

Thanks for such a quick reply...

I foolishly Realised after posting about the function at the bottom.

So where I am now is trying to work out how to get variables from the doBackup file into the getCredentials function so the class can be used for several databases?

I am googling passing variables into a function and all I do is break the script.

  5. Re: Struggling passing database...   Reply   Report abuse  
Picture of Gerry Danen Gerry Danen - 2016-04-04 19:58:00 - In reply to message 3 from wayne b
Add this to the class:

public function setCredentials($hostname, $username,$password,$database)
{
$this->hostname = $hostname;
$this->username = $username;
$this->password = $password;
$this->database = $database;
}

Then, after this line,
$B = new danenMysqlBackup();

Add:
$B->setCredentials("yourhost", "host", "pw", "db");


  6. Re: Struggling passing database...   Reply   Report abuse  
Picture of Gerry Danen Gerry Danen - 2016-04-04 20:18:10 - In reply to message 5 from Gerry Danen
<?php
// example to do a full database backup

require_once 'danenMysqlBackup.class.php';


// custom credentials
$customHostname = 'myHost';
$customUsername = 'myUser';
$customPassword = 'myPw';
$customDatabase = 'myDb';


$B = new danenMysqlBackup();

$backupSqlFile = $B->backupStart();

// if custom credentials are needed, pass them here
// this call is optional
$B->setCredentials($customHostname, $customUsername, $customPassword, $customDatabase);


if ( $backupSqlFile )
{
$backupSqlFile = basename($backupSqlFile);
echo "Created: $backupSqlFile";
}
else
{
echo "ERROR: backup failed.";
}

  7. Re: Struggling passing database...   Reply   Report abuse  
Picture of wayne b wayne b - 2016-04-05 06:13:39 - In reply to message 5 from Gerry Danen
Hi

Works perfectly...

Thank you :-)