0

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.

Sammy7
  • 364
  • 6
  • 21
  • 1
    Shouldn't you do `$firstname = $row['firstname']` if you're setting a variable? (if that's not what you're trying to do tell me). – kzhao14 Oct 09 '15 at 02:06
  • If you do `$row['firstname'] = $firstname;`, you're setting `$row['firstname']` AS `$firstname`, which I'm guessing isn't what you're trying to do. – kzhao14 Oct 09 '15 at 02:06
  • set a variable for each row's columns in the `while` loop. Do the inverse of what you're trying to do. – Funk Forty Niner Oct 09 '15 at 02:08
  • @k97513 yeah but I didn't realize I had to put it `while($row = mysqli_fetch_assoc($result)) { HERE }` and I was trying to do `$row["firstname"] = $firstname;` but in reality I needed `$firstname = $row['firstname']` – Sammy7 Oct 09 '15 at 13:18

1 Answers1

4

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:

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • Remember multiple results will overwrite it each time so `$first` and `$last` will only ever be the last record extracted from the database. – Doug Oct 09 '15 at 02:29
  • @Doug yes and that's why I made an additional edit, placing the echo inside the `while` loop. I wanted to show the OP the difference between both. – Funk Forty Niner Oct 09 '15 at 02:30
  • Beat me to it! Must have submitted my comment as you made the update – Doug Oct 09 '15 at 02:31
  • Sorry about the confusion. You do realize this is just a snippet of my code. I needed help how to get the `$firstname` by it self. Little did I know you have to put it inside `while($row = mysqli_fetch_assoc($result)) { HERE }` Thanks so much. As for `$option` it's already declared a bit higher up(code I didn't paste.) Thanks you help me a lot. – Sammy7 Oct 09 '15 at 13:15
  • And I didn't need the `loop` because based on the goal I'm trying to achieve here there will only ever be one result for `WHERE option = $option` – Sammy7 Oct 09 '15 at 13:20