Click here to Skip to main content
15,888,288 members
Home / Discussions / Web Development
   

Web Development

 
AnswerRe: App de películas Pin
Richard MacCutchan26-Aug-19 3:44
mveRichard MacCutchan26-Aug-19 3:44 
AnswerRe: App de películas Pin
ZurdoDev16-Sep-19 3:56
professionalZurdoDev16-Sep-19 3:56 
QuestionMessage Removed Pin
22-Aug-19 2:15
alicia1990jones22-Aug-19 2:15 
AnswerMessage Removed Pin
22-Aug-19 2:27
gitgit622-Aug-19 2:27 
QuestionBackgroungWork Pin
KGr2820-Aug-19 6:08
KGr2820-Aug-19 6:08 
Rant[REPOST] BackgroungWork Pin
Richard Deeming20-Aug-19 6:18
mveRichard Deeming20-Aug-19 6:18 
GeneralRe: [REPOST] BackgroungWork Pin
KGr2820-Aug-19 7:17
KGr2820-Aug-19 7:17 
QuestionRest API vs wpdb to connect to a separate database on one server Pin
Martin Kaspar13-Aug-19 7:28
Martin Kaspar13-Aug-19 7:28 
hello dear experts, good day, 


i want to connect two pages  on which a plugin like

- participants_database or 
- wp-job-manager runs 


i want to do that via wpdb to another database :: how to combine two wp-pages - here i want to discuss a approach that works  - see https://wordpress.stackexchange.com/questions/1604/using-wpdb-to-connect-to-a-separate-database.  There are serveral options - to do that. 
- use Rest-API 
- user other methods such als the following one which i want to discuss with you 


If they are all on the same server under the same hosting account,  that is helpful. In a case like that, you could write your custom code to access the other site’s database directly. This is discussed here, and it’s quite feasible:  link: Using wpdb to connect to a separate database

see here the discussion of several approaches - which is so fruitful that i want to share it with you... - see https://wordpress.stackexchange.com/questions/1604/using-wpdb-to-connect-to-a-separate-database


[b]question[/b]: I want to connect wpdb to another database. How do I create the instance and pass it the database name/username/password?

Answer1: 134 votes  Yes it's possible. The wpdb object can be used to access any database and query any table. Absolutely no need to be Wordpress related, which is very interesting. The benefit is the ability to use all the wpdb classes and functions like get_results, etc so that there's no need to re-invent the wheel.  Here's how:



[CODE]

$mydb = new wpdb('username','password','database','localhost');

$rows = $mydb->get_results("select Name from my_table");

echo "<ul>";

foreach ($rows as $obj) :

   echo "<li>".$obj->Name."</li>";

endforeach;

echo "</ul>";[/CODE]

comment 1: you can also save time by using global $wpdb. But before firing ;[/CODE]$wpdb->get_results method,;[/CODE]

you must include wp-load.php as: [CODE]require_once('/your/wordpress/wp-load.php');[/CODE]

comment 2: Set WPDB prefix to make WP_Query and get_post to generate correct sql query by calling $mydb->set_prefix('wp_');



answer2: [ 30 votes ] Connecting to a second database is easy in WordPress, you simply create a new instance of the WPDB class and use it the same way you would use the standard $wpdb instance we all know and love.

Assuming the second database has the same login information as the main WP one you can even use the predefined constants

from wp-config.php to avoid hardcoding the login information.

[CODE]/**

 * Instantiate the wpdb class to connect to your second database, $database_name

 */

$second_db = new wpdb(DB_USER, DB_PASSWORD, $database_name, DB_HOST);

/**

 * Use the new database object just like you would use $wpdb

 */

$results = $second_db->get_results($your_query);[/CODE]



comment 1: This is somewhat redundant to Wadih's answer but I think my code example is a bit clearer and its also important to remember the db login constant's as they are almost always the right ones to use and otherwise you risk issues when moving from dev->stage->live environments where the login details might change. – jerclarke Sep 11 '10 at 20:55

comment 2: Set WPDB prefix to make WP_Query and get_post to generate correct sql query by calling [CODE]$second_db->set_prefix('wp_'); [/CODE]



answer3: [ 21 votes ] no one has said this so I thought I'd add an even easier way..as long as your additional database has the same user/pass details to access it as your wordpress database you can use the database name before the table name like this

[CODE]$query = $wpdb->prepare('SELECT * FROM dbname.dbtable WHERE 1');

$result = $wpdb->get_results($query);[/CODE]

comment 1: From my experience, this only works to get data, i.e. using SELECT. You can't insert data.

comment 2: it will not work externally,

  - see https://wordpress.stackexchange.com/questions/1604/using-wpdb-to-connect-to-a-separate-database


[b]Conclusio[/b]: well i like this approach very very much.  The good thing - i am with the sites in question - i am on the same server - so i guess that this will work for me.



see the discussion of several approaches - which is so fruitful that i want to share it with you... - see https://wordpress.stackexchange.com/questions/1604/using-wpdb-to-connect-to-a-separate-database


any idea any experience !? 

love to hear from you

QuestionUnity source version control experiences? Pin
Member 1455222313-Aug-19 5:07
Member 1455222313-Aug-19 5:07 
AnswerRe: Unity source version control experiences? Pin
ZurdoDev16-Sep-19 3:58
professionalZurdoDev16-Sep-19 3:58 
QuestionSignalR Connection Problem - Version Mismatch Pin
Kevin Marois8-Aug-19 7:58
professionalKevin Marois8-Aug-19 7:58 
AnswerRe: SignalR Connection Problem - Version Mismatch Pin
Member 1291556124-Sep-20 13:19
Member 1291556124-Sep-20 13:19 
QuestionAsp.Net Web API Installer Pin
Kevin Marois5-Aug-19 5:52
professionalKevin Marois5-Aug-19 5:52 
QuestionCan someone help me understand this error? - HTTP Error 403.14 - Forbidden Pin
GungaDin161-Aug-19 6:45
GungaDin161-Aug-19 6:45 
AnswerRe: Can someone help me understand this error? - HTTP Error 403.14 - Forbidden Pin
ZurdoDev1-Aug-19 8:07
professionalZurdoDev1-Aug-19 8:07 
AnswerRe: Can someone help me understand this error? - HTTP Error 403.14 - Forbidden Pin
Regina Hawk13-Aug-19 4:31
Regina Hawk13-Aug-19 4:31 
QuestionI keep getting error $(...).fileupload is not a function. Pin
KGr2830-Jul-19 4:38
KGr2830-Jul-19 4:38 
AnswerRe: I keep getting error $(...).fileupload is not a function. Pin
Afzaal Ahmad Zeeshan30-Jul-19 5:41
professionalAfzaal Ahmad Zeeshan30-Jul-19 5:41 
GeneralRe: I keep getting error $(...).fileupload is not a function. Pin
KGr2830-Jul-19 7:35
KGr2830-Jul-19 7:35 
GeneralRe: I keep getting error $(...).fileupload is not a function. Pin
Afzaal Ahmad Zeeshan30-Jul-19 8:55
professionalAfzaal Ahmad Zeeshan30-Jul-19 8:55 
GeneralRe: I keep getting error $(...).fileupload is not a function. Pin
KGr2830-Jul-19 9:12
KGr2830-Jul-19 9:12 
GeneralRe: I keep getting error $(...).fileupload is not a function. Pin
Afzaal Ahmad Zeeshan30-Jul-19 12:28
professionalAfzaal Ahmad Zeeshan30-Jul-19 12:28 
GeneralRe: I keep getting error $(...).fileupload is not a function. Pin
KGr2831-Jul-19 3:38
KGr2831-Jul-19 3:38 
GeneralRe: I keep getting error $(...).fileupload is not a function. Pin
Afzaal Ahmad Zeeshan31-Jul-19 8:17
professionalAfzaal Ahmad Zeeshan31-Jul-19 8:17 
GeneralRe: I keep getting error $(...).fileupload is not a function. Pin
KGr2831-Jul-19 8:19
KGr2831-Jul-19 8:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.