简体   繁体   中英

Echo php Variable from Mysql DB

This is my Entire Code:

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

I am showing the value of $categories variable using this code given below: Categories ( '. $categories; .' ) Actaully, the above code including to is not directly written in php web page. The code "Categories ( '. $categories; .' ) " is containing in the data base. Therefore $categories; cannot be parsed.

What I need is to show the Value:
Eg, if $categories = Books and Shelf;
I need, Categories ( '. $categories; .' ) :- Categories (Books and Shelf)

The $categories; value is already obtained in the php page before selecting from Mysql Table.

How can I parse php variable inserted in Mysql Row?

Categories ( '. $categories; .' ) :- The complete html tag is putted in the data base. The complete html code in the Mysql DB.

I'd like to know why you are storing variable references like that in your db but to solve it you could simply do something like this:

/*
assuming $string contains EXACTLY this
<h4>Categories ( '. $categories; .' ) </h4>
*/
echo str_replace('\'. $categories; .\'',$categories, $string);

If you commonly need to do word replacement on strings stored in a database I would recommend one of the following instead:

1) Use sprintf() and store your string like this:

$string = '<h4>Categories ( %s ) </h4>';
echo sprintf($string, $categories);

2) Use str_replace() and format string with braces around replacements:

$string = '<h4>Categories ( {categories} ) </h4>';
echo str_replace('{categories}', $categories, $string);

The benefit of that last one is you could store all kinds of variable references and replace them accordingly without having to know if they exist in the 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);

OUTPUT: Hello, my name is john and I live in somewhere, IL

A rewrite of your code:

1) Stop using MySQL_ fuctions, they are deprecated, insecure and should not be used . There are two alternatives; mysqli_ functions (Object orientated or Procedural) or PDO functionality (object orientated only)

2) Your question appears uclear, is your <h4> tag within <?php tags or is it just HTML? To output PHP you need to wrap the print / echo article in <?php tags to tell the server how to process this section of the code.

Likewise, you need to be sure the page is pocessed as a PHP page rather than just as an HTML page. So does the page name end with .php (such as page.php )?

3) For clarity: while ($row = mysql_fetch_row($result)) will only ever output one row at a time, each MySQL row with hold numerous columns .

4) It's very useful for you to indent your brackets correctly, typing four spaces ( not tab ) for each bracket contents, as exampled a litte bit below.

5) Your While loops are confused; you have too any brackets. Your value $c_row will only ever be the final value found in the row, but the row will only ever have one unqiue value in it -- that of the category column, because that's what's specified in the SQL query.


Rewrite:

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

That would give you an output:

whatever_value
whatever_value_row2

To make your <h4> work as expected you can try replacing your echo with:

  print "<h4>Categories ( ". $category; ." ) </h4>";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM