Jump to content

Recommended Posts

Good morning,

 

I have a script and I'm trying to check whether it contains 1 character and whether it is between A and Z.

 

preg_match() seems to be returning 0 and I'm not sure why.

 

Here is my code:

 

<?php



$conn = mysqli_connect("localhost","root","","gaming") or die("Error: ".mysqli_error());



$qry = "SELECT game_id, release_date, game_desc, game_icon FROM games";



if(isset($_GET['letter']))

{    

    /*if(strlen($_GET['letter']) == 1)

    {

        

    }

    else

    {

        header("Location: ?page=games");

    }*/

    $match = preg_match("/A-Z/", $_GET['letter']);

    echo $match;



    $qry .= " WHERE game_tag = '{$_GET['letter']}'";

}



$sql = mysqli_query($conn, $qry) or die("Error: ".mysqli_error($conn));



$game_output = "";



while($row = mysqli_fetch_assoc($sql))

{        

    $game_output .= "<div class='game_containers'>";

    $game_output .= "<img src='images/game_icons/{$row['game_icon']}' alt='{$row['game_id']}' class='game_icon' />";

    $game_output .= "<h3 class='game_header'>{$row['game_id']}</h3>";

    $game_output .= "<p class='game_desc'>{$row['game_desc']}</p>";

    $game_output .= "<hr />";

    $game_output .= "Release Date: <span class='game_date'>{$row['release_date']}</span>";

    $game_output .= "</div>";

}





mysqli_close($conn);

?>

 

When I echo $match, it always returns 0 even though $_GET['letter'] contains say 'M' or 'C'

 

Any thoughts?

 

Kind regards,

 

L2c.

Link to comment
https://forums.phpfreaks.com/topic/275452-preg_match-returning-0/
Share on other sites

You know I was going to try that but I thought you only had to add the opening and closing brackets when you were looking for more than one type of match. :suicide:

 

Thanks very much for the prompt response also!

 

Kind regards,

 

L2c.

Edited by Love2c0de

Good evening Salathe,

 

That's correct, I've setup a CHAR table field to cater for tihs value. I think my code should work now because I check the length of $_GET['letter'] and make sure it's equal to 1 before performing the preg_match().

 

Here is my completed code:

 

<?php

$conn = mysqli_connect("localhost","root","","gaming") or die("Error: ".mysqli_error());

$qry = "SELECT game_id, release_date, game_desc, game_icon FROM games";

if(isset($_GET['letter']))
{    
    if(strlen($_GET['letter']) == 1)
    {
        if(preg_match("/[A-Z]/", $_GET['letter']) === 1)
        {
            $qry .= " WHERE game_tag = '{$_GET['letter']}'";
        }
        else
        {
            header("Location: ?page=games");
        }
    }
    else
    {
        header("Location: ?page=games");
    }    
}

$sql = mysqli_query($conn, $qry) or die("Error: ".mysqli_error($conn));

$game_output = "";

while($row = mysqli_fetch_assoc($sql))
{        
    $game_output .= "<div class='game_containers'>";
    $game_output .= "<img src='images/game_icons/{$row['game_icon']}' alt='{$row['game_id']}' class='game_icon' />";
    $game_output .= "<h3 class='game_header'>{$row['game_id']}</h3>";
    $game_output .= "<p class='game_desc'>{$row['game_desc']}</p>";
    $game_output .= "<hr />";
    $game_output .= "Release Date: <span class='game_date'>{$row['release_date']}</span>";
    $game_output .= "</div>";
}


mysqli_close($conn);
?>

 

I'm always open to suggestions on how to do things better though. I had the length in mind when I decided to go for regexp but didn't know how to edit the regular expression so I used strlen() instead.

 

Kind regards,

 

L2c.

Edited by Love2c0de

Do the length check in the RegExp as well, to avoid unnecessary code and (not to mention) nesting.

 

Also, I would recommend that you reverse the logic of your test a bit. So that you're checking for error conditions instead. This will not only make it a lot easier to read your code, as your error messages will immediately follow the test which produces them; But it will also help reduce the amount of nesting required, and thus further improve the readability.

Lastly: Always use die after a header redirect.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.