我有一个包含此代码段的网站:
<?php
mysql_connect('localhost','root','password');
mysql_select_db('news');
$id_article = $_GET['newsid'];
$query = mysql_query('SELECT * FROM news WHERE id="$id_article"');
{
echo '<div class="item"><h1><a href="read-news.php?newsid='.$query['id'].'">'.$query['subject'].'</a></h1><br />';
echo $query['full_content'].'<br / >';
echo date('D-M-Y', $query['date']).'<br / >';
echo 'Posted by '.$id_article;
echo '</div>';
}
?>
$id_article
获取上一个请求的ID。 $id_article
有效,但$query
不起作用。 $query['***']
仍为空白区域。我不知道为什么。请帮我!非常感谢!
答案 0 :(得分:0)
像这样使用
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result) or die(mysql_error());
echo $row['id']. " - ". $row['date'];
答案 1 :(得分:0)
您没有收到查询结果。使用如下:
<?php
mysql_connect('localhost','root','password');
mysql_select_db('news');
$id_article = intval($_GET['newsid']);
$query = mysql_query('SELECT * FROM news WHERE id=' . $id_article);
if (mysql_num_rows($query) > 0)
{
$row = mysql_fetch_assoc($query);
echo '<div class="item"><h1><a href="read-news.php?newsid='.$row['id'].'">'.$row['subject'].'</a></h1><br />';
echo $row['full_content'].'<br / >';
echo date('D-M-Y', $row['date']).'<br / >';
echo 'Posted by '.$id_article;
echo '</div>';
}
?>
提示:使用PDO或mysqli而不是mysql_函数!在新的php版本中不推荐/不支持Mysql_函数!
此外,在传递给您的查询之前,请对您的输入进行整理!
答案 2 :(得分:0)
您尚未使用mysql_fetch_assoc
。使用方法如下:
$query = mysql_query('SELECT * FROM news WHERE id=' . $id_article);
$row = mysql_fetch_assoc($query);
if(count($row)>0)
{
echo '<div class="item"><h1><a href="read-news.php?newsid='.$query['id'].'">'.$row['subject'].'</a></h1><br />';
echo $row['full_content'].'<br / >';
echo date('D-M-Y', $row['date']).'<br / >';
echo 'Posted by '.$id_article;
echo '</div>';
}
答案 3 :(得分:0)
更改此代码
$query = mysql_query('SELECT * FROM news WHERE id="$id_article"');
{
echo '<div class="item"><h1><a href="read-news.php?newsid='.$query['id'].'">'.$query['subject'].'</a></h1><br />';
echo $query['full_content'].'<br / >';
echo date('D-M-Y', $query['date']).'<br / >';
echo 'Posted by '.$id_article;
echo '</div>';
}
到
$sql = "SELECT * FROM news WHERE id = $id_article";
$query = mysql_query($sql);
while($result = mysql_fetch_assoc($query));
{
echo '<div class="item"><h1><a href="read-news.php?newsid='.$result['id'].'">'.$result['subject'].'</a></h1><br />';
echo $result['full_content'].'<br / >';
echo date('D-M-Y', $result['date']).'<br / >';
echo 'Posted by '.$id_article;
echo '</div>';
}
答案 4 :(得分:0)
您的代码中存在很多错误 mysql_query已被折旧 使用以下代码 和ckeck的错误
<?php
mysql_connect('localhost', 'root', 'password');
$conn = mysql_select_db('news');
$id_article = $_GET['newsid'];
if($query = mysqli_fetch_assoc(mysqli_query($conn,"SELECT * FROM news WHERE id=$id_article"))){
echo '<div class="item"><h1><a href="read-news.php?newsid=' . $query['id'] . '">' . $query['subject'] . '</a></h1><br />';
echo $query['full_content'] . '<br / >';
echo date('D-M-Y', $query['date']) . '<br / >';
echo 'Posted by ' . $id_article;
echo '</div>';
}
?>
答案 5 :(得分:0)
试试这个..
$id_article = $_GET['newsid'];
$query = "SELECT * FROM news WHERE id='$id_article'";
$query1=mysql_query($query);
答案 6 :(得分:0)
您应该调用函数来获取查询返回的行。您正在尝试访问$ query而不是行。您的代码应如下所示:
<?php
mysql_connect('localhost','root','password');
mysql_select_db('news');
$id_article = $_GET['newsid'];
$query = mysql_query('SELECT * FROM news WHERE id="$id_article"');
if ($query) {
$row = mysql_fetch_assoc($query));
echo '<div class="item"><h1><a href="read-news.php?newsid='. $row['id'].'">'. $row['subject'].'</a></h1><br />';
echo $row['full_content'].'<br / >';
echo date('D-M-Y', $row['date']).'<br / >';
echo 'Posted by '.$id_article;
echo '</div>';
} else {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}
?>
您还可以查看mysql_result(),mysql_fetch_array(),mysql_fetch_row()函数。