这是我的整个代码:
<?php
$query = "SELECT category FROM html where id = '1' ";
$result = mysql_query($query);
//while loop to display mysql table field values in html text boxes
$i = 0;
while ($row = mysql_fetch_row($result)) {
$count = count($row);
$y = 0;
while ($y < $count) {
$c_row = current($row);
next($row);
$y = $y + 1;
}
$i = $i + 1;
$category = $c_row;
}
echo $category;
?>
我使用以下代码显示$类别变量的值: 类别('。$类别;。'(Actaully,上述代码不直接写在PHP中网页。代码"类别('。$类别;。'("包含在数据库中。因此$类别;无法解析。
我需要的是显示值:
例如,如果$类别=书籍和书架;
我需要,类别('。$类别;。'(: - 类别(书籍和书架(
从MySQL表选择之前,$categories;
值已在PHP页面中获得。
如何解析MySQL行中插入的PHP变量?
类别('。$类别;。'(: - 完整的HTML标签放在数据库中。MySQL DB中的完整HTML代码。
我想知道您为什么在DB中存储类似的变量引用,但要解决它,您可以简单地做类似的事情:
/*
assuming $string contains EXACTLY this
<h4>Categories ( '. $categories; .' ) </h4>
*/
echo str_replace(''. $categories; .'',$categories, $string);
如果您通常需要在数据库中存储的字符串上替换单词,我会建议以下一个:
1(使用sprintf()
并以这样的方式存储字符串:
$string = '<h4>Categories ( %s ) </h4>';
echo sprintf($string, $categories);
2(使用str_replace()
和格式字符串,并带有括号围绕替换的牙套:
$string = '<h4>Categories ( {categories} ) </h4>';
echo str_replace('{categories}', $categories, $string);
最后一个的好处是您可以存储各种变量引用并相应地替换它们,而不必知道它们是否存在于字符串中:
$string = 'Hello, my name is {firstname} and I live in {city}, {state}';
$replace = ['{firstname}','{lastname}','{address}','{city}','{state}'];
$info = [
'firstname' => 'john',
'lastname' => 'doe',
'address' => '123 main st',
'city' => 'somewhere',
'state' => 'IL'
];
echo str_replace($replace, $info, $string);
输出:你好,我叫约翰,我住在某个地方,IL
重写您的代码:
1( stop 使用MySQL_
fuctions,不应使用它们,不应使用。有两种选择;mysqli_
函数(面向对象或过程(或PDO
功能(仅以对象为导向(
2(您的问题出现Uclear,您的<h4>
标签是<?php
标签中的标签还是仅仅是HTML?要输出PHP,您需要将print
/echo
文章包装在<?php
标签中,以告诉服务器如何处理代码的这一部分。
同样,您需要确保该页面被称为PHP页面,而不是将页面当作HTML页面。那么页名是否以.php
结束(例如page.php
(?
3(为了清晰:while ($row = mysql_fetch_row($result))
只能一次输出一排,每个mysql row 都保留多个列。
4(正确地缩进支架非常有用,每个支架内容键入四个空间((,如下面的litte位。
5(您的While
循环感到困惑;您也有任何括号。您的值 $c_row
只能是该行中的 final 值,但是该行只有一个unqiue值,即category
列的值,因为这就是SQL查询中指定的内容。
重写:
/***
* Fill with your own values, Address is usually 'Localhost'
***/
// connction details:
$conn = mysqli_connect(Address, User, Password, Database);
// A numeric column (id) does not need values to be quoted.
$query = "SELECT category FROM html where id = 1 ";
// note the mysqli_ and use of new $conn variable setout above
$result = mysqli_query($conn, $query);
/***
* Typical output from the above for returning two rows from
* the DB would be:
* $result[0]['category'] = "whatever_value"
* $result[0][0] = "whatever_value"
* $result[1]['category'] = "whatever_value_row2"
* $result[1][0] = "whatever_value_row2"
***/
// This will fetch all the rows, one row at a time,
// with array keys being the SQL column names
// (ignores numeric array keys).
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
{
// use definite named array key selectors to not need counters.
$category = $row['category'];
echo $category;
}
这将为您提供输出:
whyther_value
whywhen_value_row2
使您的<h4>
按预期工作,您可以尝试使用以下方式替换echo
print "<h4>Categories ( ". $category; ." ) </h4>";