如何从数据库中使用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>

我试图通过它自己访问 firstname code>。 我把下面的代码放在一起: p>

p>

 &lt;?php 
 
 $ sql =“SELECT firstname,lastname 来自客人WHERE选项= $选项“; 
 
 $ result = mysqli_query($ conn,$ sql); 
 
 
 
 
(mysqli_num_rows($ result)&gt; 0){
 
 // // 输出每行的数据
 
 while($ row = mysqli_fetch_assoc($ result)){
 
 echo“You Are”。  $行[“名字”。  “”。  $行[“姓氏”。  “&LT峰; br&gt;” 中; 
 
&GT; 代码>  PRE> 
 
  DIV> 
 
  DIV> 
 
 
 
 

有 给我你是Bob测试。 code>我需要将它们转换为自变量,这样我就可以在任何地方使用它们。 像 $ row [“firstname”] = $ firstname; code>那么我可以 echo $ firstname; code>但是如果我使用 $ row [“它将无法工作 firstname“] = $ firstname; code> p>

我认为问题出在我如何形成结果 $ result = mysqli_query($ conn,$ sql); 我可以在这里说一些其他内容,然后我可以使用say $ row [“firstname”] = $ firstname; code>并使用类似 echo $ firstname; code>? 谢谢。 p> div>

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: