像使用字母数字字符串bindParam

时间:2018-11-14 11:28:53

标签: php mysql pdo bindparam

我是使用pdo语句的新手,当我尝试在phpMyadmin sql控制台中设置这样的查询时,效果很好:

SELECT name, number FROM client WHERE number LIKE '%30%'

结果:

|name|number|
|antonio|30857898H|
|andrés|30987454U|

但是,当我使用方法bindParam()或方法bindValue()或类型值如PDO::PARAM_STRPDO::PARAM_INT传递参数时,行为是查询停止在第一个数字中,不要得到其他字符。列都是'char'...

这是我的方法:

function getQueryData($key,$i_query){

    require_once '../queryList.php';//get all the querys
//$key is an array like this $key[0][0] = ':number', $key[0][1] = '%30%'    
    $this->consulta = $this->conInstance->prepare($gQuery[$i_query]);

    foreach ($key as $clave => $valor) {
        $this->consulta->bindParam($key[$clave][0], $key[$clave][1]);
        }       
    $this->consulta->execute();

    $this->consulta->setFetchMode(PDO::FETCH_ASSOC);

    return $this->consulta;
    //Make the bucle on the system
}

在这个社区中提供了很多帮助,这里的人很棒!

2 个答案:

答案 0 :(得分:0)

尝试在

这样的匹配子句中隔离参数内容
[TestFixture]
public class MemoizerTest
{
    [Test]
    public void MemoizationWorksOnFuncs()
    {
        int counter = 0;

        Func<int, int> f = x => counter += x;

        Assert.That(this.Memoized(1, f), Is.EqualTo(1));

        Assert.That(this.Memoized(2, f), Is.EqualTo(3));

        Assert.That(this.Memoized(2, f), Is.EqualTo(3));

        Assert.That(this.Memoized(1, f), Is.EqualTo(1));
    }

    private class MemoizedTest
    {
        private int _counter = 0;

        public int Method(int p)
            => this.Memoized(p, x => { return _counter += x; });
    }

    [Test]
    public void MemoizationWorksOnInstances()
    {
        var obj1 = new MemoizedTest();

        Assert.That(obj1.Method(5), Is.EqualTo(5));
        Assert.That(obj1.Method(4), Is.EqualTo(9));
        Assert.That(obj1.Method(5), Is.EqualTo(5));
        Assert.That(obj1.Method(1), Is.EqualTo(10));
        Assert.That(obj1.Method(4), Is.EqualTo(9));

        obj1 = new MemoizedTest();

        Assert.That(obj1.Method(5), Is.EqualTo(5));
        Assert.That(obj1.Method(4), Is.EqualTo(9));
        Assert.That(obj1.Method(5), Is.EqualTo(5));
        Assert.That(obj1.Method(1), Is.EqualTo(10));
        Assert.That(obj1.Method(4), Is.EqualTo(9));
    }

    [Test]
    [Ignore("This test passes only when compiled in Release mode")]
    public void WeakMemoizationCacheIsCleared()
    {
        var obj1 = new MemoizedTest();

        var r1 = obj1.Method(5);

        MemoizerExtension._weakCache.TryGetValue(obj1, out var cache);

        var weakRefToCache = new WeakReference(cache);

        cache = null;
        GC.Collect(2);

        obj1 = null;

        GC.Collect();
        GC.Collect();

        var msg = weakRefToCache.TrackResurrection;

        Assert.That(weakRefToCache.IsAlive, Is.False, "The weak reference should be dead.");

        Assert.That(r1, Is.EqualTo(5));
    }
}

例如

SELECT name, number FROM client WHERE number LIKE concat('%', '30', '%')

或同等

答案 1 :(得分:0)

使用绑定参数时,您需要使用'%'传递整个字符串

查询:

SELECT name, number FROM client WHERE number LIKE :your_param

代码:

$value = 'some value';
$this->consulta->bindParam('your_param', '%'.  $value . '%');