douchun1948 2015-10-09 01:58
浏览 40
已采纳

如何从数据库中使用php自己获取值?

I am trying to access firstname by it self. I have the code below put together:

<?php
$sql = "SELECT firstname, lastname FROM guests WHERE option = $option";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        echo "You Are " . $row["firstname"]. " " . $row["lastname"]. "<br>";
?>

It gives me You are Bob Testing. I need to convert them to self variable so I can use them anywhere. Like $row["firstname"] = $firstname; so then I could echo $firstname; But it won't work if I use $row["firstname"] = $firstname;

I think the issue is somewhere in how I form the result $result = mysqli_query($conn, $sql); Can I say something else here so then I could just use say $row["firstname"] = $firstname; and use like echo $firstname;? Thanks.

</div>
  • 写回答

1条回答 默认 最新

  • dongyun4010 2015-10-09 02:23
    关注

    Firstly, if this is your actual code, it's missing a few closing braces.

    if (mysqli_num_rows($result) > 0) {
        // output data of each row
        while($row = mysqli_fetch_assoc($result)) {
            echo "You Are " . $row["firstname"]. " " . $row["lastname"]. "<br>";
        } // this one was missing
    
    } // as was this one
    

    Now, assign a variable "to" the row(s) and not the other way around.

    if (mysqli_num_rows($result) > 0) {
        // output data of each row
        while($row = mysqli_fetch_assoc($result)) {
    
            $first = $row["firstname"];
            $last = $row["lastname"];
        }
            echo "You are " . $first . " " . $last . "<br>";
    }
    

    However the above will only echo a single row, therefore you will need to place the echo "inside" the while loop in order to echo all the rows in your table:

    if (mysqli_num_rows($result) > 0) {
        // output data of each row
        while($row = mysqli_fetch_assoc($result)) {
    
            $first = $row["firstname"];
            $last = $row["lastname"];
    
            echo "You are " . $first . " " . $last . "<br>";
        }
    
    }
    

    Something about this though WHERE option = $option";

    If $option is a string, it will need to be quoted:

    WHERE option = '$option'";
    

    otherwise, MySQL will see that as a syntax error. Check for errors on your query:

    It will also be prone to an SQL injection, therefore it is best you use a prepared statement.

    Seeing you may be new to working with MySQL, it's best to learn about protecting yourself against SQL injection. Here is a good article about it on Stack:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?