0

I have been having hard time trying to convert my php code to html for 5 hours and at this point, I'm really burned out :X. Here's my Php code

 <?php
  $con=mysqli_connect("localhost","dbuser","pw","dbname");
 // Check connection
 if (mysqli_connect_errno())
   {
   echo "Failed to connect to MySQL: " . mysqli_connect_error();
   }

 $result = mysqli_query($con,"SELECT * FROM tablea");

 echo "<table border='1'  >
 <tr>
 <th>id</th>
 <th>Subject_Code</th>
 <th>date</th>
 <th>name</th>
 <th>description</th>
 <th>Other_Details</th>
 </tr>";

 while($row = mysqli_fetch_array($result))
   {
   echo "<tr>";
   echo "<td>" . $row['id'] . "</td>";
   echo "<td>" . $row['Subject_Code'] . "</td>";
   echo "<td>" . $row['date'] . "</td>";
   echo "<td>" . $row['name'] . "</td>";
   echo "<td width='600' class='table_width'>" . $row['description'] . "</td>";
   echo "<td width='600' class='table_width' align='center'>" . $row['Other_Details'] . "</td>";
   echo "</tr>";
   }
 echo "</table>";

 mysqli_close($con);
 ?> 

and here's what i have done so far for HTML

<!doctype html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <title>database connections</title>
    </head>
    <body>
      <?php
      $username = "dbuser";
      $password = "pw";
      $host = "localhost";
      $database= "dbname";

      $connector = mysql_connect($localhost,$dbuser,$pw,dbname)
          or die("Unable to connect");
        echo "Connections are made successfully::";
      $selected = mysql_select_db("test_db", $connector)
        or die("Unable to connect");

      //execute the SQL query and return records
      $result = mysql_query("SELECT * FROM tablea ");
      ?>
      <table border="2">
      <thead>
        <tr>
          <th>id</th>
          <th>Subject_Code</th>
          <th>date</th>
          <th>name</th>
          <th>description</th>
          <td>Other_Details</td>
        </tr>
      </thead>
      <tbody>

        <?php
    while ($row = mysql_fetch_array($result)) {
        ?>
        <tr>
            <td><?php echo $row['id']; ?></td> 
            <td><?php echo $row['Subject_Code']; ?></td> 
            <td><?php echo $row['date']; ?></td> 
            <td><?php echo $row['name']; ?></td>
            <td><?php echo $row['description']; ?></td>
            <td><?php echo $row['Other_Details']; ?></td>
        </tr>

     <?php mysql_close($connector); ?>
    </body>
    </html>

little Help here please, Thanks !!

EDIT: seems like some people are not understanding my question. My php is working fine so i want to convert the php code into html. Basically, i want my table from database to show up using HTML table.

13
  • help to do what? what are you asking? Commented Oct 1, 2016 at 9:15
  • what is your problem with code . what you want achieve ? Commented Oct 1, 2016 at 9:20
  • 2
    mysql_* is deprecated try to use mysqli_* or PDO Commented Oct 1, 2016 at 9:30
  • My php is working fine so i want to convert the php code into html. Basically, i want my table from database to show up using HTML table. Commented Oct 1, 2016 at 9:36
  • is there any error ? Commented Oct 1, 2016 at 9:42

4 Answers 4

1

you not close while;

so change code to :

    <!doctype html>
        <html lang="en">
        <head>
          <meta charset="UTF-8">
          <title>database connections</title>
        </head>
        <body>
          <?php

/////////////////////////////////// change \\\
          $username = "dbuser";
          $password = "pw";
          $host = "localhost";
          $database= "dbname";

          $connector = mysql_connect($localhost,$username,$password)
              or die("Unable to connect");
            echo "Connections are made successfully::";
          $selected = mysql_select_db($database, $connector)
            or die("Unable to connect");

    /////////////////////////////////// end change \\\

          //execute the SQL query and return records
          $result = mysql_query("SELECT * FROM tablea ");
          ?>
          <table border="2">
          <thead>
            <tr>
              <th>id</th>
              <th>Subject_Code</th>
              <th>date</th>
              <th>name</th>
              <th>description</th>
              <td>Other_Details</td>
            </tr>
          </thead>
          <tbody>

            <?php
        while ($row = mysql_fetch_array($result)) :
            ?>
            <tr>
                <td><?php echo $row['id']; ?></td> 
                <td><?php echo $row['Subject_Code']; ?></td> 
                <td><?php echo $row['date']; ?></td> 
                <td><?php echo $row['name']; ?></td>
                <td><?php echo $row['description']; ?></td>
                <td><?php echo $row['Other_Details']; ?></td>
            </tr>
         <?php endwhile;?>
         <?php mysql_close($connector); ?>
        </body>
        </html>
Sign up to request clarification or add additional context in comments.

1 Comment

change connect db for not mach php code.pls,chack code
0

Actually your problem is as you said like html version there is no variable like $localhost but your passing the parameter to connect mysql etc. below error code shows the variable mismatch of your code.

Error code :

<?php
  $username = "dbuser";
  $password = "pw";
  $host = "localhost";
  $database= "dbname";

  $connector = mysql_connect($localhost,$dbuser,$pw,dbname);
                             ^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^

  //here there is no variable like what you passing to connect mysql that's problem here 
  ?>

solution :

         <!doctype html>
        <html lang="en">
            <head>
          <meta charset="UTF-8">
          <title>database connections</title>
        </head>
        <body>

    <?php
      $con=mysqli_connect("localhost","dbuser","pw","dbname");
     // Check connection
     if(mysqli_connect_errno())
       {
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
       }

     $result = mysqli_query($con,"SELECT * FROM tablea");

     echo "<table border='1'  >
     <tr>
     <th>id</th>
     <th>Subject_Code</th>
     <th>date</th>
     <th>name</th>
     <th>description</th>
     <th>Other_Details</th>
     </tr>";

      while($row = mysqli_fetch_array($result))
       {
            echo "<tr>";
           echo "<td>" . $row['id'] . "</td>";
           echo "<td>" . $row['Subject_Code'] . "</td>";
           echo "<td>" . $row['date'] . "</td>";
           echo "<td>" . $row['name'] . "</td>";
           echo "<td width='600' class='table_width'>" . $row['description'] . "</td>";
           echo "<td width='600' class='table_width' align='center'>" . $row['Other_Details'] . "</td>";
           echo "</tr>";
       }
     echo "</table>";

     mysqli_close($con);

     ?>

       </body>
        </html>

2 Comments

just tried that and it just shows the code instead, not even a table :X.
what is file name with extenstion filename.php or filename.html ? @JessicaLim
0

Here's another version that makes use of PDO - by far the superior of php connectors in terms of maintainability and versatility. I have the same code working under MySQL, SQLite and SQLServer - the only thing that changes is the connection string.

You'll note I pull all of the results at once. I also make use of the fact that we get back an array. Each element of that array is a row. Each row is an array of cells. This greatly reduces the code needed to output a result-set. We just need two forEach loops and that's it.

<?php
    $host = 'localhost';
    $userName = 'dbuser';
    $password = 'pw';
    $nameOfDb = 'dbname';
    $nameOfTable = 'tablea';

    $pdo = new PDO('mysql:host='.$host, $userName, $password);
    $pdo->query("use " . $nameOfDb);

    // desired results requested explicitly, so when we get an array for the row's data, the elements will be in the same order
    // - not required if the order of the columns in the database matches the desired display order. (we could just use 'select *' then)
    //$query = $pdo->prepare('select * from '.$nameOfTable);
    $query = $pdo->prepare('select id, Subject_Code, date, name, description, Other_Details from '. $nameOfTable);

    $query->execute();
    $query->setFetchMode(PDO::FETCH_ASSOC);
    $rows = $query->fetchAll();
?><!doctype html>
<html>
<head>
</head>
<body>
    <table>
        <thead>
            <tr>
                <th>id</th><th>Subject_Code</th><th>date</th><th>name</th><th>description</th><th>Other_Details</th>
            </tr>
        </thead>
        <tbody><?php
                forEach($rows as $curRow)
                {
                    echo '<tr>';

                        // use this one if you want to display the columns in the same order they are returned in the query results
                        // AND you'd like to display all columns in the result
                        forEach($curRow as $curCell)
                            echo '<td>'.$curCell.'</td>';

                        // otherwise, you can request the desired elements by name
                        //
                        //  echo '<td>' . $curCell['id'] . '</td>'
                        //  echo '<td>' . $curCell['Subject_Code'] . '</td>'

                    echo '</tr>';
                }
            ?></tbody>
    </table>
</body>

Comments

0

Download HTTrack Website Copier and install and run the soft. Run your script and paste your URL in web copier software. You will get HTML output in your desire folder

Comments

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.