douanye8442 2015-06-22 01:30
浏览 9
已采纳

PHP(mysqli)找到单值

I know how to search using mysqli to find if there is at least one match. How do I go about retrieving another value from a found row. It must just require changing 1 or 2 things.

Imagine I have the following DB:

 ID     |     emailaddress     |    password
 1      |     dummy@email.com  |    DUMB1PASS
 2      |     second@email.com |    DUMB2Pass

I can use the following code to verify if the the email address "dummy@email.com" exists. How would I look up the password associated with that row (i.e. the row containing dummy@email.com).

$email = "dummy@email.com";
$servername = "correct"; $username = "correct"; $DBpass = "correct"; $dbname = "correct";
    $conn = new mysqli($servername, $username, $DBpass, $dbname);
    if ($conn->connect_error)
        {
            die("Connection failed: " . $conn->connect_error);
        } 

    $sql = "SELECT emailaddress FROM registration WHERE emailaddress = '$email'";
    $result = $conn->query($sql);

    if ($result->num_rows > 0)
        {
            echo "we found a match";
        } 
    else
        {
            echo "we did not find a match";
        }

I imagine I can do something like:

$email = "dummy@email.com";
$servername = "correct"; $username = "correct"; $DBpass = "correct"; $dbname = "correct";
    $conn = new mysqli($servername, $username, $DBpass, $dbname);
    if ($conn->connect_error)
        {
            die("Connection failed: " . $conn->connect_error);
        } 

    $sql = "SELECT password FROM registration WHERE emailaddress = '$email'";
    $result = $conn->query($sql);

    if ($result->num_rows > 0)
        {
            echo "the value is '$result'";
        } 
    else
        {
            echo "we did not find a match";
        }

However, it produces this error:

Catchable fatal error: Object of class mysqli_result could not be converted to string in MYPAGE on line XX.

I think that the cause of this is likely that $result is an array or something. However, I don't know enough about sql/php to know if that is the problem, or how to pull the result from it if it is the case.

I'd really appreciate any help.

  • 写回答

1条回答 默认 最新

  • duanbi1983 2015-06-22 01:32
    关注

    You need to fetch the row first, try this:

    if ($result->num_rows) {
      $row = $result->fetch_row();
      echo 'the value is: ', $row[0];
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录