How to apply this code to get mysql to php output? - php

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

Related

Simple and Efficient Code to Count Data in PHP with MySQL

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'];

Multiple MySQL Queries (first one to get an id based on the user) second one to get information of another project in a database with the id

I am trying to accomplish the following:
Get the project id from a database where the username =...
-> Users can be signed into different projects and it is being tracked in a database
Next step is to get information of the project database to display it for the user
-> But since the user can be signed into different projects I want him to see them underneath each other in a not yet specific order (I a
This is basically what I was trying to get to work and it displays the right content but unfortunately stops at the first project. My assumption is that it stops at the first project id and doesn't continue
-> I checked to see what code of the code works and as soon as is cut the information gathering query I get all project_id's
Here is basically the code that I am working with right now:
$sql = "SELECT project_id FROM project_members_db WHERE user_ID = '$user_id'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while ($row = $result->fetch_assoc()) {
//Album Informationen ziehen herausfinden
$sql = "SELECT project_name, sticker_count, manufacturer, sport, creation_datetime FROM project_db WHERE project_id = '$row[project_id]'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while ($row = $result->fetch_assoc()) {
//OUTPUT
echo "
$row['project_name'];
$row['count'];
$row['sport'];
$row['manufacturer'];
$row['creation_datetime'];
";
}
}
}
}
It looks like you ought to be able to do this with a single query - and as you are taking user supplied input you ought to consider using a prepared statement
$sql='select `project_name`, `sticker_count`, `manufacturer`, `sport`, `creation_datetime`
from `project_db` where `project_id` = ( select `project_id` from `project_members` where `user_id` = ? )';
$stmt=$conn->prepare( $sql );
$stmt->bind_param('s',$user_id);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($project_name,$sticker_count,$manufacturer,$sport,$creation_datetime);
while( $stmt->fetch() ){
echo $project_name,$sticker_count,$manufacturer,$sport,$creation_datetime;
}
You overwrite the first result with the second result. Change to:
$sql = "SELECT project_id FROM project_members_db WHERE user_ID = '$user_id'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
//Album Informationen ziehen herausfinden
$sql = "SELECT project_name, sticker_count, manufacturer, sport, creation_datetime FROM project_db WHERE project_id = '$row[project_id]'";
$result2 = $conn->query($sql);
if ($result2->num_rows > 0) {
// output data of each row
while($row2 = $result2->fetch_assoc()) {
//OUTPUT
echo $row2['project_name'] . ' '
$row2['count'] . ' '
$row2['sport'] . ' '
$row2['manufacturer'] . ' '
$row2['creation_datetime'];
}
}
}
}
Note putting arrays in your string doesn't work, so i have concatenated the echo instead.

How to update data using one select in other database table

I have two databases and i have one table "TabelaX" in database "Servidor1" with out data and other database "Servidor2" with one table "TabelaY". And i want do one select in table "TabelaY" and with her data do one Update in table "TabelaX" which is in another database. I already made some code but it is not working correctly.
<?php
$conn= mysqli_connect('localhost','root',null,'Servidor2') or die
(mysqli_connect_error());
if (!$conn) {
die("Falha de conexao: ". mysqli_connect_error());
}
$ID = $_POST['ID'];
$sql = "SELECT * FROM TabelaY WHERE ID = $ID";
$result = mysqli_query($conn, $sql);
mysqli_select_db($conn,"Servidor1");
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
$row1 = $row["ID"];
$row2 = $row["Data"];
}
} else {
echo "0 results";
}
$sql = "INSERT INTO Servidor1.TabelaX (ID, Data)
SELECT ID, Data
FROM Servidor3.TabelaW
WHERE ID = $ID;";
$sql = "UPDATE Servidor1.TabelaX SELECT ID, Data FROM
Servidor3.TabelaW SET Data = $row2 WHERE $row1 = $ID;";
if (mysqli_multi_query($conn, $sql)) {
echo "Dados Inseridos";
} if (mysqli_multi_query($conn, $sql)) {
echo "Dados Atualizados";
}
mysqli_close($conn);
I have no idea what your query is trying to do, because you assign to $sql twice without ever executing the first query, but if you're asking how to update a row in tableX based on data from tableY, then:
UPDATE Servidor1.TabelaX as x, Servidor2.TabelaY as y
SET x.Data = y.Data
WHERE x.id = y.id
AND x.id = $someIdForWhichYouWantToUpdate
Also, do not do this:
$ID = $_POST['ID'];
$sql = "SELECT * FROM TabelaY WHERE ID = $ID";
Imagine what happens when the user posts 1; DROP DATABASE Servidor1 into the form. This is called SQL injection and your code is full of vulnerabilities to it.

How to output all data from multiple tables using mysqli php

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

mysqli result amounts depending on fetch inside while

I am fetching rows from a mysql table (jobs). Inside of that fetch, I am also fetching from another table (accounts) [to receive account api keys all depending on what ID_ASSOC is attacted to the job]: below is the code
$sql = "SELECT * FROM jobs";
$query = mysqli_query($db_conx, $sql);
while($row = mysqli_fetch_assoc($query)){
echo $row['action'];
echo "<br/>";
$job_poster_id = $row['id_assoc'];
$sql = "SELECT * FROM accounts WHERE id_assoc='$job_poster_id'";
$query = mysqli_query($db_conx, $sql);
while($rows = mysqli_fetch_assoc($query)){
$username = $rows['twitter_username'];
$consumer_key = $rows['consumer_key'];
$consumer_secret = $rows['consumer_secret'];
$access_token = $rows['access_token'];
$access_token_secret = $rows['access_token_secret'];
}
echo $job_poster_id ;
echo "<br/>";
echo $twitter_username;
echo "<br/>";
echo "----------------------------------";
echo "<br/>";
}
OUTPUT:
specific-message
4
admin
----------------------------------
When I do this, I only get one row output..and I can't seem to find out why. I want the above out put to repeat as many times as it has rows, and it's only doing one row (with the account fetch in the code). However when I do it without the internal fetch (accounts fetch), it returns multiple rows just as desired. Why is this? (below is sample code WITHOUT the accounts fetch):
$sql = "SELECT * FROM jobs";
$query = mysqli_query($db_conx, $sql);
while($row = mysqli_fetch_assoc($query)){
echo $row['action'];
echo "<br/>";
$job_poster_id = $row['id_assoc'];
echo $job_poster_id ;
echo "<br/>";
echo "----------------------------------";
echo "<br/>";
}
OUTPUT:
specific-message
4
----------------------------------
specific-message
1
----------------------------------
specific-message
2
----------------------------------
$query = mysqli_query($db_conx, $sql);
while($row = mysqli_fetch_assoc($query)){
echo $row['action'];
echo "<br/>";
$job_poster_id = $row['id_assoc'];
$sql = "SELECT * FROM accounts WHERE id_assoc='$job_poster_id'";
$query = mysqli_query($db_conx, $sql);
The problem is that you're using $query for the inner and the outer query.
When the inner query runs, and it steps through the loop, it's iterating to the end of the result set; when the outer while loop runs, mysqli_fetch_assoc($query) is returning false, because you're already at the end of the result set - just not the result set you were expecting.
You can fix this by renaming one of the $query variables.

Categories