dongwei4103 2013-09-30 08:19
浏览 45

如何在wordpress $ wpdb中使用mysqli_fetch_array

can someone help me convert this to wordpress friendly code.

basically, im creating a custom page where i want to dispaly json_encode arrays. based on the member ID input.

so if i append id=3 on my custom wordpress page. i want to display the associated name for that id.

when i run below code and type this on my browser http://localhost/site/?page_id=43&id=3 i only get this on my page {"result":[]}

this is my code. i just need to know how to make mysqli_fetch_array work in wordpress.

/*
Template Name: getID
*/

global $wpdb;
if (isset($_GET['id']) === true && empty($_GET['id']) === false) {      
    $query = $wpdb->get_results("SELECT * FROM `memberid` WHERE `memberid` = `$_GET['id']`");

   $array = array();

   while ($row = mysqli_fetch_array($query)) {
    array_push($array, array('name' => $row [2]);
   }

   echo json_encode(array("result" => $array));
}
  • 写回答

1条回答 默认 最新

  • duanliushua5026 2013-09-30 09:02
    关注

    i think i managed to figure this out. since i wanted to retrieve the numerical Array.

    i need to add ARRAY_N on my query

    $query = $wpdb->get_results("SELECT * FROM `table`, ARRAY_N );
    

    and just found out, that while loop won't work. so i used foreach

    foreach ($query as $row) {
            array_push($array, array('name' => $row[2]);
        }
    echo json_encode(array("result" => $array));
    
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法