4 Replies - 714 Views - Last Post: 28 November 2014 - 01:40 PM Rate Topic: -----

#1 dsnowdon   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 221
  • Joined: 18-January 12

Running php in the teminal

Posted 28 November 2014 - 11:17 AM

Hi, i was wondering if anyone has encountered this problem before, when I run my script in the browser it runs fine, with no errors, but then I run it in the terminal i get MYSQL connection errors.

the errors I get are simple sql connection errors.
Warning: mysqli_connect(): (HY000/2002): No such file or directory in /Users/Dale/GoogleDrive/Projects/Dale Snowdon Computing/htdocs/PHP_classes/Database_Agent.class.php on line 38
Failed to connect to MySQL: No such file or directory
Warning: mysqli_query() expects parameter 1 to be mysqli, boolean given in /Users/Dale/GoogleDrive/Projects/Dale Snowdon Computing/htdocs/PHP_classes/Page_Agent.class.php on line 43

Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, null given in /Users/Dale/GoogleDrive/Projects/Dale Snowdon Computing/htdocs/PHP_classes/Page_Agent.class.php on line 44

Warning: mysqli_query() expects parameter 1 to be mysqli, boolean given in /Users/Dale/GoogleDrive/Projects/Dale Snowdon Computing/htdocs/PHP_classes/Page_Agent.class.php on line 55

Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, null given in /Users/Dale/GoogleDrive/Projects/Dale Snowdon Computing/htdocs/PHP_classes/Page_Agent.class.php on line 56

Warning: mysqli_query() expects parameter 1 to be mysqli, boolean given in /Users/Dale/GoogleDrive/Projects/Dale Snowdon Computing/htdocs/PHP_classes/Page_Agent.class.php on line 64

Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, null given in /Users/Dale/GoogleDrive/Projects/Dale Snowdon Computing/htdocs/PHP_classes/Page_Agent.class.php on line 65





Thanks in advance
Dale

Is This A Good Question/Topic? 0
  • +

Replies To: Running php in the teminal

#2 andrewsw   User is offline

  • palpable absurdity
  • member icon

Reputation: 6905
  • View blog
  • Posts: 28,565
  • Joined: 12-December 12

Re: Running php in the teminal

Posted 28 November 2014 - 11:22 AM

It would help if you posted the two scripts you are running for comparison.
Was This Post Helpful? 0
  • +
  • -

#3 dsnowdon   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 221
  • Joined: 18-January 12

Re: Running php in the teminal

Posted 28 November 2014 - 11:28 AM

View Postandrewsw, on 28 November 2014 - 06:22 PM, said:

It would help if you posted the two scripts you are running for comparison.



I shouldn't matter it works in a Browser instance but not in a Terminal instance.

anyway


class ProjectView_Agent {
	
	/** Holds the data transfer object all the projects retreived
	  */
	private $projectsDTO;
	/** Holds the connection variable being used by the class
	  */
	private $con;
	/** Holds the initial MYSQL result set of project created in the constructor method.
	  */
	private $projects_row;
	
	/** Constructor creats project result set from two limits
	  * the collects the data for the objects and stores them.
	  * @param start_limit 	the id of the first file
	  * @param end_limit 	the id of the end file
	  */
	function __construct($start_limit, $end_limit){
		// get the connection to the database
		$this->con = new Database_Agent();
		$con = $this->con;
	}

}



<?php

/** Class creates and keeps the connection to the database, it can also close the connection.
  * Each class instantuatied runs and closes it own instance
  *
  * @author Dale Snowdon <[email protected]>
  * @version 1.1
  * @since 20/11/14
  * @lastupdate 25/11/14
  * @copyright	Copyright (c) 2014 D Snowdon Computing
  *
  *
  * history:
  * 1.1  
  *	   -   Impoved memory usage
  */
class Database_Agent {
	/** Holds the name of the local host.
	  */
	static private $DB_HOSTNAME = 'localhost';
	/** Holds the username of the database.
	  */
	static private $DB_USERNAME = 'root';
	/** Holds the password to the MYSQL account.
	  */
	static private $DB_PASSWORD = '';
	/** Holds the database name.
	  */
	static private $DB_NAME = 'dsnowdon_v4_1';
	/** Hold the instances databse conection.
	  */
	static protected $_db_connect;
	
	function __construct(){
        $this->_db_connect = mysqli_connect(self::$DB_HOSTNAME,
											self::$DB_USERNAME,
											self::$DB_PASSWORD,
											self::$DB_NAME);
											
											// Check connection
		if (mysqli_connect_errno()) {
  			echo "Failed to connect to MySQL: " . mysqli_connect_error();
		}

    }

    function get_connection() {
		return $this->_db_connect;
	}
	function close_con() {
		mysqli_close($this->_db_connect);
	} // end function close_con
	// Auto close connection is foggot
	function __destruct() {
		mysqli_close($this->_db_connect);
	} // end function auto close conection
} // end class Database_Handler


?>


<?php

/** Class Page runs on multiple instances to retrive content for a page.
  *
  * @author Dale Snowdon <[email protected]>
  * @version 1.1
  * @since 20/11/14
  * @lastupdate 24/11/14
  * @copyright	Copyright (c) 2014 D Snowdon Computing
  *
  *
  * requires: Database_Agent.class.php version 1.0 >
  *
  * history:
  * 1.1
  *	   -   
  */
class Page_Agent
{
	/** Holds the page title.
	  */
	private $title;
	/** Holds the page description.
	  */
	private $description;
	/** Holds an nested array containing the pages 'content_element(s)'  and
	  * its children 'content_block(s)'. [][]
	  */
	private $content;
	/** Holds the connection variable for page.
	  */
	private $con;
	
	
	
	/** Sets all the variables for the page.
	  */
	public function __construct($page_id) {
		 $this->con =  new Database_Agent();
		
		// get the pages content_elements and their respective content_blocks
		$sql = "SELECT title, description, content_element_ids FROM pages WHERE id='$page_id'";
		$result = mysqli_query($this->con->get_connection(), $sql);
		$row = mysqli_fetch_assoc($result);
	
		// get info from the pages table
		$this->title = $row['title'];
		$this->description = $row['description'];
		$content_elements = explode(",",$row['content_element_ids']);
		
		// get info from the contnet elements table(s)
		$content_elements_data;
		foreach($content_elements as $key => $value) {
			$sql = "SELECT content_block_ids FROM content_elements WHERE id='$value'";
			$result = mysqli_query($this->con->get_connection(), $sql);
			$row = mysqli_fetch_assoc($result);
			
			// get info from its blocks
			$content_blocks = explode(",",$row['content_block_ids']);
			
			// for each block get the content
			foreach($content_blocks as $key2 => $value2) {
				$sql = "SELECT content FROM content_blocks WHERE id='$value2'";
				$result = mysqli_query($this->con->get_connection(), $sql);
				$row = mysqli_fetch_assoc($result);
				$content_elements_data[$key][$key2] = $row['content'];
			}
		} // end for each
		$this->content = $content_elements_data;
	} // end functon __construct
	
	
	
	// --------------------------------------------------------------------------
	// Get methods
	// --------------------------------------------------------------------------
	public function getContent() {
		return $this->content;
	} // end funciton getContent
	public function getDesc() {
		return $this->description;
	} // end function getDesc
	public function getTitle() {
		return $this->title;
	} // end method getTitle
	
	
	// --------------------------------------------------------------------------
	// Close SQL connection to the pages table
	// --------------------------------------------------------------------------
	public function endPage() {
		$temp = $this->con;
		$temp->close_con();
	}
	
} // end class Page
?>




index.php
<?php
/** Template for D Snowdon website 
  * @Version		4.1
  * @Since			11/9/2014
  * @lastupdated	24/11/2014
  * @author			Dale Snowdon <[email protected]>
  */ 
require 'PHP_classes/Database_Agent.class.php'; 			 // get the con variable
require 'PHP_classes/Page_Agent.class.php'; 				// fucntions from gettting page content
$page = new Page_Agent('1');
$title = $page->getTitle();
$desc = $page->getDesc();
$content = $page->getContent();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><!-- InstanceBegin template="/Templates/main_v4.1 new.dwt.php" codeOutsideHTMLIsLocked="false" -->
<head>
 <meta charset="utf-8">
 <!-- InstanceBeginEditable name="doctitle" -->
  <title><? echo $title; ?></title>
 <!-- InstanceEndEditable -->
 <!-- InstanceBeginEditable name="docdesc" -->
 <meta name="description" content="<? echo $desc; ?>">
 <!-- InstanceEndEditable -->
 <meta name="author" content="Dale Snowdon">
 <meta charset="UTF-8" />
 
 <link rel="stylesheet" href="css/Base/layout.css"><!-- css  MUST REPLACE WITH A COMBINED/OPTIMIZED SHEET FOR PROFORMANCE-->
 <link rel="stylesheet" href="css/Base/normalize.css">
 <link rel="stylesheet" href="css/Base/typeography.css">
 <link rel="stylesheet" href="css/Components/alerts.css">
 <link rel="stylesheet" href="css/Components/buttons.css">
 <link rel="stylesheet" href="css/Components/forms.css">
 <link rel="stylesheet" href="css/Components/list.css">
 <link rel="stylesheet" href="css/Components/tables.css">
  <link rel="stylesheet" href="css/Components/images.css">
 <link rel="stylesheet" href="css/Components/popup_box.css">
 <link rel="stylesheet" href="css/Modules/footer.css">
 <link rel="stylesheet" href="css/Modules/headerv2.css">
 <link rel="stylesheet" href="css/Modules/social_links.css">


 
 <!-- java script -->
 <script src="Javascript/jquery-1.11.1.min.js"></script>
 <script src="Javascript/main.js"></script>

 
 <link rel="shortcut icon" href="Images/main/favicon.ico">
 <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
<!-- InstanceBeginEditable name="head" -->
<!-- InstanceEndEditable -->
</head>

<body>
<div id="blanket" style="display:none;"></div>
<div id="popUpDiv" style="display:none;">
	<a href="#" id="closeLink" >Click to Close CSS Pop Up</a>
</div>	
<? include 'HTML/header_nav.php'; ?>
<div class="container clearfix">
<!-- InstanceBeginEditable name="EditRegion3" -->
  <div class="three_cell_con clearfix"> <!-- end clear fix -->
    <div class="three_cell content_style clearfix cell_one">
      <? echo $content[0][0]; ?>   
    </div>
        
    <div class="three_cell content_style clearfix cell_two">
      <? echo $content[0][1]; ?>  
    </div>
          
          
    <div class="three_cell content_style clearfix cell_three">
      <? echo $content[0][2]; ?>  
    </div>
  </div><!-- Div to  End seprate content from the footer -->

  <div class="clearfix"> <!-- end clear fix -->
    <section class="clearfix content_style" style="height: 158px;">
	  <? echo $content[1][0]; ?>   
    </section>
      
    <aside class="clearfix content_style" style="height: 158px;">
	  <? echo $content[1][1]; ?>             
    </aside>
  </div>
<!-- InstanceEndEditable -->
</div>

<footer>

 <div class="three_cell_con clearfix"> <!-- end clear fix -->
 <!--<a id="link">Click to Open CSS Pop Up</a>-->
 	<div class="three_cell clearfix cell_one">
       <p>Copyright  2015 &nbsp; <a href="http://www.dsnowdon.co.uk"> D Snowdon Computing</a></p> 
    </div>
    
    <div class="three_cell clearfix cell_two">
   	 <h4>Contact Information</h4>
     <ul><li>Phone : 07463 369933</li>
     <li>Email : [email protected] </li></ul>
    </div> 
      
      
      <div class="three_cell clearfix cell_three">
      	<div class="menu_con">
        	<a href="http://www.members.dsnowdon.com">
            	Members Login
            </a>
        </div>
      </div>
  </div><!-- Div to  End seprate content from the footer -->
  
  
   <div class="three_cell_con clearfix"> <!-- end clear fix -->
 <!--<a id="link">Click to Open CSS Pop Up</a>-->
 	<div class="three_cell clearfix cell_one">
     
    </div>
    
    <div class="three_cell clearfix cell_two">
    <br />
   	 <h4>Misc Information</h4>
     <ul><li><a href="#">View Company Ethos</a></li>
     <li><a href="#">View Cookies Policy</a></li></ul>
    </div> 
      
      
      <div class="three_cell clearfix cell_three">
      <a href="#">Sign up to the newsletter</a>
      	      </div>
  </div><!-- Div to  End seprate content from the footer -->

</footer>
</body>
<!-- InstanceEnd --></html>
<?php
$page->endPage(); // mysqli_close on this pages connection instance
?>

Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb   User is online

  • D.I.C Lover
  • member icon

Reputation: 3837
  • View blog
  • Posts: 13,994
  • Joined: 08-August 08

Re: Running php in the teminal

Posted 28 November 2014 - 01:30 PM

Did you cd into the same directory as index.php or did you type the path to it in your command?
Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6259
  • View blog
  • Posts: 24,028
  • Joined: 23-August 08

Re: Running php in the teminal

Posted 28 November 2014 - 01:40 PM

Settings often differ between the CLI and webserver PHP. In any event, the first error is the relevant one. The "HY000/2002" is the important thing to search for in Google. There may be multiple reasons for it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1