php/mysql problem building cms

jlknauff

New member
Aug 25, 2008
237
1
0
I'm building sort of a cms...

The db field that holds the urls has two urls in it at the moment. My challenge right now is just to get the script to tell me whether it finds the url in the database (I'm assuming if it does, my next step is to pass headers to the browser). Any idea what I'm doing wrong here?

PHP:
<?php

$mysqli = new mysqli("localhost", "user", "pw", "db");

if (mysqli_connect_errno()) {
    printf("Connection failed: %s\n", mysqli_connect_error());
    exit();
} else {

    $req_url = $_SERVER['REQUEST_URI']; 

    echo $req_url;

    $sql = "SELECT * FROM ag_pages";
    $res = mysqli_query($mysqli, $sql);

    if ($res) {
        while ($newArray = mysqli_fetch_array($res, MYSQLI_ASSOC)) {
            $page_url = $newArray['page_url'];

                if (in_array($req_url, $page_url)) {
                    echo "Page Exists";
                }

            }

        } else {

            printf ("Could not retrieve records: %s\n", mysqli_error($mysqli));
        }

    mysqli_free_result($res);
    mysqli_close($mysqli);
}
?>
 


PHP:
$sql = "SELECT * FROM ag_pages WHERE page_url = '".$reg_url."'";
$res = mysqli_query($mysqli, $sql);

$row_count = mysqli_num_rows($res); // number of entries in ag_pages with $reg_url in the page_url field

if ($row_count >= 1) { 
// page exists
} else {
// page doesn't exist
}
 
you're getting an element of an array:
$page_url = $newArray['page_url'];

and then trying to search it as if it were an array:
if (in_array($req_url, $page_url))

Also, $_SERVER['REQUEST_URI'] will not return a full URL; not sure what the fields in your DB contain, but if $page_url is a full URL, you may want to try to do a partial match.