简体   繁体   中英

How to use session variable in PHP for mysql queries?

I have no experience on using session variables for mysql queries. This is the sentence that I would like to use session variable:

mysql_query("UPDATE viewcounter SET `views` = `views`+1 WHERE pagename='$_SESSION['pro_title']'");

Please let me know how to do it in correct way...

you mispell the quotes. Also you'd better use ID for pages instead of page titles.

mysql_query("UPDATE viewcounter SET views = views +1 WHERE pagename='{$_SESSION['pro_title']}'");

Using sprintf can make things a bit clearer. Using %s here because I assumed that 'pro_title' is a string.

$query = sprintf( "UPDATE viewcounter SET `views` = `views`+1 WHERE  pagename='%s'", $_SESSION['pro_title']);
mysql_query($query);

Note: beware that the mysql extension has been deprecated since PHP 5.5 and will be removed in PHP 7.0. More information .

Problem with Quote, Try this piece of code:

mysql_query("UPDATE viewcounter SET views = views+1 WHERE pagename='" . $_SESSION['pro_title'] . "');

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