我正在尝试使用mysql LIKE从数据库中获取结果但是在wordpress中它不起作用的是我正在尝试的代码
//this is what i am putting in where clause.
$state = $_POST['state'];
//table name.
$table_name = $wpdb->prefix . 'userprofile';
//trying but this is returning empty
$q = 'SELECT * FROM ' . $table_name . 'WHERE state LIKE \'%' . esc_sql( like_escape( $state ) ) . '%\'';
echo $q;
$result = $wpdb->get_results($q);
if (empty($result)) {
echo "the result is empty";
}
//returns empty array.
print_r($result);
答案 0 :(得分:1)
就像Marc B.说的那样,有一些缺失的引号和不必要的引用..改变你的查询行,对此:
<?php
trait AbstractTrait
{
public function concreteMethod()
{
return $this->abstractMethod();
}
public abstract function abstractMethod();
}
class TraitClassTest extends PHPUnit_Framework_TestCase
{
public function testConcreteMethod()
{
$mock = $this->getMockForTrait('AbstractTrait');
$mock->expects($this->any())
->method('abstractMethod')
->will($this->returnValue(TRUE));
$this->assertTrue($mock->concreteMethod());
}
}
?>
和你的POST行:
$q = "SELECT * FROM $table_name
WHERE state LIKE '%". esc_sql( like_escape( $state ) ) . "%'
AND WHERE city LIKE '%". esc_sql( like_escape( $city ) ) . "%'
AND WHERE session LIKE '%". esc_sql( like_escape( $session ) ) . "%'
OR WHERE another LIKE '%". esc_sql( like_escape( $another ) ) . "%' ";
答案 1 :(得分:1)
你错过了一个空格:
$q = 'SELECT * FROM ' . $table_name . 'WHERE[..snip..]
^---here
表示你正在制作
SELECT * FROM whateveruserprofileWHERE
这是无效的SQL。
答案 2 :(得分:0)
这样做
$ state = esc_sql($ state);
$ state = like_escape($ state);
$ state =&#39;%&#39; 。 $ state。 &#39;%&#39 ;;
$ q =&#39; SELECT * FROM&#39; 。 $ table_name。 &#39; WHERE状态LIKE&#39; $ state&#39;;