如何在存储过程中添加where条件作为参数?

时间:2017-03-08 19:19:48

标签: php mysql stored-procedures

我不知道是否可能。当参数id获取值时,它会显示错误//字段列表中的未知表'tbl_news'

for file in listOf Makefiles
for line in Makeifle
find code file (.c or .h)


    try:
        index = lstFilenames.index(strFilename)     # to determine if I have seen this file name yet
        #  Yes the file name is already in list
        lstOfFile = lstProjectNames[index]          # get list of projects that this file is seen in
        try:
            fileIndex = lstOfFile.index(myProject)  # check to see if the project is in the project list
        except:  # project not in list
            lstCount[index] +1                      # add one to count - I am counting the number of projects this file appears
            lstOfProjects.append(myProject)         # append the new project to list
    except:   # file name not found in list
            lstFilenames.append (strFilename)       #  put file in file name list 
            lstCount.append(1)                      # set count to one 
            lstOfProjects.append(myProject)         # add project name to project list

#************************************************************************** End of makefile  
lstProjectNames.append(lstOfProjects)  # add  project list to this list ( this list in indexed with teh file names and the counts)
del lstOfProjects[:]

这是我的存储过程

function select_table_img ($type,$eid)
    {
        if($b==NULL)
        {
            $id=1;
        }
        else
        {
            $id="tbl_news.nid='$eid'";
        }
        echo $sql = "CALL Proc_selectvideo('$type',$id)";//call the stored procedure
        $result = mysql_query($sql)or die(mysql_error());
        return $result;
    }

1 个答案:

答案 0 :(得分:0)

如果我理解,问题是你从php发送的单引号。

  

$ ID = “tbl_news.nid = '$ EID'”;

也许您可以使用IF语句或CASE尝试以不同的方式在存储过程中解决它。

  

$ ID = $ EID;

CREATE DEFINER=`root`@`localhost` PROCEDURE `Proc_selectvideo`(IN `type` VARCHAR(500), IN `id` VARCHAR(500))

DECLARE part_of_query VARCHAR(300) DEFAULT '';

SET part_of_query = CASE 
    WHEN id IS NOT NULL THEN CONCAT(" AND tbl_news.nid='",id,"' ")
    ELSE 'maybe other parameter or rule...'
END;

select * from tbl_news,tbl_video where  tbl_news.nid  = tbl_video.cid and tbl_news.ntype=type part_of_query