Mysqli不会允许表格在被召唤时显示

时间:2014-03-26 21:18:21

标签: php mysql mysqli

截至最近我一直在学习php和在那之间我必须现在使用Mysql以保持我的更大的信息表ogranized之间的联合,我写这个代码,以显示表(或者我认为我做对了)。我完全难倒,因为我看不到我正在呼叫的任何显示表,我越是尝试越少我工作,所以我想知道是否有人可以在我的代码中看到一个循环漏洞或者我做错了什么?或者也许我所做的一切都是错的......?

`

    $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);
?>`

3 个答案:

答案 0 :(得分:0)

您是否忘记了页面开头的开头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");
}

?>

答案 1 :(得分:0)

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

Here is a correct implementation进行比较。它可能是第一个PHP开始标记,我还在connect语句中添加了默认端口,并添加了一些带有错误消息的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"); }

?>

答案 2 :(得分:0)

如果我改变你的一些代码,那好吗?

见:

<!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>