PHP博客错误“意外结束”

时间:2012-11-15 09:42:15

标签: php html

您好我正在尝试为学校项目创建一个博客,这是我提出的代码。

<html>
<?php
$connection['host'] = '127.0.0.1';
$connection['user'] = 'root';
$connection['password'] = 'ascent';
$connection['webdb'] = 'login';
$connection['newstable'] = 'news';

if (isset($_GET['newsid']))
{
    $id = (int)$_GET['newsid'];
    connect::selectDB('webdb');

    $result = mysql_query("SELECT * FROM news WHERE id='".$id."'");
    $row = mysql_fetch_assoc($result); ?>
    <div class='box_two_title'><?php echo $row['title']; ?></div>

    <?php 
    Some cool way to post the "body" row here.
    ?>
</html>

基本上我只是想让它在网站上发布新闻,我已经从这里和那里借了一些代码,每当我尝试时都会出现一些错误。非常感谢帮助。 : - )

3 个答案:

答案 0 :(得分:3)

您忘记了结束花括号,在结束<? } ?>标记之前添加<html>

答案 1 :(得分:2)

我见过您的代码实际上代码中有一些错误,所以首先必须连接mysql数据库连接,然后选择DB。我给你正确的代码,所以试试这肯定会有助于创建你的博客。

<html>
<?php
$connection['host'] = '127.0.0.1';
$connection['user'] = 'root';
$connection['password'] = 'ascent';
$connection['webdb'] = 'login';
$connection['newstable'] = 'news'; 
if (isset($_GET['newsid']))
{
//code to set database connection
$link = mysql_connect($connection['host'], $connection['user'], $connection['password']);
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
// make login the current db
$db_selected = mysql_select_db($connection['webdb'], $link);
if (!$db_selected) {
    die ('Can\'t use db : ' . mysql_error());
}
//get news id
$id = (int)$_GET['newsid'];
$result = mysql_query("SELECT * FROM news WHERE id='".$id."'");
$row = mysql_fetch_assoc($result); ?>
<div class='box_two_title'><?php echo $row['title']; ?></div>
<?php //    Some cool way to post the "body" row here. } ?> </html>

答案 2 :(得分:1)

PHP基本上是在抱怨,因为你没有关闭if子句的大括号{

<html>
<?php
$connection['host'] = '127.0.0.1';
$connection['user'] = 'root';
$connection['password'] = 'ascent';
$connection['webdb'] = 'login';
$connection['newstable'] = 'news';

if (isset($_GET['newsid']))
{
    $id = (int)$_GET['newsid'];
    connect::selectDB('webdb');

    $result = mysql_query("SELECT * FROM news WHERE id='".$id."'");
    $row = mysql_fetch_assoc($result); ?>
    <div class='box_two_title'><?php echo $row['title']; ?></div>

    <?php 
//    Some cool way to post the "body" row here.
}
    ?>
</html>

此外,虽然您刚开始使用PHP,但应使用PDOmysqli来访问数据库。不推荐使用mysql_X函数。

至少在开发时检查错误时,MySQL可能会返回以查看查询失败的原因。