Trouble outputting PHP text - php

I seem to be having issue outputting a line of PHP.
<?php
include 'db_connect.php';
{
$query = "SELECT INSERT_DATE,LINE_TEXT FROM DATA_TAB order by RAND() LIMIT 1";
}
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
mysql_close($con);
?>
<table >
<tbody>
<tr>
<td><?php
$insert_date = strtotime($row['INSERT_DATE']);
echo date("j F Y", $insert_date); ?></td>
</tr>
<tr>
<td><?php
$line_text = $row['LINE_TEXT'];
echo $line_text; ?></td>
</tr>
</tbody>
</table>
The output of $line_text doesn't appear to be displaying. Any ideas?

Related

Absolutely confused, how come this particular query is not working?

I do not understand because I can delete comments so far. However, when I attempt to update them, it does not update in the database. What is even more shocking to me is there are no error messages explaining why. Is there a particular reason for this?
<table class="table table-bordered table-hover">
<thead>
<tr>
<td>Id</td>
<td>Post Author</td>
<td>Author Email</td>
<td>Post Content</td>
<td>In Response To</td>
<td>Post Status</td>
<td>Date</td>
<td>Approve</td>
<td>Disapprove</td>
</tr>
</thead>
<tbody>
<?php
if(isset($_GET['delete'])) {
$comId = $_GET['delete'];
$deleteComment = $conn->prepare("
DELETE FROM comments
WHERE comment_id = ?
");
$deleteComment->bind_param("i", $comId);
$deleteComment->execute();
$deleteComment->close();
}
if(isset($_GET['approve'])) {
$comIdApp = $_GET['approve'];
$approveStat = 'approved';
$approveComment = $conn->prepare("
UPDATE comments
SET comment_status = ?
WHERE comment_id = ?
");
$approveComment->bind_param("si", $approveStat, $commIdApp);
$approveComment->execute();
$approveComment->close();
}
?>
<?php
$post_output = $conn->query("SELECT * FROM comments");
while($row = $post_output->fetch_assoc()) {
$commentId = $row['comment_id'];
$postCommentId = $row['post_comment_id'];
$commentAuthor = $row['comment_author'];
$authorEmail = $row['author_email'];
$commentContent = $row['comment_content'];
$commentStatus = $row['comment_status'];
$commentDate = $row['comment_date'];
?>
<tr>
<td><?php echo($commentId); ?></td>
<td><?php echo($commentAuthor); ?></td>
<td><?php echo($authorEmail); ?></td>
<td><?php echo($commentContent); ?></td>
<?php
$postCommentRelate = $conn->query("SELECT * FROM posts WHERE post_id = {$postCommentId}");
while($row = $postCommentRelate->fetch_assoc()) {
$postTitle = $row['post_title'];
?>
<td><?php echo($postTitle); ?></td>
<?php } ?>
<td><?php echo($commentStatus); ?></td>
<td><?php echo($commentDate); ?></td>
<td><a href='admin_comments.php?approve=<?php echo($commentId); ?>'>Approve</a></td>
<td><a href='admin_comments.php?disapprove=<?php echo($commentId); ?>'>Disapprove</a></td>
<td>DELETE</td>
<td><a href=''>EDIT</a></td>
</tr>
<?php } ?>
</tbody>
</table>
Your variables are spelled differently $comIdApp vs $commIdApp
$comIdApp = $_GET['approve'];
$approveStat = 'approved';
$approveComment = $conn->prepare("UPDATE comments SET comment_status = ? WHERE comment_id = ?");
$approveComment->bind_param("si", $approveStat, $commIdApp);

Pagination in while loop with php

I'm newbee about php,trying to learn.
I've search other topic about while loop pagination but not satisfied.
I have 70 user records in my database,wants to list them with html table. I'm showing all records with this code. How can i make simple pagination with these codes? Please help me.
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Surname</th>
<th>Email</th>
<th>Password</th>
<th>Date</th>
<th>Gender</th>
</tr>
</thead>
<tbody>
<?php
$q = "SELECT * FROM users ORDER BY uid ASC";
$r = mysqli_query($dbc,$q);
while($userlist = mysqli_fetch_assoc($r)){ ?>
<tr>
<td><?php echo $userlist['uid']; ?></td>
<td><?php echo $userlist['name']; ?></td>
<td><?php echo $userlist['surname']; ?></td>
<td><?php echo $userlist['email']; ?></td>
<td><?php echo $userlist['password']; ?></td>
<td><?php echo $userlist['date']; ?></td>
<td><?php echo $userlist['gender']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
The follow provide very simple pagination as a starting point. You will need to supply formatting for the pagination and such.
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Surname</th>
<th>Email</th>
<th>Password</th>
<th>Date</th>
<th>Gender</th>
</tr>
</thead>
<tbody>
<?php
$q = "SELECT count(*) as `numrows` FROM `users` ORDER BY `uid` ASC";
$c = mysqli_query($dbc,$q);
if($c) {
if($t = mysqli_fetch_assoc($c)) {
$numrows = $t['numrows'];
}
}
$numrows = 0;
$rowsperpage = 10;
$currpage = isset($_REQUEST['currpageno']) && $_REQUEST['currpageno'] != 0 ? $_REQUEST['currpageno'] : 1;
$numpages = ceil($numrows / $rowsperpage);
$startrow = ($currpage - 1) * $rowsperpage;
if($startrow > $numrows) {
$startrow = $numrows - $rowsperpage;
}
if($startrow < 0) {
$startrow = 0;
}
$q = "SELECT * FROM `users` ORDER BY `uid` ASC LIMIT ".$startrow.",".$rowsperpage.";";
$r = mysqli_query($dbc,$q);
while($userlist = mysqli_fetch_assoc($r)){
?>
<tr>
<td><?php echo $userlist['uid']; ?></td>
<td><?php echo $userlist['name']; ?></td>
<td><?php echo $userlist['surname']; ?></td>
<td><?php echo $userlist['email']; ?></td>
<td><?php echo $userlist['password']; ?></td>
<td><?php echo $userlist['date']; ?></td>
<td><?php echo $userlist['gender']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
<div id='pagination'>
<?php
for($pgno = 1;$pgno <= $numpages;$pgno++) {
echo "<a class='' href='?currpageno=".$pgno."'>".$pgno."</a>";
}
?>
</div>

PHP not displaying last column in table

I am trying to display all the info in the table but when I query the information then put it into a PHP table it doesn't show any data in the last table.
Here is my PHP code for the table
<table border='1'>
<tr>
<th>Ticket ID</th>
<th>Username</th>
<th>Message</th>
<th>Date</th>
<th>Error #</th>
<th>Priority</th>
</tr>
<?php
if(!$query){
die('Invalid query: ' .mysql_error());
}
while ($row = mysql_fetch_array($query)) { ?>
<tr>
<td><?php echo $row['TicketID']; ?></td>
<td><?php echo $row['Message']; ?></td>
<td><?php echo $row['Date']; ?></td>
<td><?php echo $row['Error']; ?></td>
<td><?php echo $row['Priority']; ?></td>
</tr>
<?php } ?>
</table>
Here is the PHP code for query
<?php
$server = mysql_connect("localhost", "root", "**password**");
$db = mysql_select_db("minecraft", $server);
$query = mysql_query("SELECT * FROM tickets");
?>
All of my row names are correct but it doesn't want to put the data into that column.
Here is my table structure
You Omitted
<td><?php echo $row['Username']; ?></td>
that should be after
<td><?php echo $row['TicketID']; ?></td>

Query always returns recently added data only

I'm trying to show all my data from the database I made in mysql.
I am using this code:
<table border= "3">
<tr>
<th>ID</th>
<th>Game Name</th>
</tr>
<?php
$query = "SELECT * FROM `test_game_name`";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)) {
$id = $row['game_id'];
$name = $row['game_name'];
}
?>
<tr>
<td><?php echo $id; ?></td>
<td><?php echo $name; ?></td>
</tr>
</table>
My problem is that not all the data show up, only the data I recently added. I believe that SELECT * means selecting all the data.
But I don't know what's the problem why it does not show all the data, anyone would happen to know?
You need to add your td inside your while loop
<?php
$query = "SELECT * FROM `test_game_name`";
$result = mysql_query($query);
?>
<tr>
<?php
while ($row = mysql_fetch_array($result)) {
$id = $row['game_id'];
$name = $row['game_name'];
echo " <td>" . $id . "</td>";
echo " <td>" . $name . "</td>";
}
?>
</tr>
Note:- mysql is deprecated instead use mysqli and PDO
Try using this:
<table border= "3">
<tr>
<th>ID</th>
<th>Game Name</th>
</tr>
<?php
$query = "SELECT * FROM `test_game_name`";
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{
$id = $row['game_id'];
$name = $row['game_name'];
?>
<tr>
<td><?php echo $id; ?></td>
<td><?php echo $name; ?></td>
</tr>
<?php } ?>
</table>
add this inside while,
<tr>
<td><?php echo $id; ?></td>
<td><?php echo $name; ?></td>
</tr>
Final Code
while($row = mysql_fetch_array($result)) {
$id = $row['game_id'];
$name = $row['game_name']; ?>
<tr>
<td><?php echo $id; ?></td>
<td><?php echo $name; ?></td>
</tr>
<?php } ?>
Switch to mysqli_* or PDO instead of mysql_* which is deprecated.
If you are not connected with database then follow this code it will help you.
<table border= "3">
<tr>
<th>ID</th>
<th>Game Name</th>
</tr>
<?php
$link=mysql_connect("localhost", "root","");
mysql_select_db('dbname', $link);
$query = "SELECT * FROM `test_game_name`";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)) {?>
<tr>
<td><?php echo $row['game_id']; ?></td>
<td><?php echo $row['game_name']; ?></td>
</tr>
<?php }
?>
</table>

echo items from mysqli_fetch_array into html table td tags not showing up

Here is my code, I get the items to show but nothing will echo in the tags. I tried just removing the php echo statements and just try writing in some text but still nothing. Thanks in advance
<?php
//Create a connection
$connect = mysqli_connect('localhost', 'root', 'bachi619', 'company');
//check connection
if(mysqli_connect_errno($connect)){
echo 'Failed to connecto to database'.mysqli_connect_error();
}
$result= mysqli_query($connect, "SELECT * FROM employees");
?>
<br>
<table width="500", cellpadding=5 callspacing=5 border=1>
<tr>
<th>ID</th>
<th>Name</th>
<th>Last Name</th>
<th>Department</th>
<th>Email</th>
</tr>
<?php while($rows = mysqli_fetch_array($result)): ?>
<tr>
<td><?php echo $rows['id']; ?></td>
<td><?php echo $rows['first_name']; ?></td>
<td><?php echo $rows['last_name']; ?></td>
<td><?php echo $rows['department']; ?></td>
<td><?php echo $rows['email']; ?></td>
</tr>
<?php endwhile; ?>
</table>
You're missing PHP in several places:
<?php
$connect = mysqli_connect('localhost', 'root', '11111', 'company');
if(mysqli_connect_errno($connect)){
echo 'Failed to connecto to database'.mysqli_connect_error();}
$result= mysqli_query($connect, "SELECT * FROM employees");
?>
<table width="500", cellpadding=5 callspacing=5 border=1>
<tr>
<th>ID</th>
<th>Name</th>
<th>Last Name</th>
<th>Department</th>
<th>Email</th>
</tr>
<?php while($rows = mysqli_fetch_array($result)): ?>
<tr>
<td><?php echo $rows['id']; ?></td>
<td><?php echo $rows['first_name']; ?></td>
<td><?php echo $rows['last_name']; ?></td>
<td><?php echo $rows['department']; ?></td>
<td><?php echo $rows['email']; ?></td>
</tr>
<?php endwhile; ?>
</table>
I used to have the same issue. At first I thought mysqli_fetch_array() couldn't work inside tag. But now it finally did. Anyways, after checking your codes have you tried removing the : in <?php while($rows = mysqli_fetch_array($result)): ?>
Because as far as I know you don't need a ; or a : if you're calling a function inside while-loop or any loop function.
Try this:
while($row=mysqli_fetch_assoc($result))

Categories