Mysqli 在调用时不允许显示表



作为最近我一直在学习php和在连接之间,我现在必须使用Mysql,以保持我的更大的信息表组织,好吧,我写了这段代码,以显示表(所以我认为我做得对)。我完全被难住了,因为我看不见任何我正在调用的显示表,我越试越少工作,所以我想知道是否有人能在我的代码中看到一个循环洞,或者我可能做错了什么?或者也许我所做的一切都是错的?

'

    $dbhost = "localhost";
    $dbuser = "juliegri_AAlassa";
    $dbpass = "********"; // to not show real password
    $dbname =  "juliegri_AAlassaly";
    $connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
    if(mysqli_connect_errno()) {
        die("Database connection failed: " .
            mysqli_connect_error() . 
             " (" . mysqli_connect_errno () . ")"
             );
    }
?>

<?php
    $query = "SELECT * ";
    $query .= "FROM subjects ";
    $query .= "WHERE visible = 1 ";
    $query .= "ORDER BY position ASC";
    $result = mysqli_query($connection, $query);
    if (!$result) {
        die("Database query failed");
    }

?>
<!doctype html>
<html lang="en">
<head>
<title>databases</title>
</head>
<body>
 <ul>
    <?php
        while($subject = mysqli_fetch_assoc($result)) {
            ?>
            <li><?php echo $subject["menu_name"] . "(" . $subject["id"] . ")"; ?></li>
        <?php
            }
        ?>
</ul>
    <?php
     mysqli_free_result($result);
    ?>
 </body>
</html>
<?php
 mysqli_close($connection);
?>`

您是否忘记了页面开头的PHP开始标签?

<?php
$dbhost = "localhost";
$dbuser = "juliegri_AAlassa";
$dbpass = "********"; // to not show real password
$dbname =  "juliegri_AAlassaly";
$connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
if(mysqli_connect_errno()) {
    die("Database connection failed: " .
        mysqli_connect_error() . 
         " (" . mysqli_connect_errno () . ")"
         );
}
$query = "SELECT * ";
$query .= "FROM subjects ";
$query .= "WHERE visible = 1 ";
$query .= "ORDER BY position ASC";
$result = mysqli_query($connection, $query);
if (!$result) {
    die("Database query failed");
}
?>

我认为有两件事可能是错的。

下面是一个比较的正确实现。它可以是第一个PHP开始标记,我还为连接语句添加了默认端口,并添加了一些带有错误消息的try catch,这些可以告诉连接或查询是否不工作。

<?php
$dbhost =  "localhost";
$dbuser =  "juliegri_AAlassa";
$dbpass =  "********"; // to not show real password
$dbname =  "juliegri_AAlassaly";
//original connect statement with a port added in
try {
     $connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname , 3306);
} catch(Exception $e) { echo $e->getMessage(); }

if ($mysqli->connect_errno) {
    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}

//Query looks fine, easier to trouble shoot when its one line, first get it working then break it up
$query = "SELECT * FROM subjects WHERE visible = 1 ORDER BY position ASC";
// This will try to fetch the result and give an error if it can't.
try {  $result = mysqli_query($connection, $query);
} catch(Exception $e) { echo $e->getMessage(); }
if (!$result) { die("Database query failed"); }
?>

我可以修改你的一些代码吗?

看到这个:

<!doctype html>
<html lang="en">
<head>
<title>databases</title>
</head>
<body>
<?php
/* ESTABLISH CONNECTION */
$connection=mysqli_connect("localhost","juliegri_AAlassa","YourPassword","juliegri_Aalassaly");
if(mysqli_connect_errno()){
echo "Error".mysqli_connect_error();
}
/* START QUERY */
$result=mysqli_query($connection,"SELECT * FROM subjects WHERE visible='1' ORDER BY position ASC");
?>
<ul>
<?php
/* DO THE WHILE LOOP */
while($subject = mysqli_fetch_array($result)) {
?>
<li><?php echo $subject['menu_name'] . "(" . $subject['id'] . ")"; ?></li>
<?php
} /* END OF WHILE LOOP */
?>
</ul>
</body>
</html>

最新更新