0

I have a simple page which basically gets unique input text from user and queries the database to see if that any contact record belonging to that input. So far I know connection is established and it is trying to query the database. However, no record is retrieved. This is what the code looks like:

<form action="processAttendance.php" method="POST">
  <div class="input">
    <input type="text" class="button" id="inviteCode" name="inviteCode" placeholder="Enter invite code i.e. E2562SA">
    <input type="submit" class="button" id="submit" value="Register"> &nbsp;
  </div>
</form>
<?php  
include 'dbh.php';
$inviteCode = $_POST['inviteCode'];
echo $inviteCode;
$sql = "SELECT `FirstName`, `LastName`, `Confirmed`, `InviteCode`, `PlusOnes` FROM `Guest` WHERE 'InviteCode' = '$inviteCode'";
$result = $conn->query($sql);
$row = mysqli_fetch_assoc($result);
echo "my result" . $row."<br>";
echo "my result" . $row['classtype'];

The result printed back is as follows:

TEST

my resultArray

my result

Could anyone offer some insight as to why this could be the case?

AndrewR
  • 6,668
  • 1
  • 24
  • 38
saint
  • 268
  • 1
  • 5
  • 16
  • 1
    **Danger**: You are **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that you need to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin May 18 '17 at 13:54
  • Apparently you do have results, but they are in an array format due to `mysqli_fetch_assoc `, thus getting _"my resultArray"_ , you would need to iterate over your results to echo each row's content. – CodeGodie May 18 '17 at 14:01

4 Answers4

1

Youre not fetching your rows correctly. Youre using mysqli OOP, so you should use ->fetch_assoc() and you need to iterate over your results to get your rows. Something like this:

$sql = "SELECT `FirstName`, `LastName`, `Confirmed`, `InviteCode`, `PlusOnes` FROM `Guest` WHERE 'InviteCode' = '$inviteCode'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {

        echo "<pre>";
        print_r($row);
        echo "</pre>";

        echo "my FirstName: " . $row['FirstName'];
    }
} else {
    echo "No results";
}
$conn->close();

BTW, you should use prepared statements to prevent sql injections.

CodeGodie
  • 12,116
  • 6
  • 37
  • 66
0

However, no record is retrieved

Incorrect. If you didn't get a record that you wouldn't get:

my resultArray

mysqli_fetch_assoc returns NULL if there are no results. You are getting an array.

$row['classtype'] is blank because you didn't include classtype in the list of fields you were selecting.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

Your sql statement is this:

SELECT FirstName, LastName, Confirmed, InviteCode, PlusOnes FROM Guest WHERE 'InviteCode' = '$inviteCode'

You are attempting to output $row['classtype'], but look at the SELECT part of your SQL, that column was not selected.

Use classtype to your sql statement to make it available, assuming that is a field in your table.

SELECT `classtype` FROM `Guest` WHERE 'InviteCode' = '$inviteCode'

After getting $row, you can run the statement var_dump($row) to output the entire object.

AndrewR
  • 6,668
  • 1
  • 24
  • 38
-1

Here is a method I use:

for($row as $data) {

echo $data['classtype'];

}

Try this. I am assuming you will be dealing with the rows of all members you have there. It will go through all the rows with whatever info you want.

abssse
  • 1