Import data from my database

Hi,

I tried many things but I don’t know what to do.
I created all the tables I needed in my suitecrm database and inserted all of my data. That works.

But now I want import the data into my suitecrm and get the relationships but that didn’t work.

I connect with my database:

$servername=“localhost”;
$username="";
$password="";
$dbname="";

$db=new mysqli($servername, $username, $password, $dbname);

if($db->connect_error) {
die("Connection failed: " . $db->connect_error);
}

and when I try to make this:

[…]

$sfrc_query = "SELECT * from {$sfrc_table} ";
$sfrc_result=$db->query($sfrc_query);
while($item=$db->fetchByAssoc($sfrc_result)){

[…]

I just get the error:

PHP Fatal error: Uncaught Error: Call to undefined method mysqli::fetchByAssoc()

and when I comment the first one out I just get:

PHP Fatal error: Uncaught Error: Call to a member function query() on null

Changing the $db to $GLOBALS[“db”] didn’t work.

I’m executing my script in the linux terminal with "php -f ‘script name’ ".

What I have to change? Do I need to execute it somewhere else?

I used the second script from: https://community.sugarcrm.com/thread/19736

It would be nice if someone could help me :slight_smile:

Hi

is this your attempt to connect to the original database (the source of data) or to the SuiteCRM database (the destination)?

If it’s the source, do you have a lot of complex data with relationships etc, or could you be better off just exporting a table to CSV and then importing?

Thank your for your answer.

I already have imported my csv files into my suitcrm database. Now I want to parse the data from the salesforce tables to the respective suitcrm db tables to see them with the relationships in the UI.

There is a lot of data to export from the database to suitcrm.

Here is an example of direct SQL access, in its simplest form:


  if (!defined('sugarEntry') || !sugarEntry) die ('Not a Valid Entry Point!');

  global $db;

  $query = "SELECT lead_source, sales_stage as stage, amount as total, user_name as user
            FROM opportunities
            LEFT JOIN users ON users.id = opportunities.assigned_user_id
            WHERE opportunities.deleted=0
            ";
  $result=$db->query($query,true);
  while( $row = $db->fetchByAssoc($result)) {
    echo $row['stage'], ": ", $row['total'], '<br/>';
  }

Please try to grab simple data from your input tables with this code, and see if it works.

Make sure you register your PHP file as an entrypoint first:
https://docs.suitecrm.com/developer/entry-points/#_creating_an_entry_point

Do I need to execute my php scripts like an entry point?
Or how do I execute the scripts?

Since you’re using SuiteCRM classes and code, if you don’t register the entrypoint your code will be excluded for security reasons.

It’s easy - just register the entrypoint, and your code will run “inside” SuiteCRM, then you can use all the power of Beans etc for your import.

1 Like

Okey, I’ll try it with my code. Thank you Sir!