Jump to content

Interact with external database (mysql)


maba
 Share

Recommended Posts

If you want to access another MySQL connection, you have any of the native PHP options (mysqli, PDO) available at your disposal. You could also use ProcessWire's Database class (derived from mysqli) to start another connection. Lastly, you could include some other library like to use ActiveRecord from Code Igniter, for example. Personally, I would just keep it simple and use Database, mysqli or PDO. But I would not use mysql_connect, because the old mysql driver in PHP is deprecated... you can use it, but your code might break in some future version of PHP.

If you want to use PW's Database class, here's an example of how you'd do it:

$mydb = new Database('localhost', 'user', 'pass', 'db_name'); 
$result = $mydb->query("SELECT * FROM some_table"); 
while($row = $result->fetch_assoc()) {
 print_r($row); 
}

See PHP's mysqli functions, as the Database class essentially is identical to mysqli with some extra PW-specific stuff thrown in that won't matter if you are interacting with a non-PW database.

To use native mysqli, you'd just replace "new Database(...)" with "new mysqli(...)".

  • Like 4
Link to comment
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...