mysqli_fetch_assoc() not working, unable to enter loop - php

I have been trying to display the data from a table onto a page. The table is called events and the database is school.
As far as I know, I'm doing everything right, I've stored the query in a string, run it using mysqli_query() and later put the echo statements in a while loop while placing $row=mysqli_fetch_assoc($result) inside the parenthesis. Yet when I run it not even a single entry is displayed on the page.
Here's my entire code:
<?php
require("includes/common.php");
$query = "SELECT name,place,date FROM school.events";
$result = mysqli_query($con, $query)or die(mysqli_error($con));
?>
<!DOCTYPE html>
<!--
Calendar Page for Trinity High School
-->
<html>
<head>
<title>Events Calendar</title>
<?php
require 'includes/external.php';
?>
</head>
<body>
<div class="content">
<?php
include 'includes/header.php';
?>
<div class="container">
<table class="table">
<thead>
<tr>
<th>Date</th>
<th>Event</th>
<th>Place</th>
</tr>
</thead>
<tbody>
<?php
while ($row = mysqli_fetch_assoc($result)) {
?>
<tr>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['place']; ?></td>
<td><?php echo $row['date']; ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
</div>
</body>
</html>
PS: Using some trial and error I found out that the program simply doesn't enter the while loop. Perhaps there's something wrong with the condition?

Related

Unable to delete a row from website with php

Hi im making Crud app with php mysqli procedural language.i was able to retrieve and insert data but unable to delete. when i clicked on delete link it goes to delete.php page but nothing happens.
db.php
<?php
$conn = mysqli_connect('localhost', 'root','123','app');
?>
index.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>CRUD</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" integrity="sha384-PsH8R72JQ3SOdhVi3uxftmaW6Vc51MKb0q5P2rRUpPvrszuE4W1povHYgTpBfshb" crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js" integrity="sha384-alpBpkh1PFOepccYVYDB4do5UnbKysX5WZXm3XxPqe5iKTfUKjNkCk9SaVuEZflJ" crossorigin="anonymous"></script>
</head>
<body>
<table class="table table-bordered ">
<tr>
<th>id</th>
<th>Email</th>
<th>Fisrtname</th>
<th>Lastname</th>
<th>Delete</th>
<th>Edit</th>
</tr>
<?php
include 'db.php';
$query ="select *from people";
$result = mysqli_query($conn,$query);
while($row = mysqli_fetch_assoc($result)){
?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['email']; ?></td>
<td><?php echo $row['firstname']; ?></td>
<td><?php echo $row['lastname']; ?></td>
<td><a href='delete.php?id=<?php echo $row['id']; ?>'>Delete</a></td>
<td><a href='edit.php?id=<?php echo $row['id']; ?>'>Edit</a></td>
</tr>
<?php
}
?>
<div class="container">
<i class="glyphicon glyphicon-plus"></i> Add Records
</div>
</body>
</html>
delete.php
<?php
include 'db.php';
if (isset($_GET('id'))) {
$delete_id = $_GET['id'];
$query = "delete from people where id = '$delete_id'";
$run = mysqli_query($conn,$query);
if ($run) {
header("Location:index.php");
}
}
?>
You have a typo in delete.php on line 4.
In GET array you have to use [] brackets around key name instead of ()
if (isset($_GET['id'])) {...}

First row in database not displaying in table with either mysql_fetch_row or mysql_fetch_assoc

Though these questions have already been answered and i have tried to follow those, yet the first row from mysql database is not getting displayed in the table. (I have three rows)
Following is my code. Will be very kind of you if you can help. Thank You.
<?php
require_once 'dbreg.php';
?>
<!DOCTYPE html>
<head>
<title>User Listing</title>
</head>
<body>
<table border="1">
<tr>
<td>Firstname</td>
<td>Lastname</td>
<td>Gender</td>
<td>Address</td>
<td>Country</td>
<td>Actions</td>
</tr>
<?php
$query = mysql_query("SELECT * FROM regform");
if(mysql_fetch_row($query) > 0) {
while ($result = mysql_fetch_assoc($query)) {
?>
<tr>
<td><?php echo $result['firstname'];?></td>
<td><?php echo $result['lastname'];?></td>
<td><?php echo $result['gender'];?></td>
<td><?php echo $result['address'];?></td>
<td><?php echo $result['country'];?></td>
<td>View / Edit</td>
</tr>
<?php
}
}
?>
</body>
</html>
"if(mysql_fetch_row($query) > 0) {" line is reading already the result-set moving the cursor forward so when code execution gets to the "while ($result = mysql_fetch_assoc($query)) {" line it is already at second row in result-set.
You should remove the "if line" because while will act exactly as you expect.

Grouping table with data headers

Hi everyone im attempting to group my data table to display the users listed by department eg:
IT DEPARTMENT
Name Phone Email Extension
Bob 99393 ksand#sda 8484
but mine is displayed as this:
Name Phone Email Extension Department
Bob 99393 ksand#sda 8484 IT
I did some reading on datatable grouping but coulodnt find anything that explains how it works and how to implement it.
My code is below. If someone could point me to a link to help that would be greatly appreciated, thank you.
<?php
require_once"connection.php";
$contacts = array();
$all_contacts = "select * from contacts where contact_status = '1'";
$sql_all_contacts = $conn->query($all_contacts);
$total_contacts = $sql_all_contacts->num_rows;
while ($row = mysqli_fetch_assoc($sql_all_contacts)) {
$contacts[] = $row;
}
?>
<html>
<head>
<?php include"includes/head.inc"; ?>
</head>
<body>
<div class="wrapper">
<!-- header section -->
<?php include"includes/header.inc"; ?>
<!-- content section -->
<div class="content">
<div class="floatl"><h1><?php echo $total_contacts ?> Contact(s) Total</h1></div>
<a class="floatr" href="insert_contact.php"><input class="cancel_contact_button" type="button" value="New Contact"></a> $
<div class="clear"></div>
<hr class="pageTitle">
<table border ="1" style="width:100%" id="contactsTable" class="display">
<thead>
<tr align="left">
<th>Name:</th>
<th>Email:</th>
<th>Department:</th>
<th>Extension:</th>
<th>Cellphone:</th>
<th>Actions</th>
</tr>
</thead>
<tr align="left">
<th>Name:</th>
<th>Email:</th>
<th>Department:</th>
<th>Extension:</th>
<th>Cellphone:</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php foreach ($contacts as $contact) {?>
<tr>
<td><?php echo $contact["name"];?></td>
<td><?php echo $contact["email"]; ?></td>
<td><?php echo $contact["department"]; ?></td>
<td><?php echo $contact["extension"]; ?></td>
<td><?php echo $contact["cellphone"]; ?></td>
<td><a href="update_contact.php?id=<?php echo $contact["contact_id"]; ?>"><i class="fa fa-p$
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
</body>
</html>
Since you're already doing a while loop to add the db rows into an array, you could use the Department as the key. This would group the contacts by department, and you could run nested foreach loops.
while ($row = mysqli_fetch_assoc($sql_all_contacts)) {
$contacts[$row['Department']][] = $row;
}
The foreach loops:
foreach ($contacts as $department => $contactGroup) {
echo $department;
foreach($contactGroup as $contact) {
// output your contact here
}
}

How to debug a MySQL error in PHP?

I have code that normally don't have any problem with but somehow the MySQL query part (marked with an arrow ------>) is stopping the code.
The MySQL command is fine and the mysql_connection is working, I can ping it.
I'm working all the time like that, I just can't find the error, and since the code is completely stopping I can't fetch any error message.
<?php
//including the mysql_connection
include("../mechanics/mysql_con.php");
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="../mechanics/segments_container.css"/>
</head>
<body>
<div class = "container">
<div class = "titlebar">
Members
</div>
<div class = "inner_container">
<center>
<table style="color:#CCCCCD;width:100%;">
<tr>
<td style="min-width:50px;text-align:center;"><b>Avatar</b></td>
<td style="min-width:150px;text-align:center;"><b>Username</b></td>
<td style="min-width:50px;text-align:center;"><b>Joined</b></td>
<td style="min-width:50px;text-align:center;"><b>Posts</b></td>
<td style="min-width:50px;text-align:center;"><b>Reputation</b></td>
</tr>
<?php
$mysql_get_users = "SELECT avatar, name, registerdate, posts, reputation FROM members";
-----> $data = $mysqli->query($mysql_get_users);
-----> while($row = $data->fetch_array()){
?>
<tr>
<td><?php echo $row["avatar"]; ?></td>
<td><?php echo $row["name"]; ?></td>
<td><?php echo $row["registerdate"]; ?></td>
<td><?php echo $row["posts"]; ?></td>
<td><?php echo $row["reputation"]; ?></td>
</tr>
<?php
}
?>
</table>
</center>
</div>
</div>
</body>
</html>
I think I found the answer.
Because the file memberlist.php itself is a include from the index.php, the directory wasn't ../mechanics/mysql_con.php instead it was mechanics/mysql_con.php
I guess because of the including the path started from index and not the it's folder segments (where memberlist.php is in)
Thanks for every second. :)

echo the any particular client to particular project

I have created a simple Business Management setup for office work. First I fetch all information from database. All is going well except the quantity input.
i creat a simple client-Payment System in php. in this first Add any client it's doing simple and all client show at a simple page when click a client name then this page go on the project page when add new project name then all project are show on this page. I want the perticualr client name to pericular project show
<?php session_start(); include("sidebar.php"); include "config.php"; $sel="select * from new_project"; $rs=mysql_query($sel); ?>
<div class="h_right">
<h1> +ADD New Project </h1>
</div>
<div class="h_left">
<table border="2px" cellpadding="10px">
<tr>
<td>Id</td>
<td>Project Name</td>
</tr>
<?php while($rec=mysql_fetch_assoc($rs)) { ?>
<tr>
<td><?php echo $rec['id'] ?></td>
<td><a href="projectdetail.php?><?php echo $rec['project_name'] ?></a></td>
<?php } ?>
</table>
</div>
Use a foreach loop:
<?php session_start(); include("sidebar.php"); include "config.php"; $sel="select * from new_project"; $rs=mysql_query($sel); ?>
<div class="h_right">
<h1> +ADD New Project </h1>
</div>
<div class="h_left">
<table border="2px" cellpadding="10px">
<tr>
<td>Id</td>
<td>Project Name</td>
</tr>
<?php foreach($rec=mysql_fetch_assoc($rs) as $project) { ?>
<tr>
<td><?php echo $rec['id'] ?></td>
<td><?php echo $project['project_name'] ?></td>
<?php } ?>
</table>
</div>
new_project.php doesn't need a project id
projectdetail.php will display project based on the project id passed

Categories