SQL中的SQL选择无法正常工作

时间:2019-07-17 04:48:57

标签: php sql oracle

我有3个来自表单输入的变量,而我在php中有此选择 如果我在输入$itemName中进行搜索,它会根据需要返回给我确切的一项,如果我在$typeName$itemPrice中进行搜索,则会返回整个项列表,这样做不行,我尝试像'%$typeName%'那样做'%$itemPrice'$itemName,但是无论如何它都会返回我的所有列表。有什么问题吗?

$stid = oci_parse($conn, "select title, type, price
                          from items 
                          where title like '%$itemName%' 
                             or type like '$typeName' 
                             or price like '$itemPrice'");

2 个答案:

答案 0 :(得分:0)

与LIKE绑定的通用解决方案是:

<?php
$c = oci_connect("hr", "welcome", "localhost/XE");
$s = oci_parse($c,
    "select city, state_province from locations where city like :bv");
$city = 'South%';
oci_bind_by_name($s, ":bv", $city);
oci_execute($s);
oci_fetch_all($s, $res);
var_dump($res);
?>

您必须使用绑定来避免将变量值连接到SQL字符串中的SQL Injection安全问题。它还有助于提高性能和可伸缩性。

请参见https://www.oracle.com/technetwork/topics/php/underground-php-oracle-manual-098250.html中第168页的“使用LIKE和REGEXP_LIKE子句进行绑定”

答案 1 :(得分:-2)

尝试使用单个qoutation标记

$stid = oci_parse($conn, 'Select title, type, price from items 
                          where title like '%$itemName%' 
                             or type like '$typeName' 
                             or price like '$itemPrice'');
相关问题