Jump to content
maba

Interact with external database (mysql)

Recommended Posts

There isn't a tool like this in PW. But honestly using it doesn't seem much easier than doing normal mysql queries... Is there any advantage?

Share this post


Link to post
Share on other sites

You are right.

But which is the best practice to use mysql for a second connection?

Use mysql_connect() in all interested templates?

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

Thanks fot the answer.

Now I imagine that this solution can be used also for the multisite question.. :)

Yes.. I have made the wrong example telling about mysql_connect :rolleyes:

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...