开发者

Grab data from database and turn into variable [closed]

开发者 https://www.devze.com 2023-03-20 16:26 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.开发者_如何学Go

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();
0

精彩评论

暂无评论...
验证码 换一张
取 消