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];
}
?>
Related
This is my code:
$query2 = "select * from teaches where teacherid_fk LIKE '$teacherid_fk'";
$userinfo2 = mysqli_query($conn, $query2);
while ($line = mysqli_fetch_assoc($userinfo2)) {echo json_encode($line);}
Somehow, the result I'm getting is only one array when there should be two arrays displayed. I need both arrays to be displayed, but my code only displays the second array.
This is the db:
dbTableofTeaches
Am I doing something wrong here? Just recently learned PHP and any help is deeply appreciated. :)
EDIT 2: If I do the code like this:
$query2 = "select * from teaches where teacherid_fk LIKE '$teacherid_fk'";
$userinfo2 = mysqli_query($conn, $query2);
$row = mysqli_fetch_array($userinfo2);
echo json_encode($row);
The result is this: {"teaches_id":"2","teacherid_fk":"1","subjectid_fk":"65","quarterid_fk":"1","gradeid_fk":"5","sectionid_fk":"13","schoolyearid_fk":"2"} [which is actually the first array I was looking for!]
EDIT 3: Okay I'm getting more confused. I've tried to edit the db so it now looks like this: dbTableOfTeaches2. With the same (first) code above, I refreshed the page, and the second and third arrays are now displayed, but still no sign of the first array! The only way I was able to display the first array was use the second code above.
try this.
$query2 = "select * from teaches where teacherid_fk = '$teacherid_fk'";
$userinfo2 = mysqli_query( $conn, $query2 );
$respons = array();
while ( $line = mysqli_fetch_assoc( $userinfo2 ) ) {
$respons[] = $line;
}
echo json_encode($respons);
I dont know if this is possible, but am trying to create a php variable using an entry from a mysql database.
For example, lets say I want to create the following variables, but i want to replace google with whatever the company name is in the database.
$google = $row['companyname'];
$google_ticker = $row['ticker'];
$google_holding = $row['holding'];
So if I had a row in my database with the company name as Yahoo, the following would appear instead:
$yahoo = $row['companyname'];
$yahoo_ticker = $row['ticker'];
$yahoo_holding = $row['holding'];
I have tried different variations of the following however I cant get it to work (I know the code below wont work, its just an example of the sort of thing I have tried):
$query = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_assoc($query)){
$$row['companyname'] = $row['companyname'];
$$row['companyname']_ticker = $row['ticker'];
$$row['companyname']_holding = $row['holding'];
}
I can get it to work using something similar to the following, however doing it this way means I have to create each variable manually (as far as i know). I am trying to get it to create my variables automatically depending on what company names I have in my database.
$values = [];
$query = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_assoc($query)){
$values[$row['companyname']] = $row;
}
$google = $values['google']['companyname'];
$google_ticker = $values['google']['ticker'];
$google_holding = $values['google']['holding'];
Any ideas on how I can achieve this?
Many Thanks
Use a (multidimensional-)Array with KEYS that function as variable names:
$query = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_assoc($query)){
$data[$row['companyname']]['name'] = $row['companyname'];
$data[$row['companyname']]['ticker'] = $row['ticker'];
$data[$row['companyname']]['holding'] = $row['holding'];
}
Now you can access the holding variable like this:
echo $data['google']['holding'];
That should get you started...
Succes!
You can use like this:
${$row['companyname']} = $row['companyname'];
doc
I am trying to query a db for an entire column of data, but can't seem to get back more than the first row.
What I have so far is:
$medicationItem = array();
$medicationItemSql = "SELECT medication FROM medication";
$medicationItemObj = mysqli_query($connection, $medicationItemSql);
if($row = mysqli_fetch_array($medicationItemObj, MYSQLI_NUM)){
echo count($row);
}
It's not my intention to just get the number of rows, I just have that there to see how many it was returning and it kept spitting out 1.
When I run the sql at cmd line I get back the full result. 6 items from 6 individual rows. Is mysqli_fetch_array() not designed to do this?
Well, I had a hard time understanding your question but i guess you are looking for this.
$medicationItem = array();
$medicationItemSql = "SELECT medication FROM medication";
$medicationItemObj = mysqli_query($connection, $medicationItemSql);
if($row = mysqli_num_rows($medicationItemObj))
{
echo $row;
}
Or
$medicationItem = array();
$medicationItemSql = "SELECT medication FROM medication";
$medicationItemObj = mysqli_query($connection, $medicationItemSql);
$i = 0;
while ($row = mysqli_fetch_array($medicationItemObj))
{
$medicationItem[] = $row[0];
$i++;
}
echo "Number of Rows: " . $i;
If you just want the number of rows i would suggest using the first method.
http://php.net/manual/en/mysqli-result.num-rows.php
You can wrote your code like below
$medicationItem = array();
$medicationItemSql = "SELECT medication FROM medication";
$medicationItemObj = mysqli_query($connection, $medicationItemSql);
while ($row = mysqli_fetch_assoc($medicationItemObj))
{
echo $row['medication'];
}
I think this you want
You could give this a try:
$results = mysqli_fetch_all($medicationItemObj, MYSQLI_NUM);
First, I would use the object oriented version of this and always use prepared statements!
//prepare SELECT statement
$medicationItemSQL=$connection->prepare("SELECT medication FROM medication");
// execute statement
$medicationItemSQL->execute();
//bind results to a variable
$medicationItemSQL->bind_result($medication);
//fetch data
$medicationItemSQL->fetch();
//close statement
$medicationItemSQL->close();
You can use mysqli_fetch_assoc() as below.
while ($row = mysqli_fetch_assoc($medicationItemObj)) {
echo $row['medication'];
}
I am storing the names and types of images in my database. Now I want to select the newest images from that database and display them to the user. I tried to use the following code but I am getting the same image name and type, but I want to get different images.
$new_image_count = mysql_query("SELECT COUNT(id) FROM newest");
$result = mysql_result($new_image_count, 0);
$select_images = mysql_query("SELECT * FROM newest");
$fetch = mysql_fetch_assoc($select_images);
for($result; $result > 0; $result--){
$name = $fetch['image_name'];
$type = $fetch['image_type'];
$name_dot_type = $name.".".$type;
echo '<img src="main_images/'.$name_dot_type.'" width="300">';
}
Any ideas about how to fix this problem ?
Remove the query to count rows (you don't need it)
and change the main part of the code to something like
$select_images = mysql_query("SELECT * FROM newest");
while ($fetch = mysql_fetch_assoc($select_images)) {
// echo data from $fetch
}
PS: you'll recently get a comments about using PDO instead of mysql_
PPS: you get the same results because you fetch the row just once, and after that you output the same values in the loop
First off, you shouldn't be using mysql_*() functions anymore, and should switch to mysqli or PDO
Secondly, just move the $fetch = mysql_fetch_assoc($select_images); inside the for loop:
for($result; $result > 0; $result--){
$fetch = mysql_fetch_assoc($select_images);
$name = $fetch['image_name'];
$type = $fetch['image_type'];
$name_dot_type = $name.".".$type;
echo '<img src="main_images/'.$name_dot_type.'" width="300">';
}
I was wondering. I always do fetch and then create variables when I do loop. Is there a way to do this more efficient? Like as in automatically?
Maybe something like convert_to_variable("name","description","etc") and it'll automatically set variables for me without having to do each manually? Or maybe a single command like convert_to_variable($rows) and it'll do the rest for me.
Here is what I am doing now.
$sql = "SELECT * from projects";
$rows = $db->fetch_all_array($sql);
foreach($rows as $row) {
$name = $row['name'];
$description = $row['description'];
}
Something easier would be
$sql = "SELECT * from projects";
$rows = $db->fetch_all_array($sql);
foreach($rows as $row) {
convert_to_variable($row);
echo $name, $description;
}
extract can do that for you.