doumi4676 2016-05-24 13:43
浏览 26
已采纳

如何选择包含名和姓的所有结果?

Very specific question, how can I make a select from a $_SESSIONthat contains the first, middle and last name, only the first and last name?

Example:

The output from $_SESSION['NET']['USER']['NAME'] is 'Robert Pasha Biceps'

What I really need on a SELECT is ALL that contains 'Robert Biceps'

$pdo = new \PDO('mysql:host=localhost;dbname=workflow_teste', 'root', '');

$pdo->setAttribute(\PDO::ATTR_ERRMODE , \PDO::ERRMODE_EXCEPTION);

$string = "SELECT * FROM foo WHERE fooname LIKE '".$_SESSION['NET']['USER']['NAME']."' ";

$statement = $pdo->prepare($string);
$statement->execute();
$result = $statement->fetchAll();
$statement->closeCursor();

echo '<pre>';
var_dump($result);
die;

The var_dumponly shows the results that have the entire name on it, but some people submit only the first and last name, and I need that results also...

Any ideas?

  • 写回答

3条回答 默认 最新

  • duanjiu3486 2016-05-24 14:15
    关注

    You need to split the name into parts and only use first and last part in your SQL query.

    $name = explode(' ', $_SESSION['NET']['USER']['NAME']);
    $firstname = $name[0];
    $lastname = $name[count($name)-1];
    $string = "
        SELECT *
        FROM foo
        WHERE
            fooname LIKE '".$firstname." % ".$lastname."' OR
            fooname = '".$firstname." ".$lastname."'
    ";
    

    From these rows:

    Robert Pasha Biceps
    Robert Biceps
    Robert Middlename Biceps
    Robert Pasha
    Pasha Biceps
    Robert Pasha Triceps
    Robert Other Biceps
    Roberto Pasha Triceps

    the query with the name "Robert Pasha Biceps" will select

    Robert Pasha Biceps
    Robert Biceps
    Robert Middlename Biceps
    Robert Other Biceps

    Also, you should look up and use prepared statements, to prevent a "Bobby tables situation".

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。