I want to query from the db records for a single post (title, body, date)
For example I have my query..
$query = 'SELECT * FROM posts WHERE id=$post_id';
From that query how can I echo the title for instance without a while or foreach loop?
You can just call mysql_fetch_assoc() (or any similar function) one. Like this:
$sql = "SELECT * FROM table";
$row = mysql_fetch_assoc( mysql_query($sql) );
echo $row['title'];
This is the easiest and most readable way to do this with the MySQL functions.
You can use mysql_result:
$sql = 'SELECT * FROM posts WHERE id=$post_id';
$query = mysql_query($sql);
echo mysql_result($query, 0, 'title');
This will echo the field title
for the first(0) row.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With