PHP中非常基本的问题

时间:2011-07-20 16:58:59

标签: php mysql

$sql1="SELECT id FROM table ORDER BY id DESC LIMIT 1";

echo '1234/' . $sql1 . "<br />";

输出是1234 / SELECT id FROM table ORDER BY id DESC LIMIT 1

但我希望输出

1234/respective value

如果可能的话也给出一些解释

我是PHP的新手..

提前致谢

1 个答案:

答案 0 :(得分:4)

您实际上没有完成查询,只定义了您打算作为字符串变量运行的SQL查询。您需要建立数据库连接,执行查询并在respective value成为您想要的内容之前检索结果。

Start reading the PHP MySQL manual here.

// Connect to your database
$conn = mysql_connect($host, $username, $password);
mysql_select_db("database_name");

// Perform the query
$sql1="SELECT id FROM table ORDER BY id DESC LIMIT 1";
$result = mysql_query($sql);

// Fetch the results - only one row in your case
$row = mysql_fetch_array($result);

// Use the $row result set to output your string.
echo '1234/' . $row['id'] . "<br />";