displaying a data from database with 3 table joining together [duplicate] - php

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Join 3 tables to display certain data PHP-MSSQL
I have these tables and I want to get certain datas for user to view and be able to POST to other page
I cant post images so I have to break this down, so please bear with me
1st table
- dbo.users
- pkey(UserID)
- EmployeeName
2nd table
- dbo.PC
- pkey(PCID)
- PC_Number
3rd table
- dbo.FA_PC
- pkey(FAID)
- fkey(UserID)
- fkey(PCID)
<?php
ini_set("display_errors","on");
$conn = new COM("ADODB.Connection");
try {
$myServer = "WTCPHFILESRV\WTCPHINV";
$myUser = "sa";
$myPass = "P#ssw0rd";
$myDB = "wtcphitinventory";
$connStr = "PROVIDER=SQLOLEDB;SERVER=".$myServer.";UID=".$myUser.";PWD=".$myPass.";DATABASE=".$myDB;
$conn->open($connStr);
if (! $conn) {
throw new Exception("Could not connect!");
}
}
catch (Exception $e) {
echo "Error (File:): ".$e->getMessage()."<br>";
}
if (!$conn)
{
exit("Connection Failed: " . $conn);
}
$sql_exp = "select * from dbo.users";
$rs = $conn->Execute($sql_exp);
echo "<select name='empf'>";
while (!$rs->EOF) {
set_time_limit(0);
echo "<option value=".$rs->Fields('UserID')." >".$rs->Fields('EmployeeName')."</option>";
$rs->MoveNext();
}
$rs->Close();
?>
How could I display the PC_Number of the currently selected $rs->Fields('UserID') in the same form and still be able to post it on printd.php

Try INNER JOINS
select A.*,B.*,C.* from table1 A INNER JOIN Table2 B ON A.userid=B.userid
INNER JOIN table3 C ON C.pcid=B.pcid where A.userid='2'
Good luck

Related

Can I throw multiple json from a single page?

I want to throw multiple json from this page so that I an fetch json file to show the output for my mobile application with java programming. Following is my code which displays proper json from table "news" however, I want to throw json of other object available in my database. Is that possible?
$dblink = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
mysqli_query($dblink, 'SET NAMES utf8');
//Check connection was successful
if ($dblink->connect_errno) {
printf("Failed to connect to database");
exit();
}
$result = $dblink->query("SELECT * FROM news ORDER BY id DESC");
$dbdata = array();
while ( $row = $result->fetch_assoc()) {
$dbdata[]=$row;
}
echo json_encode($dbdata);
?>
It's up to you how you combine them, it could be as simple as:
Merge:
$dbdata = array_merge($dbdata1, $dbdata2);
echo json_encode(dbdata);
Different Keys:
$dbdata = array(
'table_1' => $dbdata1,
'table_2' => $dbdata2
);
echo json_encode($dbdata);
MySQL Join:
SELECT n.*, m.* FROM news n LEFT JOIN news_meta m ON m.news_id = n.news_id ORDER BY n.id DESC;
/* $dbdata will then contain your two tables worth of data: */
echo json_encode($dbdata);

Count the data under such specific ID No. from a table on the Database [duplicate]

This question already has an answer here:
SQL count and group
(1 answer)
Closed 4 years ago.
I don't know the syntax in PHP but I want to COUNT the "1"s of a certain ID no. then Display the result. In this case the desired result would be 3, 3 and 5. Since all bookstatus value on every book ID is "1". If the value of bookstatus changes from 1 to 2 then it will only COUNT the "1"s. See attached photo.
Thank you
SELECT bookID, COUNT(*) from your_table GROUP BY bookID WHERE bookStatus = 1
the above query returns all availabe books per bookID. you can add bookID in where status for certain bookID
Edit: Select data from database and fetch it(a sample code based on tutorialspoint):
<html>
<head>
<title>Selecting Table in MySQLi Server</title>
</head>
<body>
<?php
$dbhost = 'localhost:3306';
$dbuser = 'root';
$dbpass = '';
$dbname = 'TUTORIALS';
$conn = mysqli_connect($dbhost, $dbuser, $dbpass,$dbname);
if(! $conn ) {
die('Could not connect: ' . mysqli_error());
}
echo 'Connected successfully<br>';
$sql = 'SELECT name FROM tutorials_inf';
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
echo "Name: " . $row["name"]. "<br>";
}
} else {
echo "0 results";
}
mysqli_close($conn);
?>
</body>
</html>
You need to use a count() function in php to count the number of bookId available count in your database and fetch to display in your webpage.
$query = "SELECT bookId, COUNT(bookId) FROM tableName WHERE bookId ='1001' ";
Now fetch the result to your webpage.

INNER JOIN SQL Query not working with PHP

I have created two tables in my database and normally my php file is able to get table data from mysql.
But when I add INNER JOIN or anything like that, it does not work anymore. No output is seen but also no error message (so the code have to be correct, I think).
Here's my php code:
<?php
$db_name = "mydatabase";
$mysql_username = "root";
$mysql_password = "";
$server_name = "localhost";
$conn = mysqli_connect($server_name,$mysql_username,$mysql_password,$db_name);
$query = mysqli_query($conn,"SELECT * FROM firsttable INNER JOIN secondtable ON firsttable.secondtable_id = secondtable.secondtable_id");
while($row = mysqli_fetch_array($query))
{
$flag[] = $row;
}
print(json_encode($flag));
mysqli_close($conn);
?>
Try to debug like
1.if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
2.$query = mysqli_query($conn,"SELECT * FROM firsttable INNER JOIN secondtable ON firsttable.secondtable_id = secondtable.secondtable_id");
if (!$query) {
die('Invalid query: ' . mysql_error());
}
It seems there is no error in your program ! Make sure the database connection is established or not
if (!$conn) {
die('Could not connect: ' . mysqli_connect_error());
}
The mysql query seems to be correct .. better you post your database structure ! and check the column name
firsttable.secondtable_id = secondtable.secondtable_id
in your code both are secondtable_id, is that correct column name ?

How to Two Tables And One Login Form To Log In

Please help,I have a major doubt: Could you please help me with this
How can I SELECT all data from two differences table and login in username & userpassword in one login form?
$loginEmail = $_POST['loginEmail'];
$loginPassword = $_POST['loginPassword'];
$corpStaffNo = $_POST['corpStaffNo'];
$corpHRDF = $_POST['corpHRDF'];
$corpContact = $_POST['corpContact'];
$corEmail = $_POST['corEmail'];
$corpFax = $_POST['corpFax'];
$conn = mysqli_connect("localhost", "root", "", "ABC");
if (!$conn) {
die("Connection failed: ".mysqli_connect_error());
}
$sql = "SELECT * FROM user_member, corp_member WHERE loginEmail='$loginEmail' AND loginPassword='$loginPassword'";
$result = $conn->query($sql);
if (!$row = $result->fetch_assoc()) {
echo "<script type='text/javascript'>
alert('sdcsv');
window.location.replace(\"http:://localhost\");
</script>";
} else {
echo "your login";
}
you need to use a JOIN statement and have a foreign key (soft or hard your choice).
let's say that the user_member table has a columns named corp_id and the corp_name table has a column named id
$result = $comm->query("SELECT u.* FROM user_member AS u JOIN corp_member AS c ON u.corp_id = c.id WHERE loginEmail='$loginEmail' AND loginPassword='$loginPassword';");
then you can redirect using header
if (!$row = $result->fetch_assoc()) {
header('location: /');
} else {
$_SESSION['user'] = $row;
header('location: /logedin');
}
//good idea to exit to force the redirect and prevent further execution
exit;
FYI: I didn't do any SQL escaping you should really do that.
FYI2: By the naming of the tables it feels like corp_member is a relational table most likely there will be another table name corp. if that is the case the query will probably look more like this
SELECT u.*, c.* FROM user_member AS u
JOIN corp_member AS cm ON cm.id = cm.user_member_id
JOIN corp AS c ON cm.corp_id = c.id

Join 3 tables to display certain data PHP-MSSQL

so i have this tables and i want to get certain datas for user to view and be able to POST to other page
i cant post images so i have to break this down so please bear with me
1st table
dbo.users
pkey(UserID)
EmployeeName
2nd table
dbo.PC
pkey(PCID)
PC_Number
3rd table
dbo.FA_PC
pkey(FAID)
fkey(UserID)
fkey(PCID)
how could i display the PC_Number of the currently selected $rs->Fields('UserID') in the same form and still be able to post it on printd.php
i dont know how to connect the dbo.users->dbo.FA_PC->dbo.PC
<form action="printd.php" method="post" target="_blank">
<?php
ini_set("display_errors","on");
$conn = new COM("ADODB.Connection");
try {
$myServer = "WTCPHFILESRV\WTCPHINV";
$myUser = "sa";
$myPass = "P#ssw0rd";
$myDB = "wtcphitinventory";
$connStr = "PROVIDER=SQLOLEDB;SERVER=".$myServer.";UID=".$myUser.";PWD=".$myPass.";DATABASE=".$myDB;
$conn->open($connStr);
if (! $conn) {
throw new Exception("Could not connect!");
}
}
catch (Exception $e) {
echo "Error (File:): ".$e->getMessage()."<br>";
}
if (!$conn)
{exit("Connection Failed: " . $conn);}
$sql_exp = "select * from dbo.users";
$rs = $conn->Execute($sql_exp);
echo "<select name='empt'>";
while (!$rs->EOF) {
set_time_limit(0);
echo "<option value=".$rs->Fields('UserID')." >".$rs->Fields('EmployeeName')."</option>";
$rs->MoveNext();
}
$rs->Close();
?>
the join will look like this,
SELECT a.*, c.PC_Number
FROM users a
INNER JOIN FA_PC b
ON a.UserID = b.UserID
INNER JOIN PC c
ON b.PCID = c.PCID
To fully gain knowledge about joins, kindly visit the link below:
Visual Representation of SQL Joins

Categories