7

I'm trying to get the data from my mySQL database and put them into a HTML table. After searching a lot on the internet, but I coudn't find code that worked for me.

Currently, I have this code

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
    <table>
        <thead>
            <tr>
                <td>Naam</td>
                <td>Gemeente</td>
                <td>Datum</td>
            </tr>
        </thead>
        <tbody>
       <?php
          $db_select = mysql_select_db($dbname,$db);
            if (!db_select) {
                die("Database selection also failed miserably: " . mysql_error());
            }
            mysql_select_db("databaseiheko");
            $results = mysql_query("SELECT NaamFuif, GemeenteFuif, DatumFuif FROM tblfuiven");
            while($row = mysql_fetch_array($results)) {
            ?>
                <tr>
                    <td><?php echo $row['NaamFuif']?></td>
                    <td><?php echo $row['GemeenteFuif']?></td>
                    <td><?php echo &row['DatumFuif']?></td>
                </tr>

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

The only thing that I get is the first row of my table (Naam-Gemeente-Datum). Am I doing something wrong or did I forgot something?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Matt
  • 1,893
  • 11
  • 33
  • 57
  • 1. Use `mysql_error()` to check, whether there were any problems with your query. 2. `mysql_` functions are deprecated. You should switch to [PDO](http://php.net/pdo) or [`mysqli`](http://php.net/mysqli). – Sirko Jan 27 '13 at 14:04
  • 1. What you ar doing with the db not really safe, try to include the database connecting. 2. You have a typo at Datumfuif ->&row. 3. Are you sure the db is filled? – Jonathan Römer Jan 27 '13 at 14:04
  • 1
    You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). – Quentin Jan 27 '13 at 14:05
  • `` You may wish to use a `$` sign instead. – Antony Jan 27 '13 at 14:24
  • @Sirko I don't have any errors after including mysql_error(). I'm looking to switch to PDO. – Matt Jan 27 '13 at 14:24
  • 1
    Replace ``!db_select`` with ``!$db_select``. Replace ``&row`` with ``$row``. – crackmigg Jan 27 '13 at 14:24
  • @JonathanRomer How can I include the database? The typo is fixed, but that didn't solve my problem. And I'm sure my db is filled. – Matt Jan 27 '13 at 14:24
  • I think its better to check ot some tutorials, who give ou an step by step explanation. Because if i give you the answer im sure you will run into an other probbln in 5 mins. – Jonathan Römer Jan 27 '13 at 14:26
  • Also to see these syntax errors in the browser, activate ``error_reporting(E_ALL);`` on development code version (remove in production version, as also your ``die`` statement, which does not help the site user). – crackmigg Jan 27 '13 at 14:26
  • Where is ``$dbname``and ``$db`` defined? – crackmigg Jan 27 '13 at 14:28
  • What does `mysql_num_rows($results)` tell you? – Antony Jan 27 '13 at 14:29
  • I have added a working pastebin example as a comment to my answer. – That Brazilian Guy Jan 27 '13 at 15:10

3 Answers3

4

First of all, the most important thing to keep in mind is:

You are using deprecated and unsecure code

The mysql_ functions are strongly discouraged, for various reasons:

  • Are deprecated and will be removed in future versions of PHP,
  • Are insecure leading to possible SQL injections,
  • Lack many features present in more current versions of PHP

See the linked question for much more in-depth explanations.

Now, to the code itself:

You are not using mysql_connect to connect to the server

You should use mysql_connect to specify the server, the username and the password that will be used to access the data in the database. From your code, it seems that it was supposed to be present, because there's a $db variable used in the mysql_connect function, but not properly initialized nor used again anywhere else.

You should use mysql_connect in a way similar to this:

$db = mysql_connect('localhost', $user, '$password');
if (!$db) {
    die('Not connected : ' . mysql_error());
}

(Don't forget to set your username and password!)

You are using mysql_select_db twice in a row:

    $db_select = mysql_select_db($dbname,$db);
        if (!db_select) {
            die("Database selection also failed miserably: " . mysql_error());
        }

followed by

mysql_select_db("databaseiheko");

  1. Note the $dbname and $db variables, you don't have them on your code, this function won't work like this.
  2. The second mysql_select_db overwrites the first, but you don't specify a server connection to be used.

You should use the first version, but you should use mysql_connect before it.

You have typos in your code

  • if (!db_select) { should be if (!$db_select) {
  • echo &row['DatumFuif'] should be echo $row['DatumFuif']
Community
  • 1
  • 1
That Brazilian Guy
  • 3,328
  • 5
  • 31
  • 49
2

mysql_ functions are deprecated, but if you want to use them, i suggest these corrections:

You can correct your code this way: the mysql connect is needed:

 <?php
 //connect to your database
 mysql_connect("serverIpAddress","userName","password");
 //specify database
 mysql_select_db("yourDatabaseName") or die;
 //Build SQL Query
 $query = "select * from tblfuiven";
 $queryResult=mysql_query($query);
 $numrows=mysql_num_rows($queryResult);

numrows will contain the number of found records in the db. add an echo for the number of rows and let us know if the number of rows is still one. Then use mysql_fetch_assoc to get rows:

  while($row = mysql_fetch_assoc($queryResult)) {
        ?>
            <tr>
                <td><?php echo $row['NaamFuif']?></td>
                <td><?php echo $row['GemeenteFuif']?></td>
                <td><?php echo &row['DatumFuif']?></td>
            </tr>
        <?php

        }
        ?>

EDIT: You can test the code and let us know the number of rows that you obtain, using this code(write your real user name and password and db name:

         <?php
         mysql_connect("localhost","root","root");     
         mysql_select_db("databaseName") or die;
            $results = mysql_query("SELECT * FROM tblfuiven");
            $numrows=mysql_num_rows($queryResult);
           while($row = mysql_fetch_assoc($queryResult)) {
            ?>
                <tr>
                    <td><?php echo $numrows ?></td>
                    <td><?php echo $row['NaamFuif']?></td>
                    <td><?php echo $row['GemeenteFuif']?></td>
                    <td><?php echo $row['DatumFuif']?></td>
                </tr>

            <?php
           }
           ?>
Gaucho
  • 1,328
  • 1
  • 17
  • 32
  • After adding your code, it still doesn't work. Current code --> http://pastebin.com/p7J4rZek – Matt Jan 27 '13 at 14:44
  • @Matt in my answer there is a question. Could i know the value of $numrows ? – Gaucho Jan 27 '13 at 14:46
  • @Matt in mysql_connet you used $dbhost etc., within quotes. Write directly the string within quotes, for example "localhost" instead of "$dbhost" – Gaucho Jan 27 '13 at 14:50
  • When I echo something, I don't get an output (example: echo 'Hello World"). Even after directly writing the string, I get the same results. – Matt Jan 27 '13 at 15:10
  • @Matt Then probably your php.ini settings are different from the usual – That Brazilian Guy Jan 27 '13 at 15:12
  • 1
    @Matt, add the row near the other 3 td and watch the output on the webpage. – Gaucho Jan 27 '13 at 15:19
  • @Matt use this http://pastebin.com/JmuecfGh and write the correct user name, password and database name. Why you use variables that you didn't declared? – Gaucho Jan 27 '13 at 15:28
0

Your problem is in the php echo statement. It is missing ";".

BSMP
  • 4,596
  • 8
  • 33
  • 44
dave
  • 1