I cannot find any examples, in books or on the web, describing how one would properly initialize an associative array by name only (with empty values) - unless, of course, this IS the proper way(?)
It just feels as though there is another more efficient way to do this:
config.php
class config {
public static $database = array (
'dbdriver' => '',
'dbhost' => '',
'dbname' => '',
'dbuser' => '',
'dbpass' => ''
);
}
// Is this the right way to initialize an Associative Array with blank values?
// I know it works fine, but it just seems ... longer than necessary.
index.php
require config.php
config::$database['dbdriver'] = 'mysql';
config::$database['dbhost'] = 'localhost';
config::$database['dbname'] = 'test_database';
config::$database['dbuser'] = 'testing';
config::$database['dbpass'] = 'P#$$w0rd';
// This code is irrelevant, only to show that the above array NEEDS to have Key
// names, but Values that will be filled in by a user via a form, or whatever.
Any recommendations, suggestions, or direction would be appreciated. Thanks.
What you have is the most clear option.
But you could shorten it using array_fill_keys, like this:
$database = array_fill_keys(
array('dbdriver', 'dbhost', 'dbname', 'dbuser', 'dbpass'), '');
But if the user has to fill the values anyway, you can just leave the array empty, and just provide the example code in index.php. The keys will automatically be added when you assign a value.
First file:
class config {
public static $database = array();
}
Other file:
config::$database = array(
'driver' => 'mysql',
'dbhost' => 'localhost',
'dbname' => 'test_database',
'dbuser' => 'testing',
'dbpass' => 'P#$$w0rd'
);
Related
I have 2 different php files, and in one of the file I created a global array
$GLOBALS['system'] = array(
'mysqli' => array(
'host' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'database'
)
);
How can I be able to use this array in another file e.g.
$GLOBALS['system']['mysql']['host'];
$GLOBALS['system'] = array();
This is unnecessary. Just do
$system = array();
Now, you can use $system anywhere you want (in other includes, etc), but the catch is that functions won't see it due to scope. That means that each function doesn't have access to $system because it's not in the global scope (and that's a good thing because what if you needed to use $system inside a function?) Now, you can always fall back to
function foo() {
echo $GLOBALS['system'];
}
But that's clunky and it relies on $system being defined somewhere and not changing. So let's inject it instead
function foo($system) {
echo $system;
}
foo($system);
I've a PHP Configuration File in TYPO3 like
<?php
return array(
'DB' => array(
'database' => 'vicous',
'extTablesDefinitionScript' => 'extTables.php',
'host' => 'localhost',
'password' => '',
'socket' => '',
'username' => 'root',
)
);
?>
I want to get this array on an external php file. How to get that?
Something like this.
function getArray("filepath"){
$variable = filepath return that array
}
My requirement is to get array inside $variable
include and require behave exactly like you want:
$variable = include 'path/to/file.php';
From the docs:
It is possible to execute a return statement inside an included file
in order to terminate processing in that file and return to the script
which called it. Also, it's possible to return values from included
files. You can take the value of the include call as you would for a
normal function.
You Could use like
function getArray($path){
return $variable = require_once( $path );
}
I'm new to PHP. What I'm trying to achieve is declare multiple DB hosts in an array. Something like below mentioned :
$slaves=array(
array( //Slave 1
'name' => 'slave1',
'server' => '10.10.1.10',
'user' => 'user1',
'password' => 'p#ssuser1'
'port' => '3306'),
array( //Slave 2
'name' => 'slave2',
'server' => '10.10.1.11',
'user' => 'user2',
'password' => 'p#ssuser2'
'port' => '3306'),
)
Once done, I want to make random calls to these slaves i.e., every read request should get served from one of the slaves.
$idx = time() % count($slaves);
$slave = $slaves[$idx];
$con = mysqli_connect($slave['server'], $slave['user'], $slave['password'], "dbname");
But this is not working. If I try to connect these slaves individually, it is working. But not when I declare that in an array. Is there a simple way to make random calls to these slaves ? Please highlight where is the mistake in my code. I have checked few links on web but they use functions and this keyword to make DB calls.
If you require any more information, do let me know.
Thanks in advance.
$slave = $slaves[array_rand($slaves)];
$con = mysqli_connect($slave['server'], $slave['user'], $slave['password'], "dbname");
You should use array_rand function to get random value from array. Check it with this modification.
And you forget to pass database name to connect function.
I got configuration file database.php
<?php defined('_ENGINE') or die('Access Denied'); return array (
'adapter' => 'mysqli',
'params' =>
array (
'host' => 'localhost',
'username' => 'root',
'password' => 'root',
'dbname' => 'db',
'charset' => 'UTF8',
'adapterNamespace' => 'Zend_Db_Adapter',
),
'isDefaultTableAdapter' => true,
'tablePrefix' => 'engine4_',
'tableAdapterClass' => 'Engine_Db_Table',
); ?>
How to get only password from this array?
something like echo $array['password'];
How do I get the array from database.php?
You'll need to include the file and bind the returned value to a variable, such as in the below example.
$db_conf = require ('/path/to/database.php');
$db_conf will contain the data returned by database.php.
Documentation
PHP: include - Manual
How do I read the specific value from my array?
Since you are working with a nested array the solution is not as far away as you might think. First use $a[key] to get to the array stored under params, and then get the value of password from there.
As in the below example.
$password = $array['params']['password'];
Note: The above is, in a logical sense, equivalent to;
$params = $array['params'];
$password = $params['password'];
Documentation
PHP: Arrays - Manual
I tried the above but it's just shouting "Access Denied" in my face, why?
To protect database.php from unintended access it has been protected with a check to see so that it's being used inside of the engine.
The script will die if _ENGINE is not defined.
If you want to use database.php in a script outside of the thought of engine you'll need to define the _ENGINE constant before includeing the file.
define ('_ENGINE', 1);
...
$db_conf = include ('database.php');
echo $array['params']['password'];
Give this a try. :-)
I would like to return a single element of a Mongodb array. I am taking a username and password from a form and using the findOne() function to verify it's existence in the database.
$user = $collection->findOne(array(
'username' => $username,
'password' => $password,
));
In the array that it returns there is also a zip code. I would like to store that element of the array in a variable to concatenate it with another variable.
<?php
$user = $collection->findOne(array(
'username' => $username,
'password' => $password,
));
var_dump($user); // you will see your document as a PHP associative array here
$myPin = $user['pincode_keyname']; // or whatever name your pincode element has
?>
You don't need an to return all this information. As I understood all you need is a pincode, so your query should be this way
$user = $collection->findOne(
array(
'username' => $username,
'password' => $password,
),
array(
'pincode' => 1,
'_id' => 0
)
)
;
$user['pincode'] will be a pincode you need.
the second array ensure that you will not receive any additional and unimportant information except of pincode