0

I don't understand why this isn't working, I have been stuck on this for ages and tried lots of different alternatives, but it just doesn't print the data from the database.

At the moment I am just trying to get the id to print, but eventually I want to print most of the data in the database (not including the hash).

Here is my code:

<!DOCTYPE html>
<html>
    <head>
        <title>Staroids Leaderboard</title>
    </head>
    <body>
    <table border=1px>
        <thead>
            <tr>
                <td>name</td>
                <td>score</td>
            </tr>
        </thead>
        <tbody>
        <?php
            $connect = mysql_connect("localhost","root", "password");
            if (!$connect) {
                die(mysql_error());
            }
            mysql_select_db("staroids");
            $results = mysql_query("SELECT id FROM scores");
            while($row = mysql_fetch_array($results)) {
            $name = $row['id']
            ?>
                <tr>
                    <td><?php echo '$name'?></td>

                </tr>

            <?php
            }
            ?>
        </tbody>
        </table>
    </body>
</html>

The image below shows what it looks like in html:

enter image description here

This image shows the database in local host and as you can see there is lots of data, but none of the names seem to print?!

enter image description here

4 Answers 4

3

Correct your syntax where it could be

$name = $row['id'];   //Put ; here
<?php echo $name;?>   //Remove quotes and put ;

Select name from DB and you can get name then.It should be

$results = mysql_query("SELECT id,name FROM scores");
while($row = mysql_fetch_array($results)) {
    $name = $row['name'];
?>
   <td><?php echo $name;?></td>

And dont use mysql_* functions due to they are deprecated.Instead use mysqli_* functions or PDO statements.

And as @Nedstark said use try die(mysql_error()); for the errors regarding the mysql errors.

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

5 Comments

And definitely it shouldn't be mysql_* as its deprecated.
Nope, still outputs nothing :(
I have change them all to mysqli_... and it still isn't working
try die(mysql_error()); .It will print any mysql error if any
Cheers, solved my problem! I will accept the answer in 6 minutes
1
<td><?php echo $name;?></td>

or use

 <td><?php echo "$name";?></td>  <!--(Bad idea but works)->

Variables work in double quotes("") not in single quotes('')

Comments

1
  <?php
    session_start();
   if (!(isset($_SESSION['UserName'])))
    {
      echo "<script type=\"text/javascript\">alert('Unauthorize user are redirected to Login page');".
  header('Location:http://localhost/campus');

  }

  include_once "connect.php";
  $find = mysql_query("YOUR SELECT STATEMENT ") or die('error');
  ob_start();
 ?>
 <!DOCTYPE html>
 <html>
    <head>   
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>ST. MICHAEL's COLLEGE ILIGAN CITY</title> 
    <style>
        AlignJst  {
                    text-align:justify;
                    text-justify:inter-word;
                  }

         pTable   {
                    margin:2cm 4cm 3cm 4cm;
                  }

        body {color: black; font-size: 10px; font-family: Helvetica, Arial, non-serif;}
        a:link {color: #FF8C00;} 
        a:visited {color: #FF8C00;}
        a:hover {color: #FF8C00; background: #ADD8E6; text-decoration:none;}
        a:active {color: #FF0000;}
        p {line-height: 2em;
            font-size:85%;
            color:black;
            letter-spacing: 0.3em
          }

       h1 {
            font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;
            font-size: 12pt;
            color: navy;
            padding-top: 12px;
            padding-bottom: 3px;
          }

    </style>

    </head>
<body>
                      <?php

                                                     echo "<CENTER>"."<H1>TABLE TITLE</H>" . "<BR />";
                                                     echo "<H1>SUBTITLE</H>"."</CENTER>"."<BR/>";

                                                     echo "<CENTER>"."<p>"."<b>" . "PAST MORNING PRAYER SCHEDULE" ."</b>"."</p>"."</CENTER>"."<BR/>";
                                                     echo "<table border='1' width='100%' align ='center'>";
                                                     echo "<tr>";
                                                              echo "<th>SPONSOR NAME</th>";
                                                              echo "<th>VENUE </th>";
                                                              echo "<th>DATE EVENT</th>";
                                                              echo "<th>TIME </th>";

                                                           while($row = mysql_fetch_array($find)){
                                                              echo "<tr>";
                                                              echo "<td>".$row['sponsor_name']."</td>";
                                                              echo "<td>".$row['Venue']."</td>";
                                                              echo "<td>".$row['Date_Event']."</td>";
                                                               echo "<td>".$row['Time_Event']."</td>";

                                                              echo "</tr>";
                                                                            }
                                                            echo "</table>";
                                                            echo "<br />". "<br />" ."<br />";
                                                            echo "<p align = 'right'>"."Prepared By:" . $_SESSION['UserName'] ."</p>";
                                                        ?>



</body>

1 Comment

try that one just change everything to your table value
0

Other than changing mysql ==> msqli, I'd recommend a couple of debug strategies, in this case I would:

  • fix the echo, you need to pick one of those two options:

    <?php echo $variable; ?>
    <?php echo "this is my variable {$variable}"; ?>
    

If you put a single quote PHP don't parse the content of what is about to print, it just print it as text, so what you have should print $name in the HTML...but, since I don't see any $name text in the black screenshot I think you might even not get into that loop...

  • a good debug strategy would be to query for something broader, i.e. "SELECT * FROM `scores`", then you can do a

    <?php print_r($row); ?>
    

right after while ($row = mysql_fetch_array($results)) {

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.