-1

I'm starting a small online site which has content in an "issue". Here is my code to collect that:

<?php
$issue_sql = "SELECT * FROM issues WHERE id = '$id'"; // From URL
$issue_res = mysqli_query($con, $issue_sql);
while($issue = mysqli_fetch_assoc($issue_res)){

    $issue_id = $issue["id"];
    $issue_added = $issue["added"];
    $issue_content = $issue["content"];
    $issue_endorsed = $issue["endorsed"];

    $issue_year = date("Y", strtotime($issue_added));
    $issue_date = date("l jS F, Y", strtotime($issue_added));

}
?>

And I want to display the endorsement only if it exists (Which in this case it does). So my code for this is:

<?php
if($issue_endorsed === 1){
    $endorsed_sql = "SELECT article FROM issue_endorsed WHERE id = '$issue_id'";
    $endorsed_res = mysqli_query($con, $endorsed_sql);
    while($endorsed = mysqli_fetch_assoc($endorsed_res)){

        $endorsed_article = $endorsed["article"];

        $display_endorsed = "

            <div class=\"section\">
                <div class=\"sectionText\">
                    <b>We Recommend ...</b>
                    <br>
                    ... $endorsed_article
                </div>
            </div>
            <div class=\"contentDivideSecondary\"></div>

        ";

    }
}else{
    $display_endorsed = "Not Here";
};
?>

The problem is, when I <?php echo display_endorsed; ?> later on in the html code, I'm shown the Not Here instead of the actual content. Can anyone see where I'm going wrong?

19
  • 2
    You're vulnerable to SQL injection. Use prepared statements with mysqli or PDO Commented Jun 28, 2017 at 18:00
  • Are you using register globals? Commented Jun 28, 2017 at 18:00
  • 1
    Is your $issue_endorsed variable a integer or a string? Commented Jun 28, 2017 at 18:03
  • 1
    but as an integer or a string? It matters if you use ===, use var_dump to tell Commented Jun 28, 2017 at 18:05
  • 1
    @AdamForbis Well, I changed it from === to == and it's now working fine, so I guess there was an issue in there somewhere. Thanks Commented Jun 28, 2017 at 18:08

1 Answer 1

1

The issue that you where having has to do with one main thing. MySQL was likely returning the database value of the column endorsed as a string. Now your if statement:

if($issue_endorsed === 1){

is doing an identity comparison. Saying is $issue_endorsed identical to an integer of 1? This was not true because the database was returning it as a string. Switching this to == fixed the issue because it type juggles the string "1" into the integer 1 before the comparison.

The main points to remember out of an issue like this are

  1. PHP is not a typeless language, it is a dynamic one. There are still types, they are just really easy to swap between most of the time.
  2. It is normally a safe to assume that a database returns everything as a string(Certain database options won't though).
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the explaination Adam

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.