Using two Tables to show SQL data on one Screen - php

I am still battelling to show data from two different tables on one screen. The tables both contain one common field name, all i want is to filter on that field name and be able to alter and display details from that line or lines based on that field name they share.
The two tables are:
members and recipients
In members it has a primary key member_id and it is unique. Then in recipients I created a member_id field that needs to link to the members table unique member_id as well.
The following code calls information from members table
<?php
$con = mysql_connect("localhost", "****", "****");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("stingin_epanic", $con);
$result = mysql_query("SELECT * FROM members WHERE member_msisdn='$slusername'");
echo "<table border='1'>
<tr>
<th>Membership</th>
<th>Number</th>
<th>Registration Date</th>
<th>End Date</th>
<th>Copy of ID</th>
</tr>";
while ($row = mysql_fetch_array($result)) {
echo "<td><center>" . $row['member_id'] . "</td>";
echo "<td><font color=blue>" . $row['member_msisdn'] . "</td>";
echo "<td><center>" . $row['asdate'] . "</td>";
echo "<td><center>" . $row['aedate'] . "</td>";
echo "<td><center>" . $row['attid'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
This works great and gives me what i need. I am also calling member_id here to show the primary key.
On same page i am trying to call information related to the member from another table using the following code. Note that the information is supposed to be linked to the member_id of the first code
<?php
$con = mysql_connect("localhost", "****", "****");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("stingin_epanic", $con);
$result = mysql_query("SELECT * FROM members a INNER JOIN recipients b ON member_id = member_id WHERE member_msisdn=$slusername");
echo "<table border='1'>
<tr>
<th>Name</th>
<th>Number</th>
</tr>";
while ($row = mysql_fetch_array($result)) {
echo "<td><font color=blue>" . $row['recipient_name'] . "</td>";
echo "<td>" . $row['recipient_msisdn'] . "</td>";
echo "</tr>";
echo "<td><font color=blue>" . $row['recipient_name'] . "</td>";
echo "<td>" . $row['recipient_msisdn'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
Using the second code i get no results as it is calling no information

Use mysqli instead of mysql functions
Use aliases in You SQL queries.
change this
"SELECT * FROM members a INNER JOIN recipients b ON member_id = member_id WHERE member_msisdn=$slusername"
on this
"SELECT * FROM members a INNER JOIN recipients b ON a.member_id = b.member_id WHERE member_msisdn=$slusername"
Maybe this will help.

Related

Trouble with Queries and JOINS

The main page I am using is called the Project Details Page which when you select a project number on the form will query the subform for any records pertaining to that project number and display them in this page named the (TasksSubform).
This page called (TasksSubform) uses a php file called mysqli_connect.php to obtain a database connection and assigns that connection to $dbc in the mysqli_connect.php file.
This page then query’s table 1 named 'CommonTasks', and starts displaying the data row by row in a table on the page using
while($row = $result->fetch_assoc())
Currently one of the columns in the record being displayed is named “AssignedTo” which produces the unique ID number in the Employees table instead of the text value of the employees name associated with the ID number. So, I need to be able to list the records in the CommonTasks Table using Fetch then, when it tries to display the value in the “AssignedTo” column within the Common Tasks Table, it must lookup the ID in the Employees table which equals the same value in the Common Tasks Table, and replace the number value of the Assigned To Field with the text value in the Employees table.
COMMONTASKS
EMPLOYEES
* Add
* AssignedTo
* Attachments
* Cost
* CostInDays
* Description
* DueDate
* EmployeeID
* ID
* PercentComplete
* Priority
* StartDate
* SubmissionDate
* Title * ID
* Address
* BusinessPhone
* City
* Company
* CountryRegion
* EmailAddress
* FaxNumber
* FirstName
* HomePhone
* JobTitle
* LastName
* MobilePhone
* Notes
* StateProvince
* WebPage
* ZIPPostal Code
This is what I have. Yet, all it is producing is a blank in the Assigned To field on the php page.
enter image description here
I am a novice to php and mysql.
This is probably something simple which I am overlooking.
Yet, I have been troubleshooting various methods for the past few days, and just cant seem to figure out what I am doing wrong.
<?php
// Get a connection for the database
require_once('../mysqli_connect.php');
// Create a query for the database
$sql = "SELECT * FROM `CommonTasks`";
$employee = "SELECT ID, LastName, LastName FROM Employees JOIN CommonTasks ON Employees.ID=CommonTasks.AssignedTo";
$emp = "SELECT LastName, FirstName FROM Employees JOIN CommonTasks WHERE Employees.ID = CommonTasks.AssignedTo LIMIT 1";
$emp1 = "SELECT id as LastName, FirstName FROM Employees WHERE ID = CommonTasks.AssignedTo LIMIT 1";
// Get a response from the database by sending the connection
// and the query
$result1 = #mysqli_query($dbc, $sql);
$result2 = #mysqli_query($dbc, $emp);
$result = $dbc->query($sql);
$link = "commntasks-insertdata.php"
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Common Tasks-subform</title>
<meta name="viewport"charset="utf-8" content="width=device-width, initial-scale=1.0">
</head>
<body>
<?php
echo " <table border='1' #6a8fba>
<caption>SUBFORM - Common Tasks</caption>
<tr>
<th>Job Title</th>
<th>Due Date</th>
<th>Start Date</th>
<th>Cost</th>
<th>Priority</th>
<th>Percent Complete</th>
<th>Assigned To</th>
<th>Description</th>
</tr>";
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td><a href= $link > $row[Title] </a></td>";
echo "<td>". $row['DueDate'] . "</td>";
echo "<td>". $row['StartDate'] . "</td> " ;
echo "<td>". $row['Cost'] . "</td>";
echo "<td>". $row['Priority'] . "</td>";
echo "<td>". $row['PercentComplete'] . "</td> " ;
echo "<td>". $row ['SELECT LastName, FirstName FROM Employees JOIN CommonTasks WHERE Employees.ID = $_GET[AssignedTo] LIMIT 1'] . "</td>";
echo "<td>". $row['Description'] . "</td> " ;
echo "</tr>";
}
}
echo "</table>";
?>
</body>
Currently, the results are being produced by this line
$result = $dbc->query($sql);
The following line will not execute a mysql query.
echo "<td>". $row ['SELECT LastName, FirstName FROM Employees JOIN CommonTasks WHERE Employees.ID = $_GET[AssignedTo] LIMIT 1'] . "</td>";
As written, you are trying to find a row value in $result that does not exist. You need to call the second query within the first while loop and pass the value of $_GET[AssignedTo] which is probably $row[AssignedTo]
Something like this
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td><a href= $link > $row[Title] </a></td>";
echo "<td>". $row['DueDate'] . "</td>";
echo "<td>". $row['StartDate'] . "</td> " ;
echo "<td>". $row['Cost'] . "</td>";
echo "<td>". $row['Priority'] . "</td>";
echo "<td>". $row['PercentComplete'] . "</td> " ;
$emp = "SELECT LastName, FirstName FROM Employees JOIN CommonTasks WHERE Employees.ID = '$row[AssignedTo]' LIMIT 1";
$result2 = #mysqli_query($dbc, $emp);
$row2 = $result2->fetch_assoc();
echo "<td>". $row2 ['Firstname'] . " ". $row2 ['Lastname'] . "</td>";
echo "<td>". $row['Description'] . "</td> " ;
echo "</tr>";
}
So, Here are my revisions to your example:
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td><a href= $link > $row[Title] </a></td>";
echo "<td>". $row['DueDate'] . "</td>";
echo "<td>". $row['StartDate'] . "</td> " ;
echo "<td>". $row['Cost'] . "</td>";
echo "<td>". $row['Priority'] . "</td>";
echo "<td>". $row['PercentComplete'] . "</td> " ;
$emp = "SELECT LastName, FirstName FROM Employees JOIN CommonTasks WHERE Employees.ID = '$row[AssignedTo]'";
$result2 = #mysqli_query($dbc, $emp);
$row2 = $result2->fetch_assoc();
echo "<td>". $row2['LastName']," , ",$row2[FirstName] . "</td>";
echo "<td>". $row['Description'] . "</td> " ;
echo "</tr>";
}
}
echo "</table>";
?>
Which produces this: Results of modified code
Thank you so much for your Help!
Since I am converting an Access Database to mySQL and recreating all of the queries, forms, and reports.... I am sure I will have an overwhelming amount of questions in the near future.
Eric

In php-sql, I want to re-search within the first search result table.

In php-sql, I want to re-search within the first search result table.
This is the captured picture executed by my php-mysql code.
I want to use the button "Search below result" to gain the detailed result from the first search result table.
Then, "Search in below result" form action is another php code which has to hold the first result, and have the sql code that is as like
select uid, contents from datatable where contents like '%re-search word%'
and uid in (select uid from datatable where contents like '%first-search word%')
but I have a question and don't know how uid works.
How can i produce uid?
What is uid?
Where is uid information?
How can i use uid as like above the sql code?
Below is my first-search php code
<?php
$q = $_GET['q'];
$con = mysqli_connect('localhost','root','autoset','my_db');
if (!$con)
{
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"ajax_demo");
$sql="SELECT * FROM persons WHERE FirstName = '".$q."' ;
$result = mysqli_query($con,$sql);
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "<td>" . $row['Age'] . "</td>";
echo "<td>" . $row['Hometown'] . "</td>";
echo "<td>" . $row['Job'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
and How can i re-use the first-search word variable in the another php code?
Another php code is designed as like below
<?php
$q = $_GET['q'];
$p = $_GET['LastName'];
$con = mysqli_connect('localhost','root','autoset','my_db');
if (!$con)
{
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"ajax_demo");
$sql="SELECT uid, * FROM persons WHERE LastName = '".$p."' and select **uid** in (select uid from datatable where FirstName='".$q."') ;
$result = mysqli_query($con,$sql);
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "<td>" . $row['Age'] . "</td>";
echo "<td>" . $row['Hometown'] . "</td>";
echo "<td>" . $row['Job'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
Above code, I think uid is important to recall the first search result.
But How can i get uid and set uid in php code or html code??
Please help me!
Q1: How can i produce uid?
A1: It should be a column in your table you created in mysql. Your create table statement would have looked something like:
CREATE TABLE persons (
uid INT(10) NOT NULL AUTO_INCREMENT,
FirstName CHAR(30) NOT NULL,
LastName CHAR(30) NOT NULL,
Age INT(3),
Hometown CHAR(40),
Job CHAR(40),
PRIMARY KEY (uid)
);
Q2: How can i produce uid?
A2: You won't have to. The AUTO_INCREMENT field will create itself for you when you insert an entry. doco for auto-increment here
Q3: What is uid?
A3: It likely stands for "user identification" which is a number unique to a user. No other user may have that number in their "uid" field.
Q4: Where is uid information?
A4: It's in your table
Q5: How can i use uid as like above the sql code?
A5: Providing that you created it in your "create table" statement like I mentioned in A1 then you should be able to access uid with the query you presented above but should look more like:
SELECT uid FROM persons WHERE LastName = '".$p."' AND FirstName ='".$q."';
OR if you want to run a test query with a name you know is in your database then something like below:
SELECT uid FROM persons WHERE LastName = 'timmy' AND FirstName ='tom';

php mysql select from columns from 2 tables Join

I have two tables.
visitors_details, with id,scanner_id,time columns
and visitors_info with scanner_id, name,surname columns
I want to get back
id,name,surname,time in a table
i have written this but is not working
$result = mysql_query("SELECT visitors_details.id AS id,
visitors_info.name AS name, visitors_info.surname AS surname, visitors_details.time
AS time FROM visitors_details AS d LEFT JOIN visitors_info AS i ON
d.scanner_id=i.scanner_id ");
echo "<table border='1'>
<tr>
<th>id</th>
<th>name</th>
<th>surname</th>
<th>Time</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['surname'] . "</td>";
echo "<td>" . $row['time'] . "</td>";
echo "</tr>";
}
echo "</table>";
any ideas??
Its better to enable some debugging for your code like this:
<?php
error_reporting(E_ALL);
$sql = "
SELECT d.id AS id, i.name AS name, i.surname AS surname, d.time AS time
FROM visitors_details AS d
LEFT JOIN visitors_info AS i ON d.scanner_id=i.scanner_id
";
$result = mysql_query($sql);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
?>
try this query
$result = mysql_query("SELECT d.id , i.name , i.surname , d.time
FROM visitors_details AS d LEFT JOIN visitors_info AS i
ON d.scanner_id=i.scanner_id ");
Add this to catch errors. saves a lot of time:
if(!$result) {
echo mysql_error();
}

Mysql Query Group By Display Using PHP

I want to display a table that looks like this from a Mysql query using PHP:
Vendor Name
Item Not to Drawing Item Defective Incorrect Item Received Other
9 2 3 5
Vendor Name
Item Not to Drawing Item Defective Incorrect Item Received Other
2 4 5 7
etc..
Here is my code....
<?php
$con = mysql_connect("localhost","xxx","xxx");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("qa", $con);
$result = mysql_query("SELECT Vendor, Sum(draw), Sum(defective), Sun(received), Sum(other) FROM qa_reports Group by Vendor Order by Vendor ASC");
echo "<table>
<tr>
<th>Item not to Drawing</th>
<th>Item Defective</th>
<th>Incorrect Item Received</th>
<th>Other</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['Sum(draw)'] . "</td>";
echo "<td>" . $row['Sum(defective)'] . "</td>";
echo "<td>" . $row['Sum(received'] . "</td>";
echo "<td'>" . $row['Sum(other)'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
Not sure how to get the above results formatted as such.
I've done this with CFML but am new to PHP and can not grasp how to list results in a table grouped by a field.
You need to alias your columns in order to reference them in your $row array.
SELECT vendor,
Sum(draw) AS draw,
Sum(defective) AS defective,
Sun(received) AS received,
Sum(other) AS other
FROM qa_reports ...
Then you can reference them like so:
$row['draw'];
...
SELECT Vendor, Sum(draw) AS sumDraw, Sum(defective) AS sumDefective ...
$row['sumDraw'] etc.

expects parameter

here im getting problem with my join query..i dont know where is the problem whether my query is wrong or whatelse but the error its giving is
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\project\teacher\courses-list.php on line 46
heres my code please mend the code i cudnt find the problem.. :(
courses-list.php
<?php
if ($_SESSION["isteacher"])
{
$con=mysql_connect($dbserver,$dbusername,$dbpassword);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db($dbname, $con);
$result = mysql_query("SELECT courses.id AS cid, courses.title, courses.description, courses.subjects-id, subjects.id AS sid, subjects.subjectname AS sname FROM courses, subjects WHERE (courses.subjects-id==subjects.id)");
echo "<table border='1'> <br />
<tr>
<th>ID:</th>
<th>Course Name</th>
<th>Description</th>
<th>Subject-ID</th>
<th>EDIT</th>
<th>DELETE</th>
</tr>";
while($row = mysql_fetch_array($result)) // this is the error line
{
echo "<tr>";
echo "<td>" . $row['cid'] . "</td>";
echo "<td>" . $row['title'] . "</td>";
echo "<td>" . $row['description'] . "</td>";
echo "<td>" . $row['sname'] . "</td>";
echo "<td><a href='courses-edit.php?id=" . $row['id']."'>EDIT</a></td>";
echo "<td><a href='courses-delete.php?id=" . $row['id']."'>DELETE</a></td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
here is the error
$result = mysql_query("SELECT courses.id AS cid, courses.title, courses.description, courses.subjects-id, subjects.id AS sid, subjects.subjectname AS sname FROM courses, subjects WHERE (courses.subjects-id==subjects.id)");
should be
$result = mysql_query("SELECT courses.id AS cid, courses.title, courses.description, courses.subjects-id, subjects.id AS sid, subjects.subjectname AS sname FROM courses, subjects WHERE (courses.subjects-id=subjects.id)");
the error portion is
subjects WHERE (courses.subjects-id==subjects.id)");
here is the error ---------^^--------sould be =
also please avoid even dont use the mysql_* even the php manual show the message about that use the mysqli or PDO

Categories