Hi and thank you for your time,
I am having problems with the following code:
<?php
include './include/config.php';
$sqla = mysqli_query($conn, "SELECT * FROM sector");
while ($rowa = $sqla->fetch_assoc()) {
$codigo = $rowa['codigo'];
$sector = $rowa['sector'];
echo $sector. "<br>";
$sqlb = mysqli_query($conn, "SELECT * FROM cfamilia WHERE sector = '$codigo'");
while ($rowb = $sqlb->fetch_assoc()) {
$cfamilia= $rowb['cfamilia'];
echo $cfamilia. " - ";
}
}
?>
When I run the code, it only echo´s the first result of the first loop and then seems to get stuck on the inner loop, what am I doing wrong? All data in the database is available and correct.
I am probably doing it the wrong way, but I am no way a programmer.
Thanks for your help
I have searched to check if all data is correct and that all variables were clean.
Related
Hello i selected all records from users and returned them using a php whileloop and array, using implode i could get all the records outside the loop, what i wish to do actually is to access individual record and assign them to a variable individually to be used later in the same page.
this is what i came up with already, it works, but i don't know how to assign the recorded to individual variables, since all the records are displayed using implode
`<?php
$data = array();
$query = "SELECT * FROM users ";
$queryResult = mysqli_query($conn,$query);
while($_row = mysqli_fetch_array($queryResult)){
$data[] = $_row['first_name'];
$data[] = $_row['email'];
$data[] = $_row['amount'];
?>
<?php }?>
<?php
echo "<br />";
$request = implode("<br />", $data);
echo $request;?>`
please i need assistance on how to achieve or a better way to access whileloop records outside the loop this thanks in advance
This is what i intended doing with that result of the loop in the next query
`<?php
$profit = 10;
$query = "UPDATE storage SET ";
$query .="store_profit = '{$profit}' ";
$query .= "WHERE email = '{$email_from_loop_above}' ";?>`
for clarity purposes.. this script will be executed by a cronjob every 1 minute, initially this is what is did..
`
<?php
$query = "SELECT * FROM users WHERE email = $_SESSION['email'] ";
$queryResult = mysqli_query($conn,$query);
while($_row = mysqli_fetch_array($queryResult)){
$first_name = $_row['first_name'];
$email_from_loop_above = $_row['email'];
$amount = $_row['amount'];
?>
<?php }?>`
Then for the update query this is what i did
`<?php
//The profit below was gotten from a percent from the amount i return from the loop above.
$profit = 10;
$query = "UPDATE storage SET ";
$query .="store_profit = '{$profit}' ";
$query .= "WHERE email = '{$_SESSION['email']}' ";
?>`
Now this code above works perfectly, but when the user logout out the store_profit would not update anymore simply because this you know would require an active user SESSION to work, so what i am trying to do now is is make it work accordingly even when the user is logged out of the account, if i use the following method
`<?php
$query = "SELECT * FROM users ";
$queryResult = mysqli_query($conn,$query);
while($_row = mysqli_fetch_array($queryResult)){
$first_name = $_row['first_name'];
$email_from_loop = $_row['email'];
$amount = $_row['amount'];
?>
<?php }?>`
`
<?php
$profit = 10;
$query = "UPDATE storage SET ";
$query .="store_profit = '{$profit}' ";
$query .= "WHERE email = '{$email_from_loop}' ";
?>`
it would only return the last user data only, simply because i am outside the loop, now when i try to update inside the loop, it works but it adds 10 to store_profit to the first user and adds 20 to the second user also adds 30 to third user... simply because it inside the loop
now what is want to do is make this work accordingly so i decided to use array, however i am not to familiar with array that why i became confused.
An explanation of how i can achieve this purpose would be very much appreciated thanks.
however i was thinking of not destroying all the user session maybe i create session for that purpose and not distroy it.. but i don't if that method would be safe or good.
As a result of your loop you have a $data array which you then turn into a string by gluing all its elements using implode function.
You can of course still access every individual element of $data array outside of the loop by addressing to its direct index.
For instance:
echo $data[0]['email'];
will output the email record of first (arrays are 0 based in PHP) element of your array.
I'm trying to improve the loading times of images and need to change around the code.
I haven't had any luck finding out how to do this and I'm not sure if it is even possible. In the example below you can see that I use KEY to match it with U_KEY to get FILE_PATH which I then add to a long comma delimited $allPaths string.
I know I should not use queries inside loops but I have no idea how to change this.
<?php
$sql = "SELECT * FROM test_users, image_uploads LIMIT 0, 27";
$result = mysqli_query($mysqli, $sql);
while($value = mysqli_fetch_array($result)) {
$files_key = $value["KEY"];
$allPaths = "";
$inner_query = "SELECT * FROM additional_uploads WHERE U_KEY = '".$files_key;
$inner_result = mysqli_query($mysqli, $inner_query);
while ($row = mysqli_fetch_array($inner_result)) {
$allpaths .= $row['FILE_PATH'].",";
}
// how do I get $allPaths without using a query inside the while loop?
}
?>
If someone could tell me how I can get $allPaths with only one query instead of multiple queries inside the loop as shown above it would probably load the images much faster.
Is this possible?
Edit
I have tried to understand the problem using the suggested answer and additionally I was looking on other forums as well to find out more. However I still cannot find a solution. Since I'm using mysqli_fetch_array the suggested answer really confuses me even further.
<?php
$inner_query = "SELECT * FROM additional_uploads";
$inner_result = mysqli_query($mysqli, $inner_query);
$rows = [];
while ($row = mysqli_fetch_array($inner_result)) {
$rows[$row['U_KEY']] .= $row['FILE_PATH'].",";
}
$sql = "SELECT * FROM test_users, image_uploads LIMIT 0, 27";
$result = mysqli_query($mysqli, $sql);
while($value = mysqli_fetch_array($result)) {
$files_key = $value["KEY"];
$allPaths = $rows[$files_key];
}
?>
// This gets all the users that are active
// The limit is completely random, it is set to 2 for this example
$sql = <<<SQL
SELECT *
FROM `accounts`
WHERE active = 1
LIMIT 2
SQL;
if(!$getaccounts = $db->query($sql)){
die('There was an error running the query [' . $db->error . ']');
}
while ($row = $getaccounts->fetch_assoc()) {
$getid = $row["id"].',';
$getid = substr($getid, 0, -1);
$getusername = $row["username"];
$getpassword = $row["password"];
echo $getid;
echo $getusername."<br>";
echo $getpassword."<br>";
}
I know this hasn't been prepared but I am not using it for anything other than personal use.
I cannot understand why this is not getting rid of the last comma?
The output may be something like "32,14,"
And I want to get rid of the last comma by using the "substr" function.
But the output that that I get from $getid is "3214" (It gets rid of all the commas instead of just the last one.
I need it to output "32,14" but it's not working?
Could someone please tell me where I am going wrong?
If I do rtrim, it does the same thing and gets rid of all the commas! I am going to update something in the database using the ids, and that is why I need to get rid of the last comma
And I know this code is not secure, I am not using it for anything other than personal use and I was hoping someone could help me figure this out, I have been attempting it for days, it seems really simple and I bet I am missing something really stupid!
You have a XY Problem.
You want to concat all the id's into a comma-seperated string.
Here's a much easier solution by adding the items to an array and then implode().
<?php
// rest of your code
$ids = Array();
while ($row = $getaccounts->fetch_assoc()) {
$ids[] = $row["id"];
$getusername = $row["username"];
$getpassword = $row["password"];
echo $getusername."<br>";
echo $getpassword."<br>";
}
echo "ids: " . implode(",",$ids);
You should write code like..
$getid = "";
while ($row = $getaccounts->fetch_assoc()) {
$getid .= $row["id"].',';
}
$getid = rtrim($getid,',');
$q = " UPDATE accounts SET active = '0' WHERE id IN ($getid) ";
I just started learning PHP for an assignment I have to do for school.
I have a problem with the mysqli_fetch_array() function.
Here is my code:
//Find all book_id for a specific student_id
$query = " SELECT * FROM books_of_students WHERE student_id = '$s_id' ";
$result = mysqli_query($conn, $query);
$records_b_o_s = mysqli_num_rows($result);
If I echo the variable $records_b_o_s it correctly shows how many records MySql has returned from the table, here is a screenshot from phpMyAdmin:
books_of_students table
Here is the following part of my code:
if ($records_b_o_s >= 1) {
while ( $row = mysqli_fetch_array($result) ) {
$temp_book_id = $row['book_id'];
//do other stuff
}
}
The problem is that the loop only occurs 1 time and $row['book_id'] returns the value from the first book there is in "books_of_students" table. If for example I execute the following command inside the loop:
while ( $row = mysqli_fetch_array($result) ) {
$temp_book_id = $row['book_id'];
echo "Current book_id: " . $temp_book_id . "<br>"; //<---This one
//do other stuff
}
There will be only 1 message and it will show only the first book_id from the table
I really don't know what to do here so I will much appreciate your help! Thanks in advance :)
Thank you everybody for your time and your answers!
It turned out that I had done something stupid within the while() loop. At some point I was changing the value of the $result variable without noticing it! Now the code works as it should be!
I am a newbie on PHP,MySQL and HTML.
I have one question about php array.
I create MySQL query that joins 2 tables in php. From this join, I will get information about one product, but have multiple JIG.
$sql = "SELECT product.product_number,product.product_name,product.product_jitqty,product.product_desc,jit.jit_number,jit.jit_name,jit.jit_drawer,jit.jit_port,jit.jit_specpath
FROM product
JOIN production_jit
ON production_jit.product_number = product.product_number
JOIN jit
ON jit.jit_number = production_jit.jit_number
WHERE product.product_number = '$productnumber'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result))
{ }
from above query, I will get result like picture below.
So I want to create an array for column jit.jit_number. for easy to transfer both or only one data to another page.
I have tried below code.
$sql = "SELECT product.product_number,product.product_name,product.product_jitqty,product.product_desc,jit.jit_number,jit.jit_name,jit.jit_drawer,jit.jit_port,jit.jit_specpath
FROM product
JOIN production_jit
ON production_jit.product_number = product.product_number
JOIN jit
ON jit.jit_number = production_jit.jit_number
WHERE product.product_number = '$productnumber'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
?>
<?php
while($row = mysqli_fetch_assoc($result))
{
$jig = array ($row['jit_number']);
echo "This is JIG " . $jig[0] . ", " . $jig[1] . ".";
}
But the result is like picture below.
Can anyone help me? So if I want to transfer both data to another page, I just can use $jig[0] and $jig[1]. Or anyone can advise me the better way to transfer multiple data form mysql to another page.
I'm not really sure what you're trying to achieve, but $jig is not going to have two indices because $row['jit_number'] ist just a number itself. If you're trying to store all jit_numbers in a separate array, you could try something like this:
$numbers = array();
while($row = mysqli_fetch_assoc($result))
{
$numbers[] = $row['jit_number'];
}
You shall also try to prevent MySQL injection, WHERE product.product_number = '$productnumber'"; is rather horrible from a security point of view. Using a prepared statement or at least some sanitization / escaping is recommendable.
Try this->
$jig = array();
array_push($jig, $row['jit_number']);