dqwh1219 2014-02-06 06:54
浏览 53
已采纳

将数组传递给sql查询无法正常工作[重复]

<?php
require_once("dbdata.php");
if(mysql_connect($server_name,$Db_user,$Db_pass))
 {
if(mysql_select_db($Database_name))
   {
     $display_scores = "SELECT DISTINCT User_id, Quiz_id, Parent_Category,Category_Name, Score FROM custom_question_details where User_id = 3";
     $results = mysql_query($display_scores);
     $scores = array();
     $s_p = array();
     while($rows = mysql_fetch_assoc($results))
     {
        $scores[] = $rows; 
        $s_p [] = $rows['Parent_Category'];
     }
     $imp = implode(',',$s_p);
     $child_sql = "SELECT DISTINCT Category_Name FROM custom_question_details WHERE Parent_Category IN (".$imp.")";
     $child_result = mysql_query($child_sql);
     $child_array = array();
     while($fetch_child_results = mysql_fetch_assoc($child_result))
     {
         $child_array[] = $fetch_child_results;
     }
     print_r($child_array);
   }
 }
?>

I am getting this error

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in line ...

while passing $imp to $child_sql query. But if I give some string like 'Sample' statically which is in db instead of $imp variable i obtain the result. what is the problem in passing $imp variable in passing the query.

</div>
  • 写回答

5条回答 默认 最新

  • dongshuohuan5291 2014-02-06 07:13
    关注

    you must enquote values in IN clause

    try this

    $s_p [] = "'".$rows['Parent_Category']."'";
    

    instead of

    $s_p [] = $rows['Parent_Category'];
    

    now use it

    $imp = implode(',',$s_p);
         $child_sql = "SELECT DISTINCT Category_Name FROM custom_question_details WHERE Parent_Category IN (".$imp.")";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图