Most of the variables will be in my language sorry about that. Also keep in mind i have very little knowledge of php/mysql as even my schools teachers have no idea what is it.
In my database there are 4 tables. The tables only have primary keys. The tables do not have any foreign key or references to other tables.
This is because in my school project we were told to not add foreign key and references. This means I cant/dont know how to use JOIN.
Currently i am attempting to output all of the data from the 4 tables and output them as 1 using html/css.
My main problem is i do not know how to output data from different rows.
I have tried using mysqli_data_seek to select each row with my counter($no) but it seems that either im using it wrong or its just not supposed to do that.
I have also tried using mysqli_fetch_assoc but i still get the same output.
I have a connection to my database
This is the code mysqli_data_seek
<?php
$row = mysqli_query($connection, "SELECT Kod_Barang, Kod_Rak, Harga, Tarikh_Jual from stok");
$no = 0;
if (mysqli_num_rows($row) > 0) {
while ($stok = mysqli_fetch_assoc($row)) {
$stok = mysqli_query($connection, "SELECT Kod_Barang, Kod_Rak, Harga, Tarikh_Jual from stok");
$datastok = mysqli_data_seek($stok, $no);
//get data from "barang' table
$barang = mysqli_query($connection, "SELECT Nama_Barang, no_Barang from barang");
$databarang = mysqli_data_seek($barang, $no);
//get data from "lokasi"
$lokasi = mysqli_query($connection, "SELECT Jenis_Barang from lokasi");
$datalokasi = mysqli_data_seek($lokasi, $no);
//get data from "tarikh" table
$tarikh = mysqli_query($connection, "SELECT Tarikh_Beli from tarikh");
$datatarikh = mysqli_data_seek($tarikh, $no);
$no++;
then after this i will have a output which is connected to a table already made (this is still in the while loop)
echo '<tr>';
echo '<td>'.$no.' </td>';
echo '<td>'.$datastok.'</td>';
echo '<td>'.$databarang.'</td>';
echo '<td>'.$datalokasi.'</td>';
echo '<td>'.$datastok.'</td>';
echo '<tr>';
This is the code with mysqli_fetch_assoc
<?php
$rowstok = mysqli_query($connection, "SELECT Kod_Barang, Kod_Rak, Harga, Tarikh_Jual from stok");
$no = 0;
if (mysqli_num_rows($rowstok)> 0) {
while ($stok = mysqli_fetch_assoc($rowstok)) {
$stok = mysqli_query($connection, "SELECT Kod_Barang, Kod_Rak, Harga, Tarikh_Jual from stok");
$datastok = mysqli_fetch_assoc($stok);
//get data from "barang" table
$barang = mysqli_query($connection, "SELECT Nama_Barang, Bil_Barang from barang");
$databarang = mysqli_fetch_assoc($barang);
//get data from "lokasi" table
$lokasi = mysqli_query($connection, "SELECT Jenis_Barang from lokasi");
$datalokasi = mysqli_fetch_assoc($lokasi);
//get data from "tarikh" table
$tarikh = mysqli_query($connection, "SELECT Tarikh_Beli from tarikh");
$datatarikh = mysqli_fetch_assoc($tarikh);
$no++;
Again here is my output table
//output table
echo '<tr>';
echo '<td>'.$no.' </td>';
echo '<td>'.$datastok['Kod_Barang'].'</td>';
echo '<td>'.$databarang['Nama_Barang'].'</td>';
echo '<td>'.$datalokasi['Jenis_Barang'].'</td>';
echo '<td>'.$datastok['Harga'].'</td>';
echo '<tr>';
Right now the output is just showing the first's rows data multiple times based on number of row. And also my counter($no) in increasing by 2 (1,3,5,7,....) not as expected 1
I am trying to get it to be each rows data.
I got the answer thanks to #ADyson.
I just changed my long line of code to JOIN
$data = mysqli_query($connection , "SELECT *
FROM stok
INNER JOIN barang
ON stok.Kod_Barang=barang.Kod_Barang
INNER JOIN lokasi
ON stok.Kod_Rak=lokasi.Kod_Rak
INNER JOIN tarikh
ON barang.Nama_Barang=tarikh.Nama_Barang");
again sorry for non English attributes.
here is where i learnt JOIN from another question
SQL Inner join more than two tables
Related
So I think there's will be a simple and efficient code to count data with PHP and get the data output from different MySQL tables. This implementation is used to display how much data in my dashboard page.
But I prefer to use PHP procedural style because I'm still learning.
So here is my dirty code:
$count_admin = mysqli_query($conn, "SELECT COUNT(*) AS count_admin FROM employee WHERE level_id = 1");
$count_officer = mysqli_query($conn, "SELECT COUNT(*) AS count_officer FROM employee WHERE level_id = 2");
$count_goods = mysqli_query($conn, "SELECT COUNT(*) AS count_goods FROM goods");
$count_customer = mysqli_query($conn, "SELECT COUNT(*) AS count_customer FROM customer");
$row_admin = mysqli_fetch_assoc($count_admin);
$row_officer = mysqli_fetch_assoc($count_officer);
$row_goods = mysqli_fetch_assoc($count_goods);
$row_customer = mysqli_fetch_assoc($count_customer);
echo $row_admin['count_admin'];
echo $row_officer['count_officer'];
echo $row_goods['count_goods'];
echo $row_customer['count_customer'];
I am making a database with a html output to show the data from my 4 tables.
The 4 tables have been made through normalization from 1 big table.
I have done this with just 1 table after getting the mysql_query(select bla bla)
while($row = mysqli_fetch_assoc($result)) {
echo $row["Kod_Barang"].' nex row '.$row["Nama_Barang"];
}
} else {
echo "0 results";
The coding i currently have
<?php
$resultstok = mysqli_query($connection, "SELECT Kod_Rak, Harga, Tarikh_Jual from stok");
$resultbarang = mysqli_query($connection, "SELECT Nama_Barang, Bil_Barang from barang");
$resultlokasi = mysqli_query($connection, "SELECT Jenis_Barang from lokasi");
$resulttarikh = mysqli_query($connection, "SELECT Tarikh_Beli from tarikh");
if (mysqli_num_rows($resultstok) > 0) {
$rowstok = mysqli_fetch_assoc($resultstok);
$rowbarang = mysqli_fetch_assoc($reseltbarang);
$rowlokasi = mysqli_fetch_assoc($resultlokasi);
$rowtarikh = mysqli_fetch_assoc($resulttarikh);
while
}
?>
I expect to get each of the data out but i dont know how to apply the first code into the 2nd
I am new to PHP coding and just trying to fix some functionality on my site that was left over from the lead developer.
The site, [Vloggi], is a marketplace. So I need to show the name of the job poster in the assignments page . The table I have the jobs in only has the ID, not the name.
So I need a join, but I've tried and it breaks the entire site.
The SQL has 17 tables, I need to display the User Name (usr_name) contained in table 3, the organisation contained in table 7 (usrg_orgname) with the job posting user (vlop_usr_id) details in table 14.
The primary key is users.usr_id, which is linked to users_gor.usrg_usr_id and vlog-ops.vlog_usr_id.
Table 3: users
usr_id, usr_email, usr_password, usr_fbuser, usr_fbtoken, usr_name, usr_loc_name, usr_loc_lat1, usr_loc_lon1, usr_loc_lat2, usr_loc_lon2, usr_status, usr_gor, usr_vgr, usr_token, usr_regtoken,
table 7: users_gor
usrg_usr_id, usrg_creditops, usrg_creditvlog, usrg_creditvlogette, usrg_destination, usrg_orgname, usrg_orgtype, usrg_location, usrg_website, usrg_jobtitle, usrg_phone, usrg_address1, usrg_address2, usrg_state, usrg_postcode, usrg_country
Table 14: vlog-ops
vlop_id, vlop_title, vlop_description, vlop_tags, vlop_deadline, vlop_quantity, vlop_quantityposted, vlop_vser_id, vlop_usr_id,vlop_loc_name, vlop_loc_lat1, vlop_loc_lon1, vlop_loc_lat2, vlop_loc_lon2, vlop_campaign, vlop_rules, vlop_tips, vlop_status
So in main.php i have written the following Sql lookup
in main.php, I have the following SQL lookups:
$sql = "SELECT * FROM users_gor WHERE usrg_usr_id = ".$db->quote($user_info['usr_id'])." LIMIT 1";
$rows = $db->select($sql);
$users_gor = $rows[0];
$sql = "SELECT * FROM users_vgr WHERE usrv_usr_id = ".$db->quote($user_info['usr_id'])." LIMIT 1";
$rows = $db->select($sql);
$users_vgr = $rows[0];
$sql = "SELECT * FROM users WHERE usr_id = ".$db->quote($user_info['usr_id'])." LIMIT 1";
$rows = $db->select($sql);
$users = $rows[0];
$sql = "SELECT * FROM vlog-ops WHERE vlop_usr_id ".$db->quote($user_info['usr_id'])." LIMIT 1";
$rows = $db->select($sql);
$users = $rows[0];
$sql = "SELECT usr_name AS vlop_usr_name FROM users WHERE usr_id = ".$db->quote($user_info['usr_id'])." LIMIT 1";
$rows = $db->select($sql);
$users = $rows[0];
And then in the page template itself, I have written
<?php echo $vlop['vlop_vser_id'] ?>
<?php echo $vlop['vlop_usr_name'] ?>
The first one works, the second doesn’t. What I want eventually is to display the user name and the organisation name in a table.
Whenever I try a JOIN or a NATURAL JOIN or a LEFT JOIN it breaks and the entire site goes blank.
Any help for a newbie would be appreciated with a million thanks.
When you use JOIN you need to specify how you're joining them.
In the query below I'm assuming you're looking for the fields in bold from your question.
$query='SELECT u.usr_name, g.usrg_orgname, v.vlop_usr_id FROM users u
JOIN vlog-ops v on u.usr_id = v.vlop_usr_id
JOIN users_gor g on u.usr_id = g.usrg_usr_id';
I believe I got the name of the fields right but if not just replace them with the correct ones.
Once you have the data fetched, you just loop through the results:
$result = mysqli_query($connection, $query);
while($row = mysqli_fetch_assoc($result)) {
echo 'User name = ' . $row['u.usr_name'];
echo 'Org name = ' . $row['g.usrg_orgname'];
echo 'Job posting user id = ' . $row['v.vlop_usr_id'];
}
I have the following code:
// db connection info set up earlier
$sql= "SELECT `TABLE_1.ID`, `TABLE_2.ID`, `POTATO` FROM `TABLE_1.ID` LEFT JOIN `TABLE_2` ON `TABLE_1`.`ID` = `TABLE_2`.`ID_OF_OTHER_TABLE`;";
$rows = mysqli_query($connection, $sql);
foreach ($rows as $row){
$potato = $row["POTATO"];
$id = $row["TABLE_2.ID"];
}
I can't get TABLE_2.ID. I've tried to doing a print_r to get the proper format, but it says it's a mysqli object and I don't get much more info than that. However, I can get potato. So I'm guessing it's a calling syntax issue, but I've searched multiple sites (stack and google included) and not found a clear answer. So, what do I need to do instead of
$id = $row["TABLE_2.ID"];
?
Assign aliases to the columns with the same name.
$sql= "SELECT `TABLE_1`.`ID` AS t1_id, `TABLE_2`.`ID` AS t2_id, `POTATO`
FROM `TABLE_1.ID`
LEFT JOIN `TABLE_2` ON `TABLE_2`.`ID_OF_OTHER_TABLE` = `TABLE_1`.`ID`;";
$rows = mysqli_query($connection, $sql);
foreach ($rows as $row){
$potato = $row["POTATO"];
$id = $row["t2_id"];
}
You can't left join a table with itself, you also cannot have a table that isn't joined = something from another table that cannot be joined to itself.
This will work:
// db connection info set up earlier
$sql= "SELECT TABLE_1.ID, TABLE_2.ID, POTATO
FROM
TABLE_1.ID
LEFT JOIN TABLE_2 ON TABLE_1.ID = TABLE_2.ID";
$rows = mysqli_query($connection, $sql);
while ($row = mysqli_fetch_assoc($rows)) {
echo ($row['ID']);
}
mysql_free_result($rows);
I have some PHP/MySQL code that pulls data from multiple different tables (using Inner Joins). It looks something like this:
$query = "SELECT * FROM table1 INNER JOIN table2 USING (key)";
$data = mysqli_query($dbc, $query);
while ($row = mysqli_fetch_array($data)) {
echo $row[1];
}
So the code is simple enough, but what I want to do is echo the table each row is in inside of that while loop, since it could be in one of 2 tables.
I saw there was some old mysql functions like mysql_field_table and mysql_tablename that would do the trick, but they all seem to be deprecated.
Would appreciate any advice on how to accomplish this.
You could select the data with a special identifier for each table instead of using *
select table1.row1 as t1r1, table2.row1 as t2r1,..... from .....
And inside php you could look for the strings t1 and t2 and do stuff accordingly.
What you can do is echo more than one field from your result table (which hopefully now contains information from both the tables.
$query = "SELECT * FROM table1 INNER JOIN table2 USING (key)";
$data = mysqli_query($dbc, $query);
while ($row = mysqli_fetch_array($data))
{
echo $row[1] . " " . $row[2];
}
...and then $row[3] etc...
Or access the column name/field by the column name or alias provided by AS.
$query = "SELECT * FROM table1 INNER JOIN table2 USING (key)";
$data = mysqli_query($dbc, $query);
while ($row = mysqli_fetch_assoc($data))
{
echo $row["c_name1"] . " " . $row["c_name2"];
}
Where "c_name1" and "c_name2" are your column names.
Use an AS keyword to rename all columns.
$select_clause = '';
$tables = array('tblA', 'tblB');
foreach($tables as $tbl) {
mysql_query('SHOW COLUMNS FROM ' . $tbl);
while ($row = mysql_fetch_assoc())
$select_clause .= '`'.$tbl.'`.`'.$row['Field'].'` AS `'.$tbl
.'_'.$row['Field'].'`,';
}
$select_clause = substr($select_clause, 0, -1);
mysql_query('SELECT '.$select_clause.' FROM /*...*/ ');