我想在Wordpress中连接到外部数据库以获取自定义脚本。 但是当我运行脚本时它会在
之后停止连接很好,它由插件Ari Adminer测试。这个插件还可以解决表和字段的缺失问题。
Wordpressversion = 4.9.4
这是脚本的一部分:
<?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>
答案 0 :(得分:1)
我找到了答案。
WP不支持$ list = $ result-&gt; fetch_assoc()所以我用foreach替换它($ results as $ list)
结果是下一个脚本,我希望这可以帮助其他用户。
<?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>';
}
}
?>
答案 1 :(得分:0)
您好,将此代码添加到function.php中,在需要使其全局化的地方使用它
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');