What is the fastest and easiest way to get a specific value from a mysql database table and echo it as a php variable?
For example:
I have a table like so:
label value
------------------------
postpage About
name My Blog
I want to echo out the value column that has a label of postpage. In this case it would be the text "About". How would I do this with as little code as possible using php and mysql?
Thanks!
This should help get you started. Keep in mind it is a very simple example, and there are other visual/security changes you would need to make in a real application:
<?php
//Connect to your database....
$info = mysql_fetch_array(mysql_query("SELECT * FROM `your-table` WHERE `label` = 'postpage'"));
echo $info['value'];
?>
//connect with database
$result = mysql_query("SELECT label, value FROM table");
while ($row = mysql_fetch_array($result)) {
echo $row["label"] . ": ". $row["value"];
}
This will output all your labels & value
If you want only label postage just add the WHERE clause in the query:
SELECT label, value FROM table WHERE label = 'postage'
mysql_connect("localhost", "name", "pass");
mysql_select_db("database");
$value = mysql_fetch_assoc(mysql_query("SELECT value FROM table WHERE label = 'postpage'"));
echo $value['postpage'];
mysql_close();
精彩评论