Deanjoe Deanjoe - 15 days ago 6
PHP Question

Export datas from DB to HTML?

i'm working on a library DB, exporting datas from an host DB to display them into a table(HTML/CSS classes) but it reports php code in the table-cell:

Acutally display table

I inserted the following codes(HTML/PHP):

<!--DATA PAGE-->


<div id="datas">

<?php

# Init the MySQL Connection
if( !( $db = mysql_connect( 'database' ) ) )
die( 'Failed to connect to MySQL Database Server - #'.mysql_errno().': '.mysql_error();
if( !mysql_select_db( 'ram' ) )
die( 'Connected to Server, but Failed to Connect to Database - #'.mysql_errno().': '.mysql_error();

# Prepare the INSERT Query
$insertTPL = 'INSERT INTO `TITOLO` VALUES( "%s" , "%s" , "%s" , "%s" )';
$insertSQL = sprintf( $insertTPL ,
mysql_real_escape_string( $TITOLO ) ,
mysql_real_escape_string( $AUTORE ) ,
mysql_real_escape_string( $EDITORE ) ,
mysql_real_escape_string( $ANNO ) ,
mysql_real_escape_string( $#ID ) );

# Execute the INSERT Query
if( !( $insertRes = mysql_query( $insertSQL ) ) ){

<div class="tbook" align="center">
<div class="tr">
<div class="td">TITOLO</div>
<div class="td">AUTORE</div>
<div class="td">EDITORE</div>
<div class="td">ANNO</div>
<div class="td">#ID</div>
</div>
<?php

while( $row = mysql_fetch_assoc( $selectRes ) ){
echo
"<div class="tr">
<div class="td">{$row['TITOLO'\]}</div>
<div class="td">{$row['AUTORE'\]}</div>
<div class="td">{$row['EDITORE'\]}</div>
<div class="td">{$row['ANNO'\]}</div>
<div class="td">{$row['#ID'\]}</div>
</div> \n";
}
?>

</div>

<?php mysql_close($connector); ?>

</div>


Note: Host DB queries works OK but PHP code won't load datas for display them in my table.

https://jsfiddle.net/krzv2wkq/2/ show how actually works(missing datas from DB)

Answer

I am getting a hard time to relate your code in your question.

  1. Why are you using "INSERT" in the first place when you are trying to fetch data from DB and display it on the table?

  2. You can't display your table inside <?php ?> you have to use echo the <div> part.

  3. I don't get the line: if( !( $insertRes = mysql_query( $insertSQL ) ) ){ What are you trying to do here if the return is true?

EDIT:

I'm not sure if you are trying to output the data from your db table into your html table, but I think this is what you are trying to achieve. If so, you can give this code a shot.

<?php
$sql = "SELECT * FROM your_table_here";
$result = mysql_query($sql); //execute query

while($arr = mysql_fetch_array($result)){ //Fetch query
    $TITOLO = mysql_real_escape_string($arr['TITOLO']);
    $AUTORE = mysql_real_escape_string($arr['AUTORE']);
    $EDITORE = mysql_real_escape_string($arr['EDITORE']);
    $ANNO = mysql_real_escape_string($arr['ANNO']);
    $ID = mysql_real_escape_string($arr['ID']);

    echo = "
        <div class='tbook' align='center'>
          <div class='tr'>
           <div class='td'>$TITOLO</div>
           <div class='td'>$AUTORE</div>
           <div class='td'>$EDITORE</div>
           <div class='td'>$ANNO</div>
           <div class='td'>$ID</div>
          </div>
        </div>
    ";
}
?>