【问题标题】:Using PHP data in MySQL [closed]在 MySQL 中使用 PHP 数据 [关闭]
【发布时间】:2012-02-04 20:10:06
【问题描述】:

我想使用 PHP 中的数据在数据库中搜索数据

例如:

<?php
$con = mysql_connect("localhost", "root");
if (!$con){
    die('Could not connect: ' . mysql_error());
}
mysql_select_db("UsersDB", $con);
$name = "Jack"
$result = mysql_query("SELECT Username
    FROM Users
    WHERE Username = $name", $con);
echo $result;
mysql_close($con);
?>    

但这不起作用。

是否可以在select语句中使用$name

【问题讨论】:

  • “那行不通”几乎不是一个分析问题的描述。
  • 我感觉对 PHP 如何与 MySQL 一起工作缺乏了解,请查看 tizag.com/mysqlTutorial/mysqlquery.php 和那里的其他页面,了解如何查询、插入、排序甚至设置 MySQL 数据库的简单分解。
  • 您应该知道,如果 $name 可以由用户通过表单或您所拥有的东西指定,那么直接将 $name 插入到您的查询中会使您的数据库受到 SQL 注入攻击。您应该使用像这样的参数构造查询stackoverflow.com/questions/60174/…

标签: php mysql sql database


【解决方案1】:
  • 您在定义 $name 后忘记了分号(打开错误报告以查看解析错误)。
  • 您没有在 SQL 查询中分隔名称。
  • echo $result 没有做任何有用的事情。
  • 是时候阅读一本经过同行评审的 PHP 书籍了!

【讨论】:

    【解决方案2】:

    用户名是一个 VARCHAR/字符串,因此需要引用您要搜索的用户名值:

    $result = mysql_query("SELECT Username 
                   FROM Users 
                   WHERE Username = '$name'", $con);
    

    其他人指出了您代码中的其他错误

    【讨论】:

      【解决方案3】:

      回应结果资源可能不是您打算做的。您需要使用提供的函数mysql_result() 从结果集中读取数据。仔细看http://php.net/manual/en/function.mysql-result.php的例子:

      $link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
      if (!$link) {
          die('Could not connect: ' . mysql_error());
      }
      if (!mysql_select_db('database_name')) {
          die('Could not select database: ' . mysql_error());
      }
      $result = mysql_query('SELECT name FROM work.employee');
      if (!$result) {
          die('Could not query:' . mysql_error());
      }
      echo mysql_result($result, 2); // outputs third employee's name
      
      mysql_close($link);
      

      另请参阅 mysql_fetch_row()mysql_fetch_array()

      【讨论】:

        【解决方案4】:

        您的主要问题是您正在回显$result,这是一个 MySQL 结果,而不是数据。

        read this

        你应该这样做:

        $sql = "Select * from `table`";
        
        // If you will have many results
        if ($result = mysql_query($sql)) {
          while ($record = mysql_fetch_array($result)) {
            // Do stuff for each record
            }
          }
        
        // If you will have ONE result
        if ($result = mysql_query($sql)) {
          if ($record = mysql_fetch_array($result)) {
            // Do stuff for this record
            }
          }
        

        注意

        将 $name 直接转储到查询中是非常糟糕的做法,听说过SQL injection

        正确的做法是,特别是如果您不知道 $name 的值(即它来自表单输入):

        $sql = "SELECT Username 
                FROM Users 
                WHERE Username = '".mysql_real_escape_string($name)."'";
        

        【讨论】:

          【解决方案5】:

          您必须调用mysql_fetch_row()mysql_fetch_assoc() 方法来检索结果。

          $name = "Jack";
          $result = mysql_query("SELECT Username
                         FROM Users
                         WHERE Username = '$name'", $con);
          if($result)
          {
             $row=mysql_fetch_assoc($result);
             if($row)
             {
             }
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2014-04-07
            • 2015-11-08
            • 2020-12-24
            • 1970-01-01
            • 2018-01-25
            • 1970-01-01
            • 1970-01-01
            • 2020-05-16
            相关资源
            最近更新 更多