【问题标题】:No Response From Localhost Database? [closed]本地主机数据库没有响应? [关闭]
【发布时间】:2015-12-03 08:41:03
【问题描述】:

我不是专业的编码员。我只是想建立自己的网站。我执行了以下步骤,但没有成功。

1-我在 localhost 数据库中为博客创建了类别。

http://i.hizliresim.com/3jqE3M.jpg

2-我创建了database.php,如下所示。

<?php 

mysql_connect("localhost","root",""); 

mysql_select_db("mycms"); 

?>

3-我在home.html中添加了以下代码

<?php 
include("includes/database.php"); 

$get_cats = "select * from `categories`"; 

$run_categories = mysql_query($get_cats); 

while ($cats_row=mysql_fetch_array($run_cats)) { 

$cat_id=$cats_row['cat_id']; 
$cat_title=$cats_row['cat_title']; 

echo "<li><a href='home.html?cat=$cat_id'>$cat_title</a></li>"; 

} 

?> 

但是我看不到来自http://localhost/MyCMS/home.html 的类别

没有用。问题是什么 ?我怎么解决这个问题。请帮帮我:(

【问题讨论】:

  • 什么“没用”?
  • html 页面上的 php 代码@RowlandShaw。 ;)
  • @Jai 您假设他的服务器未设置为将 .html 文件解析为 php(或者有任何 URL 重写)
  • 是的!正如我所看到的问题。好像是@RowlandShaw。

标签: php jquery html mysql


【解决方案1】:

第一个 PHP 代码仅在文件名的扩展名为 .php 时才会执行,因此将 home.html 更改为 home.php

其次,您的代码中也有一个小错误。

<?php 
include("includes/database.php"); 

$get_cats = "select * from `categories`"; 

$run_categories = mysql_query($get_cats); 

//while ($cats_row=mysql_fetch_array($run_cats)) { 

// The parameter to mysql_fetch_array should be 
// the result of a call to mysql_query()
while ($cats_row=mysql_fetch_array($run_categories )) { 

    $cat_id=$cats_row['cat_id']; 
    $cat_title=$cats_row['cat_title']; 

    echo "<li><a href='home.html?cat=$cat_id'>$cat_title</a></li>"; 
} 
?> 

还有

请不要使用 mysql_ 数据库扩展,它已被弃用(在 PHP7 中永远消失了) 特别是如果您只是学习 PHP,请花精力学习 PDOmysqli_ 数据库扩展, and here is some help to decide which to use

【讨论】:

    【解决方案2】:

    home.html 更改为home.php 作为带有.html 扩展名的html 页面无法执行PHP 代码。还有一件事,mysql_* 将来会被弃用,请改用mysqli_*pdo。在while循环中,一定要传递$run_categories而不是$get_cats,如下代码:

    // this variable only store string, not the query itself
    $get_cats = "select * from `categories`";
    $run_categories = mysql_query($get_cats); 
    
    while ($cats_row=mysql_fetch_array($run_categories)) {...}
    

    推荐使用以下任一版本:

    1) Mysqli 版本

    <?php 
    // database connection
    $con = mysqli_connect("localhost","root","");
    mysqli_select_db($con, "mycms");
    
    $get_cats = "select * from `categories`";
    $run_categories = mysqli_query($con, $get_cats); 
    
    while ($cats_row=mysqli_fetch_array($run_categories )) { 
    
     $cat_id=$cats_row['cat_id']; 
     $cat_title=$cats_row['cat_title'];
     echo "<li><a href='home.html?cat=$cat_id'>$cat_title</a></li>";
    
    } 
    ?>
    

    2) PDO 版本

    <?php 
    // database connection
    $con = new PDO('mysql:host=localhost;dbname=mycms;charset=utf8', 'root', '');
    $get_cats = "select * from `categories`";
    $run_categories = $con->query($get_cats); 
    
    while ($cats_row = $run_categories->fetch(PDO::FETCH_ASSOC)) { 
    
     $cat_id=$cats_row['cat_id']; 
     $cat_title=$cats_row['cat_title'];
     echo "<li><a href='home.html?cat=$cat_id'>$cat_title</a></li>";
    
    } 
    ?>
    

    【讨论】:

    • mysqlipdo 版本查看答案。
    猜你喜欢
    • 1970-01-01
    • 2017-03-16
    • 2011-03-09
    • 2015-07-28
    • 1970-01-01
    • 1970-01-01
    • 2013-06-27
    • 1970-01-01
    • 2021-09-16
    相关资源
    最近更新 更多