简体   繁体   English

PHP无法显示MySqli查询中的表

[英]PHP cannot show tables from MySqli Query

I am trying to figure out why I can connect to a database, but cannot access the data in it. 我试图弄清楚为什么我可以连接到数据库,但是无法访问其中的数据。

Here's my configuration: 这是我的配置:

//config.php
<?php
define("HOST", "MYSERVERNAMEISHERE");
define("DATABASE", "users");
?>

My user logs in, and their information is passed to be checked: 我的用户登录,并将其信息传递给检查:

//login.php
<?php
if ($_POST) {
    if ($_POST["user"] && $_POST["password"]) {
        include_once "config.php";
        define("USER", $_POST["user"]);
        define("PASSWORD", $_POST["password"]);
        $link = new mysqli(HOST, USER, PASSWORD, DATABASE);
        if ($link) {
            $link->close();
            if ($_SESSION) {
                session_destroy();
            }
            session_start();
            $_SESSION["user"] = $_POST["user"];
            $_SESSION["password"] = $_POST["password"];
        }
    }
}
if ($_SESSION) {
    header('Location: profile.php');
}
else {
    header('Location: login.html');
}
?>

When they pass, they get to see their profile page. 当他们通过时,他们可以看到自己的个人资料页面。

//profile.php
<?php
session_start();
if (!$_SESSION["user"] || !$_SESSION["password"]) {
    session_destroy();
    header("Location: login.html");
}
else {
    include_once "config.php";
}
$link = new mysqli(HOST, USER, PASSWORD, DATABASE) or die("Unable to connect to database");
$result = $link->query("SHOW TABLES") or die("Unable to show tables");
...
ADDITIONAL PHP AND HTML CODE AFTER THIS POINT

The problem is that the process dies when I try to query the mysqli link. 问题是当我尝试查询mysqli链接时,进程死了。 (I get Unable to show tables ) Right now, the SHOW TABLES is just filler for debugging; (我Unable to show tables )现在, SHOW TABLES只是调试的填充器; I will actually have useful mysqli queries when I figure out the issue. 当我找出问题时,实际上我将有有用的mysqli查询。

Please help me determine where my bug is. 请帮助我确定我的错误在哪里。 If you find a typo or a reference link for me, sorry for wasting your time. 如果您发现我有错字或参考链接,很抱歉浪费您的时间。 I've been researching and debugging for hours now. 我已经研究和调试了几个小时了。

Thanks very much in advance. 首先十分感谢。

PS: If you have some good advice for changes I should make, I appreciate those too. PS:如果您对我应该进行的更改有很好的建议,我也很感谢。 It's my first time making a user login. 这是我第一次进行用户登录。

Your query in profile.php is failing because USER and PASSWORD are not defined. 您在profile.php中的查询失败,因为未定义USERPASSWORD When the person logs in, they are defined in login.php. 当该人登录时,它们在login.php中定义。 When redirected to profile.php, USER AND PASSWORD do not have values since they are not in config.php. 重定向到profile.php时, USERPASSWORD没有值,因为它们不在config.php中。

In profile.php, change 在profile.php中,更改

$link = new mysqli(HOST, USER, PASSWORD, DATABASE) or die("Unable to connect to database");

to

$link = new mysqli(HOST, $_SESSION["user"], $_SESSION["password"], DATABASE) or die("Unable to connect to database");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM