mysql parameter file with Array

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
SantaClara
New php-forum User
New php-forum User
Posts: 1
Joined: Fri Jan 15, 2021 3:04 am

Fri Jan 15, 2021 3:59 am

Good day everyone,

I'm a complete noob at php and this is my first post, so please be gentle with me but I am really excited to be here. Tia.

I have a prestashop site that is php based.
My issue is that i have a mySQL dbase that prestashop is using, I have decided to adopt this same dbase for some additional work that i am working on however i cant get my new php to reference the login credentials correctly. This is what I have:
urlRoot/shopdir/app/config/parameters.php - contains all the standard mySQL parameters used by prestashop, see code below.
urlRoot/mysqlconndir/mysqlconn.php - includes the file parameters.php and also contains additional parameters for use in my new application process
urlRoot/applicationdir/getdata.php - includes the file mysqlconn.php and uses the parameters passed into it.

I can create the getdata.php file that includes the mysqlconn.php parameters however i currently have to hard code the parameters in the mysqlconn.php file. I don't know how to read the array from the parameters.php file and then apply those to the mysqlconn.php variables.

Also if anyone has any recommendations on how coding conventions I would be delighted to hear about them to make my code better.

Hopefully you can help.

Thankyou in advance.

M@

urlRoot/shopdir/app/config/parameters.php

Code: Select all

<?php return array (
  'parameters' => 
  array (
    'database_host' => 'hosting.com',
    'database_port' => '',
    'database_name' => 'dbaseName',
    'database_user' => 'dbaseUsername',
    'database_password' => 'dbasePassword',
    'database_prefix' => 'ps_',
    'database_engine' => 'dbaseEngine',
    'mailer_transport' => 'smtp',
    'mailer_host' => '0.0.0.0',
    'mailer_user' => 'mailerUser',
    'mailer_password' => 'mailerPassword',
    'secret' => 'ssssshuuuushhhh',
    'ps_caching' => 'prestashopcashing',
    'ps_cache_enable' => trueorfalse,
    'ps_creation_date' => '1901-01-01',
    'locale' => 'en-GB',
    'use_debug_toolbar' => trueorfalse,
    'cookie_key' => 'strCookieKey',
    'cookie_iv' => 'strCookieIV',
    'new_cookie_key' => 'strCookieKeyNew',
  ),
);
urlRoot/mysqlconndir/mysqlconn.php

Code: Select all

<?php
	//get array of connection string properties for the dbase
	//include_file("../mtd/app/config/parameters.php");
	
	
	// Opens a connection to the database
	// Since it is a php file it won't open in a browser 
	// It should be saved outside of the main web documents folder
	// and imported when needed


	// Defined as constants so that they can't be changed these are what I tried but it doesn't like it.
	//DEFINE ('DB_HOST', $database_host);
	//DEFINE ('DB_USER', $database_user);
	//DEFINE ('DB_PASSWORD', $database_password);
	//DEFINE ('DB_NAME', $database_name);

	// Defined as constants so that they can't be changed these are what I tried but it doesn't like it.
	DEFINE ('DB_HOST', 'hosting.com');
	DEFINE ('DB_USER', 'dbaseUsername');
	DEFINE ('DB_PASSWORD', 'dbasePassword');
	DEFINE ('DB_NAME', 'dbaseName');

	DEFINE ('OTHERPARAMETERTOBEUSED', 'shushDontTellAnyoneTopSecret');
	// $dbc will contain a resource link to the database
	// @ keeps the error from showing in the browser

	$dbc = @mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
	OR die('Could not connect to MySQL: ' .
	mysqli_connect_error());
?>

urlRoot/applicationdir/getdata.php

Code: Select all

<?php
// Get a connection for the database
require_once("../mysqlconndir/mysqlconn.php");

// Create a query for the database
$query = "SELECT  field1, field2 FROM ps_tablename";

// Get a response from the database by sending the connection
// and the query
$response = @mysqli_query($dbc, $query);

// If the query executed properly proceed
if($response){

echo '<table align="left"
cellspacing="5" cellpadding="8">
<tr>
<td align="left"><b>field1</b></td>
<td align="left"><b>field2</b></td>
</tr>';

// mysqli_fetch_array will return a row of data from the query
// until no further data is available
while($row = mysqli_fetch_array($response)){

echo '<tr>';
echo '<td align="left">' . $row['field1'] . 			'</td>';
echo '<td align="left">' . $row['field2'] . 		'</td>';
echo '</tr>';
}

echo '</table>';

} else {

echo "Couldn't issue database query<br />";

echo mysqli_error($dbc);

}

// Close connection to the database
mysqli_close($dbc);

?>
Post Reply