Follow us on Twitter X-Cart on Facebook Wiki
Shopping cart software Solutions for online shops and malls

Running Simple Selects on 3rd party table

 
Reply
   X-Cart forums > X-Cart 5 > Dev Questions (X-Cart 5)
 
Thread Tools Search this Thread
  #1  
Old 05-02-2016, 04:59 PM
 
Stormer Stormer is offline
 

Member
  
Join Date: May 2016
Posts: 15
 

Default Running Simple Selects on 3rd party table

Hello,

I would like to run some simple selects on a 3rd party table in my X-Cart 5.2 database. I tried to create an entity class and access it that way, but I noticed the X-Cart table prefix was added on so it just created a new table.

Any sample code someone can share to point me in the right direction?

Thanks!
__________________
X-Cart 5.3
Reply With Quote
  #2  
Old 05-03-2016, 02:25 PM
 
BurtonTech BurtonTech is offline
 

Advanced Member
  
Join Date: Aug 2007
Posts: 46
 

Default Re: Running Simple Selects on 3rd party table

Not sure if this is the best solution, but it works for MySQLi:
Code:
//Get the Connection info from X-Cart 5 $xc_conn = \XLite\Core\Database::getEM()->getConnection(); //Collect the info you need for a MySQLi connection $sql_host =$xc_conn->getHost(); $sql_db =$xc_conn->getDatabase(); $sql_user =$xc_conn->getUsername(); $sql_password =$xc_conn->getPassword(); //Collect your data using MySQLi $conn = new mysqli($sql_host, $sql_user, $sql_password,$sql_db); if ($conn->connect_error) { error_log('MySQLi could not connect: ' . $conn->connect_error); } $sql_query = "SELECT * FROM YourTable"; $db_results = $conn->query($sql_query); $results_array = $db_results->fetch_assoc(); if ($db_results->num_rows > 0) { foreach($results_array as $dbkey=>$dbvalue) { echo 'Key: '.$dbkey.' = '.$dbvalue.'<br />'; } }

Hope that helps!
__________________
X-Cart Gold 4.4.5
X-Cart Ultimate 5.2.22
X-Cart Business 5.3.2.13
X-Payments 3
Reply With Quote

The following 2 users thank BurtonTech for this useful post:
qualiteam (05-03-2016), Stormer (05-03-2016)
  #3  
Old 05-03-2016, 02:36 PM
 
BurtonTech BurtonTech is offline
 

Advanced Member
  
Join Date: Aug 2007
Posts: 46
 

Default Re: Running Simple Selects on 3rd party table

I should also note you need to make sure to initialize X-Cart 5 by including top.inc.php if you're running this outside of the X-Carts's object model in your own PHP file:

Code:
//X-Cart initializtion require_once 'top.inc.php';
__________________
X-Cart Gold 4.4.5
X-Cart Ultimate 5.2.22
X-Cart Business 5.3.2.13
X-Payments 3
Reply With Quote

The following 2 users thank BurtonTech for this useful post:
qualiteam (05-03-2016), Stormer (05-03-2016)
  #4  
Old 05-03-2016, 03:24 PM
 
Stormer Stormer is offline
 

Member
  
Join Date: May 2016
Posts: 15
 

Default Re: Running Simple Selects on 3rd party table

Thank You! It Works!
__________________
X-Cart 5.3
Reply With Quote
  #5  
Old 05-03-2016, 10:09 PM
  qualiteam's Avatar 
qualiteam qualiteam is offline
 

X-Guru
  
Join Date: Dec 2010
Posts: 6,373
 

Default Re: Running Simple Selects on 3rd party table

You can execute raw MySQL queries like this:
Code:
$tablePrefix = \XLite::getInstance()->getOptions(array('database_details', 'table_prefix')); $query = 'UPDATE ' . $tablePrefix . 'orders SET lastVisitDate = 0 WHERE status = "T"'; \XLite\Core\Database::getEM()->getConnection()->executeQuery($query, array());

You should not use this for X-Cart 5 database tables (and use ORM instead), but it is OK to use this for non-X-Cart-5 tables in the same database.
__________________
Alex Solovev,
Qualiteam

---

User manual Video tutorials X-Cart FAQ

You are welcome to press "Thanks" button
if you find this post useful

Click here to learn how to apply patches

X-Cart Extensions
Reply With Quote
Reply
   X-Cart forums > X-Cart 5 > Dev Questions (X-Cart 5)


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -8. The time now is 03:22 AM.

   

 
X-Cart forums © 2001-2020