I am using this code to view a news headline from mysql table but I want also to add one more thing please... I want to update a field named news_views (int) with +1 everytime someone views this article.. how can I do this please...
<?php $news_id = $_GET['id']; $conn = mysql_connect('mysql.jassimrahma.com', 'jassimdb', 'xxxxxxx'); $sql = "SELECT * FROM news WHERE news_id = '$news_id'"; mysql_select_db("jassimrahma", $conn) or die(mysql_error()); $result = mysql_query($sql, $conn) or die(mysql_error()); $count = mysql_num_rows($result); if($count == 1) { $db_field = mysql_fetch_assoc($result); $news_url = $db_field['news_url']; $news_title = $db_field['news_title']; $news_webpage = file_get_contents($news_url); // $news_webpage = file_get_contents('http://www.businessweek.com/ap/financialnews/D9RH5MEO0.htm'); echo $news_webpage; } else { header("location:index.php"); } ?>
<?php $news_id = $_GET['id'];
$conn = mysql_connect('mysql.jassimrahma.com', 'jassimdb', 'xxxxxxx'); $sql = "SELECT * FROM news WHERE news_id = '$news_id'"; mysql_select_db("jassimrahma", $conn) or die(mysql_error()); $result = mysql_query($sql, $conn) or die(mysql_error()); $count = mysql_num_rows($result); if($count == 1) { $db_field = mysql_fetch_assoc($result); $news_url = $db_field['news_url']; $news_title = $db_field['news_title']; $news_webpage = file_get_contents($news_url); // $news_webpage = file_get_contents('http://www.businessweek.com/ap/financialnews/D9RH5MEO0.htm'); echo $news_webpage; } else { header("location:index.php"); }
?>
Another SQL command:
UPDATE news SET news_views = news_views + 1 WHERE news_id='$news_id'
where?
Presumably at the point where you serve the page up for the user to view.
do you mean separate <php>?
can i place it in the if block or it is not good idea?
jrahma wrote:do you mean separate <php>?
What makes you think that?
can i place it in the if block
Is that where you serve the page up for the user to view?
I am asking that because if you see my code above you'll see that the within the(if) block I am still reading the data so how can I place another sql statement if the current is not closed yet?
or should I close it in the if and start another sql statement there itself?
No, you've already read it in this line:
$result = mysql_query($sql, $conn) or die(mysql_error());