0

Okay, so i want this code snippet to check if variable $_GET['p'] exists in the DB, if it does then make $p = $_GET['p'], if it doesn't exist then make $p = '1', and if $_GET['p'] isn't even set in the link then just display page with id of 0 ($p = '0';). Here is my code. It only shows the "unknown page" when variable is set in the link.

if (isset($_GET['p']))
{
    $getpage = $_GET['p'];
    $sql     = sprintf("SELECT * FROM pages WHERE id=%d LIMIT 1", $getpage);
    $result  = $con->query($sql);
    if ($result && $result->mum_rows > 0 ){ 
        // if page id exists in "pages" table, then make $p = $_GET['p'].
        $p = $getpage;
    }
    else
    { 
        // if "p" ID doesn't exist in DB, then show "unknown page" page with id of "1".
        $p = '1';
    }
}
else if (!isset ($_GET['p'])) 
{
    //if variable "p" isn't set in link then display homepage or page with ID of 0.
    $p = '0';
}
hakre
  • 193,403
  • 52
  • 435
  • 836
  • And what is the issue that you have with the above code? Does it output any errors? – Amal Murali Nov 03 '13 at 08:30
  • 1
    [Little Bobby Tables](http://xkcd.com/327/) will love you. Here's how to keep him away http://bobby-tables.com/php.html – vascowhite Nov 03 '13 at 08:31
  • It doesn't show any errors, but when i type in a regular page id such as http://localhost/cms/example.php?p=2 it keeps showing the "unknown page" that i have in the DB. And yes a page with ID 2 exists so i have no idea why it's only outputting the unknown page which has an id of 1. – Matthew Myers Nov 03 '13 at 08:33
  • Enable exception throwing with Mysqli also log errors to file at the highest warning level and follow the error log. And: Use a variable with a name you can trust. Assign to variables. Do not do too many things at once. Test isolated code-chunks. – hakre Nov 03 '13 at 08:34
  • @MatthewMyers: What happens when you execute this query manually, using phpMyAdmin or similar? `SELECT * FROM pages WHERE id=2 LIMIT 1`? – Amal Murali Nov 03 '13 at 08:35
  • Only to ask if some value is inside the DB and using `SELECT *` for that is a big mistake. See here: [Why is SELECT * considered harmful?](http://stackoverflow.com/questions/3639861/why-is-select-considered-harmful) - use the tools you use to do distinct and concrete things, do things as concrete as possible. You can even create functions your own if it makes your life more easy. – hakre Nov 03 '13 at 08:39
  • You don't need to check `!isset($_GET['p'])` in the `else if` clause. Just use an `else` clause, since you already checked whether the variable is set. – Barmar Nov 03 '13 at 08:40
  • It gives me the page row with that ID. which is the page i want but it will not show in this script. – Matthew Myers Nov 03 '13 at 08:40
  • @Barmar , I've tried it without, but it still doesn't help this scripts progress. Thank you for noticing that though :) – Matthew Myers Nov 03 '13 at 08:41
  • 1
    Are you sure the query is succeeding? You need to check for errors from MySQL, and display the error message. – Barmar Nov 03 '13 at 08:42
  • possible duplicate of [PHP mysqli query to check if a row exist](http://stackoverflow.com/questions/5275214/php-mysqli-query-to-check-if-a-row-exist) – hakre Nov 03 '13 at 08:43
  • Alright, @hakre 's edit fixed it! Thank you so much! it works completely now! And thank you to everyone else for your help as well. – Matthew Myers Nov 03 '13 at 08:43
  • @Matthew Myers: When you post an example here, indent it properly. That's the least you need to do. Especially with errors you're not so sure with, you need to carefully review your code, beautify it, indent it, make the logic it has more clear to yourself. Readability is very important. – hakre Nov 03 '13 at 08:44
  • Alright i'll do better in future posts and scripts :) thank you hakre. – Matthew Myers Nov 03 '13 at 08:47

1 Answers1

1

As I commented it's merely about code-formatting and arrangement, here another suggestion that helps a lot when you're doing trouble shooting and is very simple to do:

function has_page_row(Mysqli $db, $pageId) 
{
    $sql    = sprintf("SELECT * FROM pages WHERE id=%d LIMIT 1", $getpage);
    $result = $db->query($sql);
    return $result && $result->mum_rows > 0;
}


$hasGet = isset($_GET['p']);
$hasRow = $hasGet && has_page_row($con, $_GET['p']);

$p = '0';
if ($hasRow) {
    $p =  $_GET['p'];
} elseif ($hasGet) {
    $p = '1';
}

You can then also easily fix the issue about the dorky SQL query by changing the code inside the new has_page_row function, see:

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836