dtu72460
2017-11-10 19:38
浏览 54
已采纳

Wordpress wpdb没有从数据库获得结果

Hi guys I'm sure I'm missing something but I can't figure out why this is not working! So I have a table and some data, and I'm trying to get the image column, but for some reason, it's not working.

This is what I'm getting (empty url):

<div class="forma_reservas_datos_imagen imgPack" style="background-image:url()"></div>

PHP

<?php 

global $wpdb;

$tablePacks = 'packs';

$res =  "SELECT * FROM ".$tablePacks." where nom_pack_get = '".$_GET["pack"]."'";

$packImg = $wpdb->get_results($res, ARRAY_A);

if(count($packImg) == 1){
    ?>
    <div class="forma_reservas_datos_imagen imgPack" style="background-image:url(<?php echo $packImg->imatge_url; ?>)">
<?php   

}

?>

图片转代码服务由CSDN问答提供 功能建议

嗨,伙计们,我确定我错过了一些东西,但我无法弄清楚为什么这不起作用! 所以我有一个表和一些数据,我正在尝试获取图像列,但由于某种原因,它不起作用。

这就是我得到的(空网址) ):

 &lt; div class =“forma_reservas_datos_imagen imgPack”style =“background-image:url()”&gt;&lt;  / div&gt; 
   
 
 

PHP

 &lt;?php \  n 
global $ wpdb; 
 
 $ tablePacks ='packs'; 
 
 $ res =“SELECT * FROM”。$ tablePacks。“where nom_pack_get ='”。$ _ GET [“pack”]。“'  “; 
 
 $ packImg = $ wpdb-&gt; get_results($ res,ARRAY_A); 
 
if(count($ packImg)== 1){
?&gt; 
&lt; div class =”  forma_reservas_datos_imagen imgPack“style =”background-image:url(&lt;?php echo $ packImg-&gt; imatge_url;?&gt;)“&gt; 
&lt;?php 
 
} 
 
?&gt; 
    
 
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dqz13288 2017-11-10 20:34
    已采纳

    $packImg will be an array of associative arrays, no an object, so you should use it as $packImg[0]["imatge_url"].

    As others have pointed out in the comments, watch out for SQL injections. Use wpdb's prepare method:

    $res = $wpdb->prepare("SELECT * FROM ".$tablePacks." where nom_pack_get = %s", $_GET["pack"]);
    

    prepare will return SQL that is safe to send to the database (and you don't have to type '" . $myvar . "' all the time, it really helps a lot, and makes your code secure at the same time).

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题