使用通配符的Joomla数据库查询

时间:2016-07-12 17:56:10

标签: database joomla wildcard

我在Joomla中遇到了JDatabase的问题。 我正在关注https://docs.joomla.org/Secure_coding_guidelines#Secure_on_search

中的一些例子

我的代码:

$db = JFactory::getDbo ();
$query = $db->getQuery ( true );

$query->select ( '*' );
$query->from ( $db->quoteName ( '#__someTable' ) );

$search = $db->escape( 'someString', true ) . '%';

$query->where ( $db->quoteName ( 'someField' ) . ' LIKE ' . $db->quote( $search, false ));

这以SQL错误结束:

1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 3 SQL=SELECT * FROM `frb6z_opensourcepage_productscomponent` WHERE

'where'-Clause只是条纹。

我做错了什么?

我想查询'someField'以'someString'开头的行。

在执行查询之前,var_dump和退出查询会给我:

object(JDatabaseQueryMysqli)#956 (24) {
["offset":protected]=> NULL
["limit":protected]=> NULL
["db":protected]=> object(JDatabaseDriverMysqli)#15 (23) {
    ["name"]=> string(6) "mysqli" 
    ["serverType"]=> string(5) "mysql" 
    ["connection":protected]=> object(mysqli)#16 (19) { 
        ["affected_rows"]=> int(1) 
        ["client_info"]=> string(79) "mysqlnd 5.0.12-dev - 20150407 - $Id: fd3bfdab644600c96b25beaf69d021447b7b4ec5 $" 
        ["client_version"]=> int(50012) 
        ["connect_errno"]=> int(0) 
        ["connect_error"]=> NULL 
        ["errno"]=> int(0) 
        ["error"]=> string(0) "" 
        ["error_list"]=> array(0) { } 
        ["field_count"]=> int(0) 
        ["host_info"]=> string(25) "Localhost via UNIX socket" 
        ["info"]=> NULL 
        ["insert_id"]=> int(0) 
        ["server_info"]=> string(21) "5.5.5-10.1.13-MariaDB" 
        ["server_version"]=> int(50505) 
        ["stat"]=> string(142) "Uptime: 685930 Threads: 1 Questions: 1395565 Slow queries: 0 Opens: 11271 Flush tables: 1 Open tables: 64 Queries per second avg: 2.034" 
        ["sqlstate"]=> string(5) "00000" 
        ["protocol_version"]=> int(10) 
        ["thread_id"]=> int(1406) 
        ["warning_count"]=> int(0) } 
    ["nameQuote":protected]=> string(1) "`" 
    ["nullDate":protected]=> string(19) "0000-00-00 00:00:00" 
    ["_database":"JDatabaseDriver":private]=> string(6) "joomla" 
    ["count":protected]=> int(12889) 
    ["cursor":protected]=> bool(true) 
    ["debug":protected]=> bool(false) 
    ["limit":protected]=> int(0) 
    ["log":protected]=> array(0) { } 
    ["timings":protected]=> array(0) { } 
    ["callStacks":protected]=> array(0) { } 
    ["offset":protected]=> int(0) 
    ["options":protected]=> array(9) { 
        ["driver"]=> string(6) "mysqli" 
        ["host"]=> string(9) "localhost" 
        ["user"]=> string(4) "root" 
        ["password"]=> string(0) "" 
        ["database"]=> string(6) "joomla" 
        ["prefix"]=> string(6) "frb6z_" 
        ["select"]=> bool(true) 
        ["port"]=> int(3306) 
        ["socket"]=> NULL } 
    ["sql":protected]=> object(JDatabaseQueryMysqli)#854 (24) { 
        ["offset":protected]=> int(0) 
        ["limit":protected]=> int(0) 
        ["db":protected]=> *RECURSION* 
        ["sql":protected]=> NULL 
        ["type":protected]=> string(6) "insert" 
        ["element":protected]=> NULL 
        ["select":protected]=> NULL 
        ["delete":protected]=> NULL 
        ["update":protected]=> NULL 
        ["insert":protected]=> object(JDatabaseQueryElement)#781 (3) { 
            ["name":protected]=> string(11) "INSERT INTO" 
            ["elements":protected]=> array(1) { 
                [0]=> string(48) "`#__opensourcepage_product_mm_productscomponent`" } 
            ["glue":protected]=> string(1) "," } 
        ["from":protected]=> NULL 
        ["join":protected]=> NULL 
        ["set":protected]=> NULL 
        ["where":protected]=> NULL 
        ["group":protected]=> NULL 
        ["having":protected]=> NULL 
        ["columns":protected]=> object(JDatabaseQueryElement)#937 (3) { 
            ["name":protected]=> string(2) "()" 
            ["elements":protected]=> array(2) { 
                [0]=> string(11) "`uid_local`" 
                [1]=> string(13) "`uid_foreign`" } 
            ["glue":protected]=> string(1) "," } 
        ["values":protected]=> object(JDatabaseQueryElement)#824 (3) { 
            ["name":protected]=> string(2) "()" 
            ["elements":protected]=> array(1) { 
                [0]=> string(11) "'169','248'" } 
            ["glue":protected]=> string(3) "),(" } 
        ["order":protected]=> NULL 
        ["autoIncrementField":protected]=> bool(false) 
        ["call":protected]=> NULL 
        ["exec":protected]=> NULL 
        ["union":protected]=> NULL 
        ["unionAll":protected]=> NULL } 
    ["tablePrefix":protected]=> string(6) "frb6z_" 
    ["utf":protected]=> bool(true) 
    ["utf8mb4":protected]=> bool(true) 
    ["errorNum":protected]=> int(0) 
    ["errorMsg":protected]=> string(0) "" 
    ["transactionDepth":protected]=> int(0) 
    ["disconnectHandlers":protected]=> array(0) { } } 
["sql":protected]=> NULL 
["type":protected]=> string(6) "select" 
["element":protected]=> NULL 
["select":protected]=> object(JDatabaseQueryElement)#839 (3) { 
    ["name":protected]=> string(6) "SELECT" 
    ["elements":protected]=> array(1) { 
        [0]=> string(1) "*" } 
    ["glue":protected]=> string(1) "," } 
["delete":protected]=> NULL 
["update":protected]=> NULL 
["insert":protected]=> NULL 
["from":protected]=> object(JDatabaseQueryElement)#789 (3) { 
    ["name":protected]=> string(4) "FROM" 
    ["elements":protected]=> array(1) { 
        [0]=> string(37) "`#__opensourcepage_productscomponent`" } 
    ["glue":protected]=> string(1) "," } 
["join":protected]=> NULL 
["set":protected]=> NULL 
["where":protected]=> object(JDatabaseQueryElement)#953 (3) { 
    ["name":protected]=> string(5) "WHERE" 
    ["elements":protected]=> array(1) { 
        [0]=> string(31) "`component_id` LIKE 'Product-%'" } 
    ["glue":protected]=> string(5) " AND " } 
["group":protected]=> NULL 
["having":protected]=> NULL 
["columns":protected]=> NULL 
["values":protected]=> NULL 
["order":protected]=> NULL 
["autoIncrementField":protected]=> NULL 
["call":protected]=> NULL 
["exec":protected]=> NULL 
["union":protected]=> NULL 
["unionAll":protected]=> NULL }

到目前为止,一切似乎都很正常......

1 个答案:

答案 0 :(得分:0)

好的,我在其他地方的代码中发现了错误。所以这个查询真的很好,但是在那个问题之后又出现了另一个问题。无论如何,Thanx试一试......