简体   繁体   English

MySQL选择$ id在哪里

[英]MySQL select where $id

I have a website with this code snippet: 我有一个带有此代码段的网站:

<?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>'; 
}
?>

The $id_article gets the id from an previous request. $id_article从上一个请求获取ID。 The $id_article works, but the $query doesn't. $id_article有效,但$query无效。 $query['***'] still blank space. $query['***']仍为空白。 I don't know why. 我不知道为什么 Please help me! 请帮我! Thanks a lot! 非常感谢!

Use like this 这样使用

$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result) or die(mysql_error());
echo $row['id']. " - ". $row['date'];

You don't get your query result. 您没有得到查询结果。 Use like below: 用法如下:

<?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>';
   } 
?>

Tip: use PDO or mysqli instead of mysql_ functions! 提示:使用PDO或mysqli代替mysql_函数! Mysql_ functions are deprecated / not supported in new php versions! 不推荐使用Mysql_函数/在新的php版本中不支持!

Further, sanatize your input before passing to your query! 此外,在传递给查询之前,请先清理输入内容!

You have not used mysql_fetch_assoc . 您尚未使用mysql_fetch_assoc Use as below : 用途如下:

$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>'; 
}

change your this code 更改您的此代码

$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>'; 
}

to

$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>'; 
}

There are a lot of errors in your code mysql_query has been depreciated Use the following code and ckeck for the errors 您的代码中有很多错误mysql_query已被弃用使用以下代码并确认错误

<?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>';
}

?>

Try This.. 尝试这个..

$id_article = $_GET['newsid'];
$query = "SELECT * FROM news WHERE id='$id_article'";
$query1=mysql_query($query);

You should call a function to get the row returned by the query. 您应该调用一个函数来获取查询返回的行。 You are trying to access the $query instead of the row. 您正在尝试访问$ query而不是行。 Your code should look like this: 您的代码应如下所示:

<?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);
  }
?>

You can also take a look at the mysql_result(), mysql_fetch_array(), mysql_fetch_row() functions. 您还可以查看mysql_result(),mysql_fetch_array(),mysql_fetch_row()函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM