0

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)

5
  • can you make a fiddle example with fake db data? Commented Nov 25, 2016 at 9:13
  • @JarlikStepsto jsfiddle update in question Commented Nov 25, 2016 at 9:20
  • still dont got, what is wrong. How should it looks like and what is happening now? please define your problem more clear Commented Nov 25, 2016 at 9:26
  • HTML display SQL query into the table and CSS class won't work as it is defined to(example: i write "background-color:red;" but it display transparent with no margin left etc etc...) Commented Nov 25, 2016 at 9:29
  • do you have a link to your page? jsfiddle.net/krzv2wkq/1 Commented Nov 25, 2016 at 9:31

2 Answers 2

2

Make sure that you close the open <?php tag with an ?> before the HTML section, otherwise it won't be rendered correctly.

Sign up to request clarification or add additional context in comments.

1 Comment

After these two lines # Execute the INSERT Query if( !( $insertRes = mysql_query( $insertSQL ) ) ){ There is a ?> missing.
0

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

5 Comments

I'm trying to insert datas into arrays and then add them into table if queries values are OK. Most of lines code are part of old programming(and a big part of my actually issue).
So the data flow looks like this From DB -> Arrays -> Table output? Am i getting it right? And btw you are missing a } of the line if( !( $insertRes = mysql_query( $insertSQL ) ) ){ and a closing php tag ?>
This is the method i need to create the output by using PHP but this question is, at the moment, not useful anymore.
Can you perhaps show us the structure of your database table at least?
Whay you mean? SQL exports or the table rows?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.