Love2c0de Posted March 10, 2013 Share Posted March 10, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/275452-preg_match-returning-0/ Share on other sites More sharing options...
Solution trq Posted March 10, 2013 Solution Share Posted March 10, 2013 $match = preg_match("/[A-Z]/", $_GET['letter']); Quote Link to comment https://forums.phpfreaks.com/topic/275452-preg_match-returning-0/#findComment-1417787 Share on other sites More sharing options...
Love2c0de Posted March 10, 2013 Author Share Posted March 10, 2013 (edited) 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. Thanks very much for the prompt response also! Kind regards, L2c. Edited March 10, 2013 by Love2c0de Quote Link to comment https://forums.phpfreaks.com/topic/275452-preg_match-returning-0/#findComment-1417789 Share on other sites More sharing options...
salathe Posted March 10, 2013 Share Posted March 10, 2013 Tony's answer doesn't limit the value to being only one letter. Is that what you want? Quote Link to comment https://forums.phpfreaks.com/topic/275452-preg_match-returning-0/#findComment-1417841 Share on other sites More sharing options...
Love2c0de Posted March 10, 2013 Author Share Posted March 10, 2013 (edited) 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 March 10, 2013 by Love2c0de Quote Link to comment https://forums.phpfreaks.com/topic/275452-preg_match-returning-0/#findComment-1417846 Share on other sites More sharing options...
Christian F. Posted March 14, 2013 Share Posted March 14, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/275452-preg_match-returning-0/#findComment-1418565 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.