I need php mysql help please, before I start having seizures!

3 replies
Hi everyone, I'm having problems trying to get stuff from a database using php and mysql. Here's what I'm trying to do:

I have a mysql database with three columns, `id`, `content` and `url`. What I want to do is when a visitor comes to the site using mysite/this-is-a-page.php I want that page to display the content from the `content` colum that's in the database row associated with that url.

So when they come to the page at this-is-a-page.php, I already have the .htaccess file set to call a dummy.php page that will have the code to do what I want in the above paragraph.

This should be a very simple script for the dummy.php page with just a database connection and a few lines of code, but I've been trying a bunch of crap I've found on sites like stackoverflow, the php site and mysql book site for two days straight and can't seem to cobble together anything that works, so I scrapped it all and came here hoping someone can whip up something or at least point me to somewhere on the web that has some actual working code I can copy and paste before my head explodes.

Can anyone here help me please?

Thanks

Paul "f'n frustrated" Short
#mysql #php #seizures #start
  • Profile picture of the author bluebotbrother
    are you able to connect to database properly.??run query and store it in a variable like
    $result=mysql_query("select content
    from (your table name)
    where url="the url user entered");


    and display $result..
    {{ DiscussionBoard.errors[6971388].message }}
  • Profile picture of the author Charles Profit
    Ok... here is how we do it:

    Our .htaccess is setup to have index.php respond to any missing pages. Since you are using error.php, simply replace the ./index.php with ./error.php. (I only mention this because some people would redirect to the error.php page and this would be a problem...)

    PHP Code:
    <IfModule mod_rewrite.c>
        
    RewriteEngine On
        RewriteBase 
    /
        
    RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond 
    %{REQUEST_FILENAME} !-d
        RewriteRule 
    . /index.php [L]
    </
    IfModule
    Then our index.php page gets the name of the file the visitor was trying to visit, and saves it to a variable called $page

    $page = $_SERVER['REQUEST_URI'];

    Then we do our SQL and get the page content

    // database connection is not here. please be sure you are connected.
    // also, our connection is called $conn --- it looks like this:
    // $conn=mysql_connect ($dbserver,$dbuser,$dbpassword) or die ("I cannot connect to the <b>$dbname</b> database at <b>$dbserver</b> " . mysql_error());

    // this is the sql & database retrieval

    $sql = "select content from yourtable where lower(url) = '" . $page . "'";
    $result = mysql_query($sql, $conn) or die ("<br><strong>Error retrieving content - $sql</strong>");

    // this is where we grab the database details. this will make is so the 'content' field in your database is available to you as $content in php

    if ( mysql_num_rows($result) != 0 ) {
    extract (mysql_fetch_array($result));

    // display the content
    echo $content;
    } else {
    echo "the page is not found, and it's not in the database";
    }


    Finally, please note that the $_SERVER['REQUEST_URI'] command will return a leading / ... i.e. in your example the url you want to search for in the database would be /this-is-a-page.php

    Hope this helps.
    {{ DiscussionBoard.errors[6971591].message }}
  • Profile picture of the author Paul Short
    Thanks for the replies, everyone

    @bluebotbrother I am able to connect to the database and display all the info from all the fields. Been working on it some more since I posted originally.

    @Charles Thanks for the thorough info and your code is exactly what I was looking for. I've been reading up on the $_SERVER['REQUEST_URI'] command and it's the crucial part that I was missing.

    Also, this little project has turned into something a little bigger - a simple little CMS. Why not, right? It's only a matter of adding an admin form for entering content into the database which is pretty easy.

    Anyway, I want to get the url redirects to work properly first and the help I've gotten here will definitely get me up and running.

    Thanks again!

    Paul

    P.S. I'll update this thread once I get the code rewritten and let everyone know how it's coming along.
    Signature
    I write stuff for marketers and my current clients keep me comfortably
    busy. But if you make me the right offer, I'll write stuff for you too.
    {{ DiscussionBoard.errors[6971777].message }}

Trending Topics