dousou3027 2016-08-06 19:12
浏览 55

在非对象上调用成员函数(包括database.php)

I am working on a new website, and I rewrote some of my previously working code for the new one to recreate the login system. Unfortunately, the rewritten version does not work so let me explain what's going on.

I have "index.php", which holds the "titlemenu.php" that is used on every single page. I intend to run the login box in this title menu (top bar) so people can log in despite what page their own, and when they successfully log in, it refreshes the page they are currently on so they're logged in.

So far, bingo - It works. They're logged in and view members only pages. However, the title bar is a different story. It still shows "LOG IN" and "REGISTER", despite being logged in. Immediatelly, I though this was a problem with that specific php file, so I logged into that php only, bypassing index. When I try to sign in on that blank page only, I get: Fatal Error: Call to a non-member function prepare() on line 18 I know this usually means your variable is not declared or not being ran, so I decided to troubleshoot it. I did a var_dump on every variable I used, which returned valid results. I checked index.php, which has no errors and runs fine, and shows users as logged in. "Welcome Back, RhapidFyre!" is what it says on the index, but the login box shows that I am not logged in yet.

loginheader.php

    <?include "641a/database.php";?><div id="login"><?
$mydbid = $_SESSION['user'];
$myquery = "SELECT * FROM logins WHERE dbid = $mydbid";
$myrow = mysqli_fetch_assoc($myquery);
$myname = $myrow['nickname'];

if($_GET['do'] == "logout") {
    unset($_SESSION['user']);
    ?><script type="text/javascript">
    window.location.replace("index.php");
    </script><?
}
if($_GET['do'] == 'login') {
            $query = 'SELECT * FROM logins WHERE username = :username';
            $query_params = array(':username' => $_POST['username']);

            var_dump($query);
            var_dump($query_params);
            try                                          
            {                                            
                $stmt = $db->prepare($query); //LINE 18              
                $result = $stmt->execute($query_params);   
            }                                            
            catch(PDOException $ex)                       
            {
                ?><script type="text/javascript">
                window.location.replace("redirect.php?error=username");
                </script><?
            }(Further irrelevant code follows)

database.php

<?  session_start();
$dbusername = "********";
$dbpassword = "********";
$dbhost = "localhost";
$dbname = "********";
$link = mysqli_connect($dbhost, $dbusername, $dbpassword, $dbname);

// PDO Initialization
try {
    $db = new PDO("mysql:host={$dbhost};dbname={$dbname};charset=utf8", $dbusername, $dbpassword); 
}

// If connection fails, die.
catch(PDOException $ex){
    die("Failed to connect to the MySQL Server!" . $ex->getMessage());
}

Snippet of index.php

<html>
<head>

    <title>My Website Template</title>
    <link rel="stylesheet" type="text/css" href="base.css"/>
</head>
<body>
    <div id="header">
        <?include "pages/titles.php";?>
        <?include "pages/loginheader.php";?>
    </div>
  • 写回答

1条回答 默认 最新

  • duanhao1004 2016-08-06 19:40
    关注

    The problem stemmed from using two connection types while trying to debug. Changed it all to MySQLi and it ran fine, but created new problems. As my programming instructor used to say, "Yay! New errors!"

    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)