Php 我可以使用SUBSTR从mysqli数据库中选择数据来优化查询吗
我正在尝试使用优化工具在我的网站上搜索。我一直坚持按起始字母搜索。例如,我可以使用通配符“%X%”,但his将返回任何包含字母“X”的内容。 我在一些网站上看到子字符串可以用于mysql查询 这是我到目前为止得到的,但没有返回任何内容。数据库中有应随查询返回的数据Php 我可以使用SUBSTR从mysqli数据库中选择数据来优化查询吗,php,sql,pdo,mysqli,substring,Php,Sql,Pdo,Mysqli,Substring,我正在尝试使用优化工具在我的网站上搜索。我一直坚持按起始字母搜索。例如,我可以使用通配符“%X%”,但his将返回任何包含字母“X”的内容。 我在一些网站上看到子字符串可以用于mysql查询 这是我到目前为止得到的,但没有返回任何内容。数据库中有应随查询返回的数据 public function refineUsersFollowers($user_id,$q){ if($this->databaseConnection()){ // get the user
public function refineUsersFollowers($user_id,$q){
if($this->databaseConnection()){
// get the users followers
$state = array(1,2);
$stmt = $this->db_connection->prepare("SELECT * FROM friends WHERE id_2 = :1 AND Friend_Request_State = :2 OR id_2 = :3 AND Friend_Request_State = :4");
$stmt->bindParam(':1', $user_id);
$stmt->bindParam(':2', $state[0]);
$stmt->bindParam(':3', $user_id);
$stmt->bindParam(':4', $state[1]);
$stmt->execute();
// format the SQL OR statements
$sql = '';
$ids = [];
while($rows = $stmt->fetch(\PDO::FETCH_ASSOC)){
array_push($ids,$rows['id_1']);
}
for($x = 0; $x < count($ids); $x++){
if(count($ids) == 1){
//if there is one result
$sql.= ' user_id = :'.$x." AND SUBSTRING('first_name',0,1) = :".$x.$x;
}else if($x == (count($ids) - 1)){
// last entry
$sql.= ' user_id = :'.$x." AND SUBSTRING('first_name',0,1) = :".$x.$x;
}else{
//continue loop
$sql.= ' user_id = :'.$x." AND SUBSTRING('first_name',0,1) = :".$x.$x." OR";
}
}
$stmt = $this->db_connection->prepare("SELECT * FROM account WHERE ".$sql);
for($x = 0; $x < count($ids); $x++){
$stmt->bindParam(':'.$x,$ids[$x]);
$insert = $x.$x.'';
$stmt->bindParam(':'.$insert,$q);
}
$stmt->execute();
$results = $stmt->fetch(\PDO::FETCH_ASSOC);
print_r($results);
// check for followers that start with letter
}
}
公共函数细化用户权限($user\u id,$q){
如果($this->databaseConnection()){
//获取用户的关注者
$state=数组(1,2);
$stmt=$this->db\u connection->prepare(“从好友中选择*,其中id\u 2=:1和好友请求状态=:2或id\u 2=:3和好友请求状态=:4”);
$stmt->bindParam(':1',$user\u id);
$stmt->bindParam(':2',$state[0]);
$stmt->bindParam(':3',$user\u id);
$stmt->bindParam(':4',$state[1]);
$stmt->execute();
//格式化SQL或SQL语句
$sql='';
$ids=[];
而($rows=$stmt->fetch(\PDO::fetch\u ASSOC)){
数组推送($ids,$rows['id_1']);
}
对于($x=0;$xdb\u connection->prepare(“从“$sql”所在的帐户中选择*);
对于($x=0;$xbindParam(':'.$x,$id[$x]);
$insert=$x.$x.';
$stmt->bindParam(“:”.$insert,$q);
}
$stmt->execute();
$results=$stmt->fetch(\PDO::fetch\u ASSOC);
打印(结果);
//检查以字母开头的跟随者
}
}
函数的第一部分很好,它获取一个id数组,然后将其作为SQL字符串放在一起。SQL是否不返回结果,因为子字符串不支持这种方式?
如果是这样的话,有没有一种方法可以生成这样的查询,或者从数据库中提取每个结果,然后在不同的函数中检查它们会更容易呢?使用运算符可以大大简化查询。这:
"AND SUBSTRING('first_name',0,1) = :".$x.$x;
可以变成这样:
"AND first_name LIKE '".$x.$x."%'";
我不确定
$x.$x
是做什么用的,所以我把它留在这里只是为了说明一下。这个表达式有两个问题:
SUBSTRING('first_name', 0, 1) = :".$x.$x;
首先,SQL中的substr()
(通常)以1开始计数,而不是以0开始计数。所以,第一个参数应该是1
第二,第一个参数是单引号。因此,这最多只能返回字母'f'
。这里有一个简单的规则:只对字符串和日期常量使用单引号。切勿使用单引号引用列名
有几种方法可以写出你想要的东西。以下是三点:
SUBSTRING(first_name, 1, 1) = $x
LEFT(first_name, 1) = $x
first_name like '$x%'
不喜欢“X%”
按起始字母搜索吗?是的,但不知道。这将使我的代码更加简单。您可以更正最后一个子字符串,将1作为第一个参数,如开头所述吗