我正在尝试创建一个PHP脚本,如果MySQL行id=1的value字段中的条目为1,则显示文本,如果它具有不同的值,则不显示任何文本。到目前为止,我写的脚本似乎不起作用:
<?php
// connect to the database
include('connect-db.php');
?>
<?php $result = mysql_query("SELECT * FROM Cups WHERE id='1'"); if
($row_queryname['value'] == '1') {?>
Display text here.
<?php }?>我是一个新手,所以任何帮助都将不胜感激!谢谢。
发布于 2011-11-22 11:51:48
在进行查询之后,您需要使用其中一个"fetch“函数来实际获取一行。由于您尚未指定任何字段名称,因此我将使用mysql_fetch_assoc()
<?php
// connect to the database
include('connect-db.php');
$result = mysql_query("SELECT * FROM Cups WHERE id='1'");
$ar = mysql_fetch_assoc($result);
if ($ar['fieldname'] == '1') // Whatever your field name is
{
?>
Display text here.
<?php
}
?>mysql_fetch_assoc()获取表示结果行的关联数组,其中数组键是字段名称。
注意:此示例不包括任何错误检查。
发布于 2011-11-22 11:57:09
mysql_fetch_array -以关联数组和/或数值数组的形式获取结果行。See More about mysql_fetch_array()
<?php
// connect to the database
include('connect-db.php');
$query = mysql_query("SELECT * FROM Cups WHERE id='1'");
$result = mysql_fetch_array($query);
if ($result['value'] == '1')
{
echo "your text here";
}
?>发布于 2011-11-22 12:00:48
你只是少了一两步。这里有一个非常简单的方法。不要忘记检查查询是否返回任何行,还要记住mysql_fetch_assoc()返回一个行数组,而不仅仅是一行(即使该数组只包含一行)。
<?php
// connect to the database
include('connect-db.php');
?>
<?php
$result = mysql_query("SELECT * FROM Cups WHERE id='1'");
if (mysql_num_rows($result) > 0) {
$rows = mysql_fetch_assoc($result);
// $rows is an array of associative arrays
if ($rows[0]['value'] == '1') {
?>
Display text here.
<?php
}
?>https://stackoverflow.com/questions/8221727
复制相似问题