Jump to content

how to connect to database?!?


vahid abdi
 Share

Recommended Posts

i want to connect to database in a file that i've created in main folder of prestashop... actually i want to use it's own classes to connect.. what would i add in my code?

 

i need this soon.. so plz help if you can

 

thanks for helping ...

Share this post


Link to post
Share on other sites

By the looks you just have to include config file and then you are free to use all PS codebase.

 

Example:

<?php

require(dirname(__FILE__).'/config/config.inc.php');

$query = new DbQuery();

$query
    ->select('*')
    ->from('customer')
    ->limit(10);

$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($query);

d($result);
  • Like 2

Share this post


Link to post
Share on other sites

in the code above

$query
->select('*')
->from('customer')
->limit(10);

 

it works pretty good

but now i want to do it like this

 

$query
->select('column1,column2')
->from('customer, address')

->where('customer.column1="1" AND address.column2="something"');

........

yea i want to take data from two tables that column1 belongs to customer and the other one to address

but it doesn't works

what can i do???
 

Share this post


Link to post
Share on other sites

You can check DbQueryCore class for all available options.

 

 

I can do one more for you:

$query2 = new DbQuery();

$query2
    ->select('c.firstname')
    ->select('a.lastname')
    ->from('customer', 'c')
    ->leftjoin('address', 'a', 'c.id_customer = a.id_customer')
    ->where('c.active = 1')
    ->where('a.phone = 0102030405');

$result2 = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($query2);

You can write it in many forms, just pick one you like

For example:

// This
$query2->select('c.firstname');
$query2->select('a.lastname');

// Is same as this
$query2->select('c.firstname, a.lastname');

// Add alias
$query2->select('c.firstname F, a.lastname L');
  • Like 3

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More