Coolen
Coolen

Reputation: 180

Connection to second database in wordpress

I want to connect to an external database in Wordpress for a custom script. But when I run the script it stops after

The connection is good it is tested by the plugin Ari Adminer. This plugin also vallidates the excistence of the tables and fields.

Wordpressversion = 4.9.4

This is part of the script:

<?php
$mydb = new wpdb('user','password','database','localhost');
$urlfoto = $url.'images/logos/';
?>
    <div class="klanten_gallery">
        <div class="container">
            <div class="klanten_gallery_inn">
                <div class="klanten_gallery_header">
                Onze klanten
                <h2><?php the_title(); ?></h2>
                    <div class="border_photo_klanten"></div>
                </div>
                <div class="klanten_gallery_text">
                    <?php
                        if ( have_posts() ) : while ( have_posts() ) : the_post(); 
                            the_content(); 
                        endwhile; endif;
                    ?>
                </div>
                <div class="gal">
                <?php   
    $qry ='
        SELECT kl_logos.id, kl_logos.klnr, kl_logos.image, kl_overzicht.kl_overzicht_naam
        FROM kl_logos
        INNER JOIN kl_overzicht
        ON kl_logos.klnr = kl_overzicht.kl_overzicht_klnr
        ORDER BY RAND()
        LIMIT 18';
    if(!$result = $mydb ->query($qry)) {
        echo ' Error in query: '. $mydb->error; 
    }else while($list = $result->fetch_assoc()){
        echo'
                <a class="fancy6" href="#" title="'.$list['kl_overzicht_naam'].'">
                    <div class="klanten_image">
                    <img src="'.$urlfoto.$list['image'].'" alt="'.$list['kl_overzicht_naam'].'"/>
                    </div>
                </a>';
                }       
    ?>      
                 </div> 
            </div>
        </div>
    </div>

Upvotes: 1

Views: 1213

Answers (2)

habib
habib

Reputation: 11

Hi add this code to function.php use it where you want its make it global

function connect_another_db() {
  global $seconddb;
  define('HOST', "localhost");
  define('USER', "root");
  define('PASS', "");
  define('DATABASE', "wpshop_node_for_post");
 
    $seconddb = new wpdb(USER, PASS, DATABASE, HOST);
}
add_action('init', 'connect_another_db');

Upvotes: 0

Coolen
Coolen

Reputation: 180

I found the answer.

WP does not support $list = $result->fetch_assoc() so I replaced it with foreach ($results as $list)

With the next script as a result, I hope this will help other users.

<?php   
$qry ='
SELECT kl_logos.id, kl_logos.klnr,  kl_logos.image, kl_overzicht.kl_overzicht_naam
FROM kl_logos
INNER JOIN kl_overzicht
ON kl_logos.klnr = kl_overzicht.kl_overzicht_klnr
ORDER BY RAND()
LIMIT 18';
if(!$results = $mydb ->prepare($qry)) {
echo ' Error in query: '. $mydb->error; 
}else{
$results = $mydb->get_results($qry, 'ARRAY_A');
foreach ($results as $list) {
echo'                   
<a class="fancy6" href="#" title="'.$list['kl_overzicht_naam'].'">
    <div class="klanten_image">
    <img src="'.$urlfoto.$list['image'].'" alt="'.$list['kl_overzicht_naam'].'"/>   
    </div>
</a>';
}   
}           
?>  

Upvotes: 1

Related Questions