0

I'm very new to PHP/Mysql and I naturally have a few questions. I was following a youtube tutorial on creating a simple dynamic website that pulls data content from a MySQL database then displays the content on a single PHP index page. I followed this tutorial to the point where I was using PHP/MySQL to connect to the DB, run a query, fetch the query using a fetch_assoc array. but nothing would display in the body of the page. During the trouble shooting process I was advised that I should be using PDO instead of the older MySQL methods. Can someone decipher my current "older" MySQL code and translate it into the proper PDO coding approach so that I can learn to grasp PDO, since it is the future I should start to understand it now :)

index.php:

<?php
// Setup document:
include('config/setup.php');

?>

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title><?php echo $page_title; ?> - test site</title>

<link rel="stylesheet" type="text/css" href="css/styles.css">

</head>

<body>

 <div class="wrap_overall">

    <div class="header">
       <?php include('template/header.php'); ?>
    </div>

    <div class="nav_main">
        <?php include('template/nav_main.php'); ?>
    </div>

    <div class="content">
        <?php //include('content/'.$pg.'.php');
        // the database connection, our query
        $q = "SELECT * FROM pages WHERE page = '$pg' AND status = 1 LIMIT 1";
        $r = mysqli_query($dbc, $q);
         if (!$r) {
         die('Invalid query: ' . mysql_error());
         }

        $page = mysqli_fetch_assoc($r);

        echo '<h1>'.$page['title'].'</h1>';
        echo '<div class="content_body">'.$page['body'].'</div>';

         ?>    
    </div>

    <div class="footer">
        <?php include('template/footer.php'); ?>
    </div>

  </div>

</body>
</html>

setup.php:

<?php ## Setup Document
// host(or location of the database), username, password, database name
//Variables for connecting to your database.
//These variable values come from your hosting account.
$hostname = "localhost";
$username = "atomcmsadmin";
$password = "uniCi2i";
$dbname = "Atom_CMS";
//Connecting to your database
$dbc = mysqli_connect($hostname, $username, $password) OR DIE ("Unable to 
connect to database! Please try again later.");
mysqli_select_db($dbname);
//include('functions/sandbox.php');
if ($_GET ['page'] == '') {
    $pg = 'home';} 
else {
    $pg = $_GET ['page']; }
$page_title = get_page_title($dbc, $pg);
?>

sandbox.php

<?php
// Sandbox Functions

function get_page ($dbc, $pg) {

    // the database connection, our query
    $q = "SELECT title FROM pages WHERE type = 1, page = '$pg' AND status = 1 LIMIT 1";
    $r = mysqli_query($dbc, $q);

    $page = mysqli_fetch_assoc($r);

    echo '<h1>'.$page['title'].'</h1>';
    echo '<div class="content">'.$page['body'].'</div>';        
}
function get_page_title ($dbc, $pg) {

    $q = "SELECT title FROM pages WHERE type = 1, page = '$pg' AND status = 1 LIMIT 1";
    $r = mysqli_query($dbc, $q);

    $page = mysqli_fetch_assoc($r);

    return $page['title'];      
}
?>
4

2 回答 2

1

http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/

您需要更改的只是设置连接,请查看教程以了解语法

与数据库的交互仍然大体相同

于 2013-11-07T03:28:38.473 回答
0

我想没有人会为你做这项工作。但我可以帮助你帮助自己:

从您的代码中,您需要学习这些 PDO 方法:

还可以查看有关 PHP 手册的PDO 书。你会比你想象的更容易学习所有的东西!祝你好运!

于 2013-11-07T03:29:00.257 回答