-2

I have this PHP page which is supposed to be a list of employees with their positions..

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"         "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>View Records</title>
</head>
<body>
<?php
    // connect to the database
    include('connection.php');
?>
<?php
    // get results from database
    $result = "SELECT employees.id, CONCAT( fname, lname ) AS FullName,     employees.hphone, employees.cphone, employees.email, position.pos\n"
. "FROM employees\n"
. "INNER JOIN position ON employees.posid = position.id\n"
. "ORDER by employees.id ASC LIMIT 0, 30 ";     
        or die(mysql_error());  

    // display data in table
    echo "<p><b>View All</b> | <a href='view-paginated.php?page=1'>View Paginated</a></p>";

    echo "<table border='1' cellpadding='10'>";
    echo "<tr> <th>ID</th> <th>Employee Name/th> <th>Home Phone</th> <th>Cell Phone</th> <th>Email</th> <th>Position</th> </tr>";

    // loop through results of database query, displaying them in the table
    while($row = mysql_fetch_array( $result )) {

            // echo out the contents of each row into a table
            echo "<tr>";
            echo '<td>' . $row['employees.id'] . '</td>'
            echo '<td>' . $row['FullName'] . '</td>';
            echo '<td>' . $row['employees.hphone'] . '</td>';
            echo '<td>' . $row['employees.cphone'] . '</td>';
            echo '<td>' . $row['employees.email'] . '</td>';
            echo '<td>' . $row['position.pos'] . '</td>';                
            echo '<td><a href="edit.php?id=' . $row['employees.id'] . '">Edit</a></td>';
            echo '<td><a href="delete.php?id=' . $row['employees.id'] . '">Delete</a>    </td>';
            echo "</tr>"; 
    } 

    // close table>
    echo "</table>";
?>
<p><a href="drop.php">Add a new record</a></p>
</body>
</html> 

But I can only get a white page..no errors no nothing....can someone help me please..i am using linux mysql and PHP....i know the sql works because i can get the records from it via MyPHPAdmin..

Please help.

  • if you clear all code from that page, and just print a hello world string, does that work? – aiiwa Feb 06 '14 at 02:05
  • [How do I enable error handling in PHP?](http://stackoverflow.com/questions/6575482/how-do-i-enable-error-reporting-in-php) – ficuscr Feb 06 '14 at 02:07
  • Yes...the hello world works – user3251779 Feb 06 '14 at 02:08
  • Yeah, as ficuscr said, you have to enable your error reporting first and see if any error is thrown. – aiiwa Feb 06 '14 at 02:09
  • I added the error handling at the very top of the page and still get a blank white page..not even the page title shows in the tab up top.. – user3251779 Feb 06 '14 at 02:09
  • hey wait, what's that or die(mysql_error()); doing there.. how are you getting $result? – aiiwa Feb 06 '14 at 02:11
  • maybe it is the way the sql is structured...i copied from MyPHPAdmin with the show PHP code link.. – user3251779 Feb 06 '14 at 02:11
  • you need to execute $result in order to get the select result.. in your case.. its just a string, not querying database. – aiiwa Feb 06 '14 at 02:12
  • for a simple example you can see here http://www.w3schools.com/php/func_mysql_query.asp – aiiwa Feb 06 '14 at 02:13
  • in your case, see my answer – aiiwa Feb 06 '14 at 02:16
  • See my answer regarding error handling but your issue is you don't execute a query at top and pass that query result to mysql_fetch_array. runtime errors are not caught if declared at top of script, but a trick is to include those in another script you include in this file. The real way is configure .htaccess file or your php.ini file and turn errors on. – Mike S. Feb 06 '14 at 02:23
  • in the link to w3school....the connection does not reference a database name or a table...how does the code know where to look for the information – user3251779 Feb 06 '14 at 02:23
  • @user3251779 i have updated my answer, check it. – aiiwa Feb 06 '14 at 02:25
  • @user3251779 database name is in $con "my_db", which in your case, guess you have placed it in include('connection.php'); .. if yes, then no need to put that $con again. – aiiwa Feb 06 '14 at 02:27

2 Answers2

0

Try this:

$con=mysqli_connect("example.com","peter","abc123","my_db");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

    $sql = "SELECT employees.id, CONCAT( fname, lname ) AS FullName,     employees.hphone, employees.cphone, employees.email, position.pos\n"
    . "FROM employees\n"
    . "INNER JOIN position ON employees.posid = position.id\n"
    . "ORDER by employees.id ASC LIMIT 0, 30 ";     



    $result = mysqli_query($con,$sql);




// display data in table
    echo "<p><b>View All</b> | <a href='view-paginated.php?page=1'>View Paginated</a></p>";

    echo "<table border='1' cellpadding='10'>";
    echo "<tr> <th>ID</th> <th>Employee Name/th> <th>Home Phone</th> <th>Cell Phone</th> <th>Email</th> <th>Position</th> </tr>";

    // loop through results of database query, displaying them in the table
    while($row = mysqli_fetch_array($result)) {

            // echo out the contents of each row into a table
            echo "<tr>";
            echo '<td>' . $row['employees.id'] . '</td>';
            echo '<td>' . $row['FullName'] . '</td>';
            echo '<td>' . $row['employees.hphone'] . '</td>';
            echo '<td>' . $row['employees.cphone'] . '</td>';
            echo '<td>' . $row['employees.email'] . '</td>';
            echo '<td>' . $row['position.pos'] . '</td>';                
            echo '<td><a href="edit.php?id=' . $row['employees.id'] . '">Edit</a></td>';
            echo '<td><a href="delete.php?id=' . $row['employees.id'] . '">Delete</a>    </td>';
            echo "</tr>"; 
    } 

    // close table>
    echo "</table>";
aiiwa
  • 591
  • 7
  • 27
0

At first glance, you did not run $result = mysqli_query($conn, $some_query_string_here) and are calling or die() on a string declaration. Since we cannot see what is in your connection.php file we don't see what you do there, but your issue is you don't execute a query on the SQL string and assign that to result, you just assign the string to result.

To troubleshoot in future, you should enable errors on your server:

  1. Find the php.ini file on your computer/server and set these as shown:

    error_reporting = E_ALL & ~E_NOTICE | E_STRICT

    display_errors = On

  2. Restart your web server ( Apache or Nginx or PHP-FPM depending on how you set it up )

  3. View errors on the screen and fix them one at a time until it works.


Alternate way to enable error reporting is .htaccess file in your script directory or a parent dir with the following php_value error_reporting 0


If you cannot view errors then comment out the php lines in your code, save, then refresh your browser until page renders. The last thing you commented out is typically the culprit and then debug.

Mike S.
  • 4,806
  • 1
  • 33
  • 35