PHP connecting with SQL and displaying data - php

HI, All
I have created (copied) a website that can view the information inside my database. But the code i have, displays every single bot as well as members.
I am connecting it to local host and the database is phpbb3.
I need a sting to get rid of the bots which have a group_id of 6.
Here is my code
<!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
/*
VIEW.PHP
Displays all data from 'phpbb_users' table
*/
// connect to the database
include('connect-db.php');
// get results from database
$result = mysql_query("SELECT * FROM phpbb_users")
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>Username</th> <th>Email</th> <th></th> <th></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['user_id'] . '</td>';
echo '<td>' . $row['username'] . '</td>';
echo '<td>' . $row['user_email'] . '</td>';
echo '<td>Edit</td>';
echo '<td>Delete</td>';
echo "</tr>";
}
// close table>
echo "</table>";
?>
<p>Add a new record</p>
</body>
</html>
Thank you,
In advanced.

Change this:
$result = mysql_query("SELECT * FROM phpbb_users")
To
$result = mysql_query("SELECT * FROM phpbb_users WHERE group_id <> 6")

Related

converting php code to html

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.
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>
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>
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>
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

MYSQL records not displaying in PHP/HTML Table

I am trying to display MySQL Records in a HTML Table. I have gone through the process and in my php document it displays the tables name, description etc. but the data from my database doesn't display.
Here is my code:
<?php
//establishing connection
mysql_connect('localhost', 'root', 'root');
//selecting a database
mysql_select_db('festival');
$sql="SELECT * FROM deejay";
$records=mysql_query($sql);
?>
<html>
<head>
<title>PHP Test</title>
</head>
<body>
<h2>Festival Diagram</h2>
<img src="diagram.jpg" alt="diagram">
<h1>Deejay Data</h1>
<table width="300" border="1" cellpadding="10" cellspacing="1">
<tr>
<th>dj_id</th>
<th>name</th>
<th>country</th>
<th>info</th>
</tr>
<?php
while($deejay=mysql_fetch_assoc($records)){
echo "<tr>";
echo "<td>" .$records['dj_id']."</td>";
echo "<td>" .$records['name']."</td>";
echo "<td>" .$records['country']."</td>";
echo "<td>" .$records['info']."</td>";
echo "</tr>";
}
?>
</table>
</body>
</html>
When you do run code here: $deejay=mysql_fetch_assoc($records) you are assigning $deejay to be the current row item (the next row in the table that you got from $records). In other-words $records is still the entire retrieved data and $deejay is the current row you are on. So you need to say: $deejay['...'] instead of $record['...'].
echo "<td>".$deejay['dj_id']."</td>";
echo "<td>".$deejay['name']."</td>";
echo "<td>".$deejay['country']."</td>";
echo "<td>".$deejay['info']."</td>";
try this:
while($deejay=mysql_fetch_assoc($records)){
echo "<tr>";
echo "<td>" .$deejay['dj_id']."</td>";
echo "<td>" .$deejay['name']."</td>";
echo "<td>" .$deejay['country']."</td>";
echo "<td>" .$deejay['info']."</td>";
echo "</tr>";
}

PHP page with embedded HTML displaying extra column

I've a php page with embedded HTML and I'm displaying data from a MySQL database. PHP is echoing the html inside the php page. All of the data is being returned; however, the data table is being displayed with an extra column and the data that should be in the last column is displayed in the extra column (e.g. my last name is 'Last Name,' but there is an extra column after 'Last Name' with the 'last name' data).
What am I doing wrong here?
Thanks.
get_records.php
//make connection
$conn = mysql_connect('localhost', 'root', '');
//select db
mysql_select_db('kis');
if (!$conn) {
die("Can not connect: " . mysql_error());
}
//select db and run query
mysql_select_db('kis');
$sql = "SELECT * FROM users";
$records = mysql_query($sql);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" type="text/css" href="css/TableCSSCode.css" media="all"/>
<title>Volunteer Data</title>
</head>
<body>
<div class="CSSTableGenerator">
<h1>Volunteer Records</h1>
<table>
<tr>
<th>First Name</th>
<th>Middle Name</th>
<th>Last Name</th>
</tr>
<tr>
<?php
//loop through the records and display in page
while ($users = mysql_fetch_assoc($records)) {
echo "<tr>";
echo "<td>" . $users['firstname'] . "</td>";
echo "<td>" . $users['middlename'] . "<td>";
echo "<td>" . $users['lastname'] . "<td>";
echo "</tr>";
}//end while
?>
</tr>
</table>
</div>
<!--end #dr_container-->
</body>
</html>
You need to close the td's
echo "<td>" . $users['middlename'] . "</td>";
echo "<td>" . $users['lastname'] . "</td>";
You have not properly closed your td tags:
echo "<td>" . $users['middlename'] . "<td>";
echo "<td>" . $users['lastname'] . "<td>";
It should be </td> at the end.
You're echoing the row tags (<tr>). So, don't add additional ones in the plain html (just around your PHP code).
Close "td" tag.
Remove "tr" tags before and after where php code start because there are already tr tags inside the php code.
Following is the updated HTML of table:
<table>
<tr>
<th>First Name</th>
<th>Middle Name</th>
<th>Last Name</th>
</tr>
<?php
//loop through the records and display in page
while ($users = mysql_fetch_assoc($records)) {
echo "<tr>";
echo "<td>" . $users['firstname'] . "</td>";
echo "<td>" . $users['middlename'] . "</td>";
echo "<td>" . $users['lastname'] . "</td>";
echo "</tr>";
}//end while
?>

Show records from MySQL Database

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.

Adding AJAX Search Filter to Results - PHP/MySQL

I'm trying to create a page that displays all the data from my database, but I'd like the ability to live filter the data through a search box, eliminating rows that don't match the query, almost exactly like DataTables. I can't use DataTables, however, because I'm finding it difficult to add the custom columns I have. I know how to make a search page that then displays results but not how to search within results.
Any help would be appreciated. Thanks.
<!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('connect-db.php');
// get results from database
$result = mysql_query("SELECT * FROM Orders")
or die(mysql_error());
// display data in table
echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>Date</th> <th>Name</th> <th>Phone</th> <th>Location</th> <th></th> <th></th></tr>";
while($row = mysql_fetch_array( $result )) {
$src = '';
switch( $row['Location'] ) {
case '1':
$src = '1.jpg';
break;
case '2':
$src = '2.jpg';
break;
default:
$src = 'default.jpg';
} // echo out the contents of each row into a table
echo "<tr>";
echo '<td>' . $row['date'] . '</td>';
echo '<td>' . $row['Name'] . '</td>';
echo '<td>' . $row['Phone'] . '</td>';
echo '<td><img src="'.$src.'"></td>';
echo '<td>Edit</td>';
echo '<td>Delete</td>';
echo "</tr>";
}
// close table>
echo "</table>";
?>
<p>Add a new record</p>
</body>
</html>
Using jQuery for client side table search

Categories