Show records from MySQL Database - php

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>Edit</td>';
echo '<td>Delete </td>';
echo "</tr>";
}
// close table>
echo "</table>";
?>
<p>Add a new record</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.

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>Edit</td>';
echo '<td>Delete </td>';
echo "</tr>";
}
// close table>
echo "</table>";

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:
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
Restart your web server ( Apache or Nginx or PHP-FPM depending on how you set it up )
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.

Related

How introduce anchor links when fetching a url from SQL database using PHP?

This is the PHP script and I can load my data in database but elements in the Link row seems not clickable .so that I redirect the user.
My attempt was to add anchor() tags similar to table definition but its still inactive.
<?php
$con=mysqli_connect($server,$user,$password,$dbname);
// Check connection
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM accident_log");
echo "<table border='1'>
<tr>
<th>ID</th>
<th>Link</th>
</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['ID'] . "</td>";
echo "<a><td>" . $row['Link'] . "</td></a>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
Learn here if you do not know https://www.w3schools.com
echo '<td>' . $row['link'] . '</td>';
echo '<td>' . $row['link'] . '</td>';

PHP code not working properly after first echo function

I've been trying to create a web application that functions like a library system, and I am currently working on building the table that will display all books in the system. However, after I close the first echo statement to start building the table in HTML, the app seems to just stop seeing it as code and prints the rest of the PHP code to the screen.
Here is the code in question:
<?php
$db = mysqli_connect('34.224.99.227','root','opendoor','library')
or die ('Could not connect to database');
$result = mysqli_query($db,'SELECT * FROM book');
echo '<table id="table_id" class="display">
<thead>
<tr>
<th>ISBN</th>
<th>Title</th>
<th>Author</th>
<th>Description</th>
<th>Status</th>
</tr>
</thead>
<tbody>';
while($row = mysqli_fetch_array($result)) {
echo '<tr>';
echo '<td>' . $row['isbn'] . '</td>';
echo '<td>' . $row['title'] . '</td>';
echo '<td>' . $row['author'] . '</td>';
echo '<td>' . $row['description'] . '</td>';
echo '<td>' . $row['status'] . '</td>';
echo '</tr>';
}
echo '</tbody></table>';
mysqli_close($db);
?>
And here is the corresponding web page output:
ISBN Title Author Description Status '; while($row = mysqli_fetch_array($result)) { echo ''; echo '' . $row['isbn'] . ''; echo '' . $row['title'] . ''; echo '' . $row['author'] . ''; echo '' . $row['description'] . ''; echo '' . $row['status'] . ''; echo ''; } echo ''; mysqli_close($db); ?>
I'm obviously not too well-versed in PHP, so I'm at a loss as to what the problem could be.
After playing around with the settings of my LAMP stack, I realized that I had apparently configured it with PHP 5 in mind, while I had downloaded PHP 7. Once I fixed the discrepancy, everything worked as intended. Thanks for help nonetheless.

Displaying MYSQL data using PHP & HTML

I'm a PHP beginner learner and I want to display all the results in a vertical way. For some reason, when there are multiple results, it just puts each result next to the other and not one over the other.
Any help will be highly appreciated.
This is my code
include "conexiondb.php";
if (isset($_POST['search'])) { // Search is the "name" attribute in the HTML input. The ones used with the "$_POST"
$busca = mysqli_real_escape_string($con, ($_POST['search'])) ;
$criteria = mysqli_real_escape_string($con, ($_POST['criteria'])) ;
if ($busca!="") {
$busqueda=$con->query("SELECT * FROM members WHERE {$criteria} = '{$busca}' ");
if ($busqueda === false) {
die('Could not connect:'.mysql_error()); // TODO: better error handling
}
}
echo "<div id ='tablennvoltura'><table border='1'>
<tr>
<th>Name</th>
<th>Address</th>
<th>Town</th>
<th>Zip</th>
<th>Cellphone</th>
<th>Birthday</th>
<th>Email</th>
<th>id</th>
<th>Editar</th>
<th>Nueva orden</th>
<th>Ver Ordenes</th>
</tr>";
while ($muestra=$busqueda->fetch_array()) {
echo '<td>'.$muestra['name'].' </td>';
echo '<td>' .$muestra['address']. '</td>';
echo '<td>' .$muestra['town']. '</td>';
echo '<td>' .$muestra['zip']. '</td>';
echo '<td>' .$muestra['cellphone']. '</td>';
echo '<td>' .$muestra['birthday']. '</td>';
echo '<td>' .$muestra['email']. '</td>';
echo '<td>' .$muestra['id']. '</td>';
echo "<td>"."See member"." </td>";
echo "<td>"."Nueva Orden"."</td>";
echo "<td>"."Ordenes"."</td>";
}
echo "</table></div>";
}
echo "</form>";
Because you place each variable into a table cell, but the cells are not encapsulated into a row (<tr>... </tr>).
while ($muestra=$busqueda->fetch_array()) {
echo '<tr>';
...
echo '</tr>';
}
Actually, this question does not have too much to do with php, it is purely an html issue.

How do I organize data in a PHP search query?

I'm in the process of making a PHP website, where a user can input data, then search through it later, but I can't seem to organize the data :/
Heres my code:
<form action="uploads.php" method="GET"><input id="search" type="text" placeholder="Type here"><input id="submit" type="submit" value="Search"></form></body></html>
When a user searched for their name, it results as so:
firstname=Mickey lastname=Mouse item1= item2= item3= item4= item5= item6=
Is there any way I can add a CSS or something to get the entries to line break or seperate?
In Your display page while displaying the data you can use the <br> tag so that it will display each and every data in different line.
First Name: <?php echo $loopvariable['fname'].'<br />'; ?>
Last Name: <?php echo $loopvariable['lname'].'<br />'; ?>
Like this you can provide for all the data which you print.
Output:
First Name: Name One
Last Name: Name Two
And you can provide as such information using the break tags in separate lines.
Basically in PHP you can echo html code so you can echo <br> statements as in the previous answer you can also echo css and you can create a block of code that you include
<?php
include('SomeMoreCode.php');
?>
You can also echo formatting commands to create a table. Just place the html statements in'' and join html and mysql/php values with .
echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>date</th> <th>Home Team</th> <th></th><th></th><th>Away Team</th> <th></th> </tr>";
while($row = mysql_fetch_array( $result )) {
echo "<tr>";
echo '<td>' . $row['date'] . '</td>';
echo '<td>' . $row['hometeam'] . '</td>';
echo '<td>' . $row['fthg'] . '</td>';
echo '<td>' . $row['ftag'] . '</td>';
echo '<td>' . $row['awayteam'] . '</td>';
echo '<td>Edit</td>';
echo "</tr>";
}
echo "</table>";
non mysql - php only
<?php
$date='19/6/16';
$hometeam='Man United';
$fthg='3';
$ftag='2';
$awayteam='Man City';
echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>date</th> <th>Home Team</th> <th></th><th></th><th>Away Team</th> </tr>";
echo "<tr>";
echo '<td>' . $date . '</td>';
echo '<td>' . $hometeam . '</td>';
echo '<td>' . $fthg . '</td>';
echo '<td>' . $ftag . '</td>';
echo '<td>' . $awayteam . '</td>';
echo "</tr>";
echo "</table>";
?>

Display the result by selection each column from a html table using php

Is it possible to have filtering option in each column of html table in which the data is fetched from mysql as we do in excel sheet?
<html>
<?php
// connect to the database
include('connect-db.php');
$result = mysql_query("SELECT * FROM log WHERE type='c' ") ;
echo "<div style='height:auto;'><table border='0' cellpadding='10' class='table table-striped table-bordered'>";
echo "<tr> <th>ID</th> <th>Date</th> <th>Type</th> <th>Batch name</th> <th>No.of Pages</th> <th>User</th><th>file</th><th>Status</th><th></th><th></th></tr>";
// loop through results of database query, displaying them in the table
while($row = mysql_fetch_array( $result )) {
$id=$row['batch_file'];
// echo out the contents of each row into a table
echo "<tr>";
echo '<td>' . $row['id'] . '</td>';
echo '<td>' . $row['assdate'] . '</td>';
echo '<td>' . $row['type'] . '</td>';
echo '<td>'.substr($row['batch_file'], 0, -4).'</td>';
echo '<td>' . $row['no_pages'] . '</td>';
echo '<td>' . $row['assign'] . '</td>';
echo '<td><a href="copy_test_uploads/'.$row['batch_file'].'"target="_blank"><img src="pdf.png" style=width:35px;></td>';
echo '<td>' . $row['status'] . '</td>';
echo '<td>Edit</td>';
echo '<td>Delete</td>';
echo "</tr>";
}
// close table>
echo "</table></div>";
?>
</html>

Categories