Drupal get database configuration - database

Is there any function preserved to extract database configuration inside of my code, something like
$db_user = drupal_get_dbuser();
$db_pass = drupal_get_dbpass();
...
...

The code for this is:
<?php
global $db_url;
$creds = parse_url($db_url);
print $creds['user'];
print $creds['pass'];
var_dump($creds);
?>
More detailed info and edge-cases can be found by reading the code of http://api.drupal.org/api/function/db_connect/6

Related

CakePHP 3: How to check if file or image exists

I am just trying to check if an image exists or not, I can do it by using PHP. For example:-
$file = WWW_ROOT .'uploads' . DS . 'employee' . DS .'_'.check.jpg;
$file_exists = file_exists($file);
It's working fine for me. But I have tried also tried using elementExists like this:-
if($this->elementExists("../".$employees->front_image))
{
echo $this->Html->image("../".$employees->front_image); // image output fine without condition.
}
// Here $employees->front_image = uploads/employee/employeename.jpg
This check is not working. How can I do this in CakePHP?
CakePHP is written in PHP, so if you already have a simple solution like file_exists() use that. So you can write something like this:-
if (file_exists(WWW_ROOT . $employees->front_image)):
echo $this->Html->image('../' . $employees->front_image);
endif;
elementExists() is intended for checking that a View element exists, not if files exist in the webroot, so should not be used like you are trying. It does do a file_exists() check, but this scans all available View element paths only.
I think this works in Cake 3 (you should do this in afterFind method IMO):
// Create a new file with 0644 permissions
$file = new File('/path/to/file.php', true, 0644);
if ($file->exists()) {
//do something
}
$file->close();
Your way is checking whether view element exists or not.
This worked for me:
Q: How to check if image exists on remote url or not?
Explanation:
$rfile will take url
$check will open the file with read rights
If file exists then it will print "File exists" else "Doesn't exists".
code:
<?php
// Remote file url
$rFile = 'https://www.example.com/files/test.pdf';
// Open the file
$check = #fopen($rFile, 'r');
// Check if the file exists
if(!$check){
echo 'File does not exist';
}else{
echo 'File exists';
}
?>

Passing Array data using SESSION

I am trying to pass a Single/Multi Dimensional array to a second page using SESSION. I read and tried all questions and answers but can not get it done. Here is the 1st page:
<?php
// SendIt.php
SESSION_START();
$iA=array('A1','A2','B2','B1','B3','A3');
print_r($iA);
$_session['iA'] = $iA;
echo '<br>Click to send the array.';
?>
Here is the 2nd Page:
<?php
// GetIt.php
SESSION_START();
$iB = $_SESSION['iA'];
print_r($iB);
echo 'I am Here...';
?>
I do not get any of the iA array in the second page.
I must be missing something simple. Please check it out. Thx
I got help from a different PHP forum site and was advised that: Like any other PHP variable, _SESSION is case sensitive. So the problem is fixed by matching _SESSION variables and making them all upper case.

Cakephp ghost file

I am facing a curious situation. I am using CakePHP 2.0 (locally), XAMPP and I wanted to add a simple hit counter in my homepage so I added the following code (very very simple)
<?php
$filename = 'hitcount.txt';
$handle = fopen($filename, 'r');
$hits = trim(fgets($handle)) + 1;
fclose($handle);
$handle = fopen($filename, 'w');
fwrite($handle, $hits);
fclose($handle);
echo $hits;
There is a text file named hitcount.txt which contains the number of hits (everytime I visit the page it should increase the number of hits). It works. The problem appeared when I tried to access the hitcount.txt file. It was empty but the echo of $hits returned the exact result! I deleted the file and it still shows me the expected result! I used a different browser, the same. I deleted CakePHP's cache, no change. I used the same piece of code in another page and it did not complain with some error, returning the expected result.
How is it possible for Cakephp to "see" a file that does not exist? Has it anything to do with Apache?
You probably view the file at the wrong location as CakePHP's. My guess is CakePHP's referring to the file at app/webroot/hitcount.txt.
You might want to define a full path for hitcount.txt so you can be sure that you and CakePHP are both referring to the same location.
<?php
$filename = TMP.'hitcount.txt';
This would locate the file at `app/tmp/hitcount.txt'.

Drupal 7 - node custom display

I've got this problem. I've created file node--mycontenttype.tpl.php to display nodes in custom way. I've listed all the $content array by print_r($content). I can display all the variables except CCK fields. For example I can print out node type like:
<?php print $content['body']['#bundle']; ?>
But if I try to display any CCK field like:
<?php print $content['body']['#object']->field_url[und][0]['value']; ?>
It gives me an error "Notice: Use of undefined constant und - assumed 'und' w include()". Alright, so the "und" means "undefined" for langauge, but nor 'pl', nor 'en' solves the problem. How can I manage this?
Alright, after researching I've finally found an answer. It works, but in some cases it look a bit inefficient. Code goes like this:
<?php
$output = field_get_items('node', $node, 'field_url');
$output = $output[0]['safe_value'];
print $output;
?>
But if you have a lot of CCK fields it looks like you have to launch field_get_items() function a lot of times. If any of you knows a better approach it could be nice you could share.
When you write [und] Drupal (PHP) assumes that there's variable $und defined somewhere in the code.
You should use:
<?php print $content['body']['#object']->field_url['und'][0]['value']; ?>

In Magento how do you get the database name?

How do you get the database name from Magento?
I have seen functions like the one below that can get the table name.
$orderItemTable = Mage::getSingleton('core/resource')->getTableName('sales/order_item');
I was hoping that there was some sort of function like this:
Mage::getSingleton('core/resource')->getDatabaseName();
Thanks in advance for any ideas.
Each module can specify it's own connection so you are right to go via a model.
$config = Mage::getResourceModel('sales/order')->getConnection()->getConfig();
// $config is an array
$dbname = $config['dbname'];
If you're only interested in the default a slightly more efficient way might be:
$dbname = (string)Mage::getConfig()->getNode('global/resources/default_setup/connection/dbname');
To get the db name try
Mage::getConfig()->getResourceConnectionConfig('default_setup')->dbname;
See How to get magento database details
It is always possible to get the current connection from Mage::getSingleton('core/resource')->getConnection('core_write'), quite specially if you use only one database.
$configArray = Mage::getSingleton('core/resource')->getConnection('core_write')->getConfig();
$db = $configArray['name'];
But it's comming with a zend adapter Zend_Config
// Create the object-oriented wrapper upon the configuration data
$config = new Zend_Config($configArray);
$db = $config->get('dbname');

Resources