doujiao8491 2012-11-04 19:49
浏览 56
已采纳

使用mysql LIKE语句

I am trying to create a duplicate username checker and I am thinking this is probably the way to do it correct me if im wrong. Basically I want the username the user input to be stored in a variable called userName and then use that variable to check and see if there are any LIKE rows in the database and if so return a count of 1 or more to a variable named $count I would then have an IF ELSE statement that would either yell at the user or let them continue. I have run into a problem using the LIKE statement. I think my syntax could be wrong since I have been trying several different methods but still no luck.

Main Code

<?php

require 'DB.php';
$userName = "tes";
echo $userName;
try{
$stmt = $conn->prepare('SELECT COUNT(*) FROM `CLL_users` WHERE `user_name` LIKE "% . ":userName" . "');
$stmt->bindValue(':userName', $userName);
$stmt->execute();
$count = $stmt->fetchColumn();
return $count;
echo $count;

} catch (PDOException $e){
    echo 'Connection failed: ' . $e->getMessage();

    }


?>
  • 写回答

3条回答 默认 最新

  • dslpofp041310584 2012-11-04 20:09
    关注

    It appears you're trying to do string concatenation in MySQL using invalid syntax. Try this:

    'SELECT COUNT(*) FROM `CLL_users` WHERE `user_name` LIKE CONCAT("%",:userName)'
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失