2016-07-12 63 views
0

我在Joomla中的JDatabase有一些问题。 我下面的一些例子从https://docs.joomla.org/Secure_coding_guidelines#Secure_on_searchJoomla数据库查询通配符

我的代码:

$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'子句是仅仅让条纹的。

我在做什么错?

我想查询'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 } 

到现在为止,一切似乎都正常...

+0

我用不同的字段名称和表,并能够得到结果。你可以用这种方法var_dump查询并在这里输出结果。后续代码var_dump($查询);出口;就在你执行或使用setquery之前。 –

+0

这看起来没问题。你可以在这里或在pastebin.com中添加完整的数据库查询: –

回答

0

好吧,我发现在我的代码别的地方的错误。所以这个查询真的很好,但之后直接出现了一个错误。 Thanx无论如何给它一个尝试...

+0

这不是一个答案。有没有人知道一些代码错误。但是你可以通过这个作为评论。 –

+0

好的,我很抱歉。我对此很新。下次我知道... – user3384516