0

我需要将其转换为课程的 PDO 形式,但由于我对这一切都不熟悉,有人可以帮我吗?我的教授声称每个功能都可以转换,但我在网上找不到任何可以帮助我的东西。

<?php
    // rnfunctions.php
    $dbhost = 'localhost';
    $dbname = 'social_local';
    $dbuser = 'root';
    $dbpass = 'root';
    $appname = "Social Networking Test Site";
    try
    {
        $dbh = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
    }
    catch ( PDOException $e )
    {
        echo $e->getMessage();
    }

    function createTable ($name, $query)
    {
        if (tableExists($name)) {
            echo "Table '$name' already exists<br />";
        } else {
            queryMysql("CREATE TABLE $name($query)");
            echo "Table '$name' created<br />";
        }
    }

    function tableExists ($name)
    {
        $result = queryMysql("SHOW TABLES LIKE '$name'");
        return mysql_num_rows($result);
    }

    function queryMysql ($query)
    {
        $result = mysql_query($query) or die(mysql_error());
        return $result;
    }

    function destroySession ()
    {
        $_SESSION = array();
        if (session_id() != "" || isset($_COOKIE[session_name()])) {
            setcookie(session_name(), '', time() - 2592000, '/');
        }
        session_destroy();
    }

    function sanitizeString ($var)
    {
        $var = strip_tags($var);
        $var = htmlentities($var);
        $var = stripslashes($var);
        return $var;
    }

    function showProfile ($user)
    {
        if (file_exists("$user.jpg")) {
            echo "<img src='$user.jpg' border='1' align='left' />";
        }
        $result = queryMysql("SELECT * FROM rnprofiles WHERE user='$user'");
        if (mysql_num_rows($result)) {
            $row = mysql_fetch_row($result);
            echo stripslashes($row[1]) . "<br clear=left /><br />";
        }
    }
    ?>
4

2 回答 2

1

您需要先实例化一个 PDO 类。然后学习 PDO 功能。

$dbh = new PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME, DB_USER, DB_PASS);
$stmt = $dbh->prepare($sql);
$stmt->execute();
return $stmt;

使用类似的东西$stmt->FetchAll()来获得结果。

于 2012-09-19T19:34:27.340 回答
0

这样的事情可能会帮助你:

$dbh = new PDO("mysql:host=localhost;dbname=yourBaseName", 'someLogin', 'somePassword');
$dbh->prepare('SELECT login FROM members WHERE login=? AND password=?');
$stm = $dbh->execute(array($login, $pass));
if (($row = $stm->fetchObject())) {
    echo "You are loggued!";
    // stuffs with loggued user
} else {
    echo "Invalid user or password";
    // login form again
}
于 2012-09-19T19:44:28.070 回答