简体   繁体   中英

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. The $id_article works, but the $query doesn't. $query['***'] still blank space. 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! Mysql_ functions are deprecated / not supported in new php versions!

Further, sanatize your input before passing to your query!

You have not used 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

<?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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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