来自Mysql DB的Echo php变量

问题描述 投票:1回答:2

这是我的整个代码:

<?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;
?>

我使用下面给出的代码显示$ categories变量的值:Categories('。$ categories;。')实际上,上面的代码包括to不直接写在php网页中。代码“Categories('。$ categories;。')”包含在数据库中。因此$类别;无法解析。

我需要的是展示价值: 例如,如果$ categories = Books and Shelf; 我需要,类别('。$ categories;。'): - 类别(书籍和书架)

在从Mysql表中选择之前,已经在php页面中获取了$categories;值。

如何解析插入Mysql Row的php变量?

类别('。$ categories;。'): - 完整的html标记放在数据库中。 Mysql DB中的完整html代码。

php html mysql database
2个回答
0
投票

我想知道你为什么要在你的数据库中存储这样的变量引用,但为了解决它你可以简单地做这样的事情:

/*
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);

输出:您好,我的名字是约翰,我住在伊利诺伊州的某个地方


-1
投票

重写您的代码:

1)停止使用MySQL_功能,它们已被弃用,不安全且不应使用。有两种选择; mysqli_函数(面向对象或程序)或PDO功能(仅面向对象)

2)您的问题显而易见,是<h4>标签中的<?php标签还是仅仅是HTML?要输出PHP,您需要在print标记中包装echo / <?php文章,告诉服务器如何处理代码的这一部分。

同样,您需要确保将页面作为PHP页面进行处理,而不仅仅是作为HTML页面进行处理。那么页面名称以.php结尾(例如page.php)?

3)为清楚起见:while ($row = mysql_fetch_row($result))一次只能输出一行,每个MySQL行都有多列。

4)正确缩进括号,为每个括号内容键入四个空格(不是制表符)非常有用,例如下面的一点点。

5)你的While循环混乱;你也有任何括号。你的值$c_row只会是行中的最终值,但该行只会有一个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;     
       }

这会给你一个输出:

whatever_value whatever_value_row2

为了让你的<h4>按预期工作,你可以尝试用以下方法替换你的echo

  print "<h4>Categories ( ". $category; ." ) </h4>";
© www.soinside.com 2019 - 2024. All rights reserved.