Issues with Pulling data from SQL - php

My script was working no problems until i added in a third element into my database:
sysinfo
SysInfoID CHNo Module Dector GasDetected ServiceID SiteID
1 1 O2 O2 O2 0 1
2 2 Meth Meth Meth 0 1
3 4 O2 O2 O2 0 1
php script
$data = mysql_query("SELECT j.JobID, j.CompletedBy, u.NameUser, j.SchDate, cu.CustName,
j.ContractNo, j.VisitNo, s.EqName, si.SiteName, si.SiteContact,
si.SiteAddress,si.SiteCounty, si.SiteTele, j.SiteID, si.SiteEmail, cu.CustEmail
FROM jobs as j
INNER JOIN users as u
ON j.AssignedTo = u.UserID
INNER JOIN site as si
ON j.SiteID = si.SiteID
INNER JOIN customer as cu
ON j.custID = cu.custID
INNER JOIN systypes as s
ON j.SysTypeID = s.SysTypeID
WHERE j.CompletedBy ='0'
AND j.AssignedTo='$mytechname'")
or die(mysql_error());
$count = mysql_num_rows($data);
$CHNo = array();
$Location = array();
$Module = array();
$Dector = array();
$GasDetected = array();
echo "<ul id='jlisthead' data-role='listview' data-inset='true' data-theme='a'><li data-role='list-divider'>Available Jobs ($count)</li></ul>";
echo '<ul data-role="listview" id="jobslist" data-split-icon="plus" data-theme="a" data-split-theme="b" data-inset="true" style="overflow: auto; height:175px; max-height: 175px;">';
while( $info = mysql_fetch_array ($data))
{
if($info[7] == '8' || $info[7] == '9'){
echo "<li><a id='ajob' data-id='$info[0]' data-assignedto='$info[2]' data-schdate='$info[3]' data-contractno='$info[5]' data-visitno='$info[6]' data-systype='$info[7]' data-sitename='$info[8]' data-contact='$info[9]' data-address='$info[10]' data-county='$info[11]' data-sitetel='$info[12]' data-siteid='$info[13]' data-siteemail='$info[14]' data-custemail='$info[15]' href='#'>$info[8]</a><a id='add' data-aid='$info[0]' data-aassignedto='$info[2]' data-aschdate='$info[3]' data-acontractno='$info[5]' data-avisitno='$info[6]' data-asystype='$info[7]' data-asitename='$info[8]' data-acontact='$info[9]' data-aaddress='$info[10]' data-acounty='$info[11]' data-asitetel='$info[12]' data-asiteid='$info[13]' data-asiteemail='$info[14]' data-acustemail='$info[15]'></a></li>";
}
else{
$data2 = mysql_query("SELECT SysInfoID, CHNo, Location, Module, Dector, GasDetected
FROM systeminfo
WHERE SiteID = $info[13]")
or die(mysql_error());
$arrlength=mysql_num_rows($data2);
while( $info2 = mysql_fetch_array ($data2)){
for($k =0;$k<$arrlength;$k++){
$CHNo[$k] = $info2[1];
$Location[$k] = $info2[2];
$Module[$k] = $info2[3];
$Dector[$k] = $info2[4];
$GasDetected[$k] = $info2[5];
}
}
$dis = "<li><a id='ajob' data-id='$info[0]' data-assignedto='$info[2]' data-schdate='$info[3]' data-contractno='$info[5]' data-visitno='$info[6]' data-systype='$info[7]' data-sitename='$info[8]' data-contact='$info[9]' data-address='$info[10]' data-county='$info[11]' data-sitetel='$info[12]' data-siteid='$info[13]' data-siteemail='$info[14]' data-custemail='$info[15]' href='#'>$info[8]</a><a id='add' data-aid='$info[0]' data-aassignedto='$info[2]' data-aschdate='$info[3]' data-acontractno='$info[5]' data-avisitno='$info[6]' data-asystype='$info[7]' data-asitename='$info[8]' data-acontact='$info[9]' data-aaddress='$info[10]' data-acounty='$info[11]' data-asitetel='$info[12]' data-asiteid='$info[13]' data-asiteemail='$info[14]' data-acustemail='$info[15]' ";
for($j = 0;$j<$arrlength;$j++){
$dis .= "data-achno".$j." ='$CHNo[$j]' data-alocation".$j."='$Location[$j]' data-amodule".$j."='$Module[$j]' data-adector".$j."='$Dector[$j]' data-agasdetected".$j."='$GasDetected[$j]'";
}
$dis .= "data-achtotal='$arrlength'></a></li>";
echo $dis;
}
}
echo '</ul>';
To explain a little, I want to check to see if the $info[7] element is 8/9 if it is then print out the information in a list. if its not then go do an ADDITIONAL query to get more information from another table (sysinfo) and store multipliable values within my list using the attr data.
The first query i have no issues with, but its when i go to do the additional query thats were it gets messed up. my arrlength is equal to "3" but all the data within the data-attr is the same.
example
<a id="add" data-aid="1" data-aassignedto="Mark McGuinness" data-aschdate="2014-03-22" data-acontractno="12" data-avisitno="4" data-asystype="3500" data-asitename="Tesco" data-acontact="Ann Jones" data-aaddress="21 Good Street Glasgow G14 4CA" data-acounty="Strathcylde" data-asitetel="1413216545" data-asiteid="1" data-asiteemail="" data-acustemail="admin#tesco.com"
data-achno0="3" data-alocation0="Side door" data-amodule0="02" data-adector0="02" data-agasdetected0="02"
data-achno1="3" data-alocation1="Side door" data-amodule1="02" data-adector1="02" data-agasdetected1="02"
data-achno2="3" data-alocation2="Side door" data-amodule2="02" data-adector2="02" data-agasdetected2="02" data-achtotal="3" title="" class="ui-btn ui-btn-icon-notext ui-icon-plus ui-btn-b"></a>
Please help it was working before i inserted a new row into the database. Thanks in advance.
Resolved
removed the for loop within the while loop:
previous
while( $info2 = mysql_fetch_array ($data2)){
for($k =0;$k<$arrlength;$k++){
$CHNo[$k] = $info2[1];
$Location[$k] = $info2[2];
$Module[$k] = $info2[3];
$Dector[$k] = $info2[4];
$GasDetected[$k] = $info2[5];
}
}
!!!Fix!!!!
while( $info2 = mysql_fetch_array ($data2)){
$CHNo[$k] = $info2[1];
$Location[$k] = $info2[2];
$Module[$k] = $info2[3];
$Dector[$k] = $info2[4];
$GasDetected[$k] = $info2[5];
$k++;
}

Related

Rotate the query of the table horizontally in PHP getting the thead of a different table of the tbody

Doesn't anyone know how to rotate the result table from a join of tables ?
Better explain
This is my result
This is what i want
and finally this is my code:
<div id="encuesta">
<?php
$y = 0;
$Query1 = $Conexion->query("SELECT ID_Encuesta,Encuesta_Nombre FROM encuestas");
while($row1 = $Query1->fetch_assoc())
{
$y++;
$x = 0;
?>
<h4><?php echo $y; ?> - <?php echo $row1['Encuesta_Nombre']; ?></h4>
<table class="table table-bordered" id="table<?php echo $row1["ID_Encuesta"]; ?>">
<tbody>
<?php
$Query2 = $Conexion->query("SELECT ID_Pregunta,Pregunta_Nombre,Pregunta_Pregunta,Pregunta_Tipo FROM preguntas WHERE pregunta_encuesta = '$row1[ID_Encuesta]'");
while($row2 = $Query2->fetch_assoc())
{
echo '<tr>';
echo '<td class="bg-primary" style="color:white;">'.$row2['Pregunta_Nombre'].' </td>';
$Query3 = $Conexion->query("
SELECT
(
CASE
WHEN A.Pregunta_Tipo = '1' THEN C.Etiqueta_Numero
WHEN A.Pregunta_Tipo = '2' THEN B.Respuesta_Respuesta
WHEN A.Pregunta_Tipo = '3' THEN B.Respuesta_Respuesta
WHEN A.Pregunta_Tipo = '4' THEN C.Etiqueta_Numero
ELSE 1
END) AS Respuesta
FROM preguntas AS A
JOIN respuestas AS B
ON A.ID_Pregunta = B.Respuesta_Pregunta
LEFT JOIN etiquetas AS C
ON B.Respuesta_Respuesta = C.ID_Etiqueta
WHERE B.Respuesta_Pregunta = '$row2[ID_Pregunta]'
");
while($row3 = $Query3->fetch_assoc())
{
echo '<td>'.$row3['Respuesta'].'</td>';
}
echo '</tr>';
}
?>
</tbody>
</table>
<?php
}
?>
well, to explain my code, the first $Query1 is a consult to the encuestas table to get the thead and the relation with the $Query2,
The $Query3 its to get the value what I need to show on the tbody.
Here is my DB SQL Fidle
You have 2 options:
You can change your query
or you can change your php code.
while($row3 = $Query3->fetch_assoc())
{
echo '<td>'.$row3['Respuesta'].'</td>';
}
To change your query I need to see how the database looks like etc.
For the php code
You can push the code in an array list.
and then push the arraylist into the tables.
I suggest you change the query.
You need to create one array with your results and call this method, it will to transpose an array and then print your table again, check this sample https://3v4l.org/v9VsV
function transposeArray($array)
{
$i = 0;
$transpose = [];
while ($columns = array_column($array, $i++))
{
$transpose[] = $columns;
}
return $transpose;
}

Why while loop does not showing result after first iteration inside while loop?

I'm trying to fetch the comments inside the while loop by newsfeed_id, but it does not showing any result after first iteration. I print_r dynamically generated query its working fine but result does not showing.
$NewsfeedRes = array();
$Newsfeed = "select * from `ws_newsfeed` where `nf_status` = 1";
$NewsfeedQuery = mysqli_query($this->connection,$Newsfeed);
while($rowNews = mysqli_fetch_assoc($NewsfeedQuery)){
$NewsfeedRes[] = $rowNews;
$PushComment = "SELECT cmt.`cmt_id`,cmt.`cmt_comment`,cmt.`date_added`,
us.`u_username`,us.`u_image`
FROM `ws_comments` AS cmt
LEFT JOIN `ws_user` AS us ON cmt.`u_id`=us.`u_id`
WHERE cmt.`cmt_target_id` = ".$rowNews['nf_id']."
AND cmt.`cmt_table_name`='ws_newsfeed'";
//echo $PushComment; This giving me correct query
$PushCommentQuery = mysqli_query($this->connection,$PushComment);
while($rowPComment = mysqli_fetch_assoc($PushCommentQuery)){
$NewsfeedRes['comments'] = $rowPComment;
}
}
$output = array(
'NewsfeedRes' => $NewsfeedRes,
);
echo json_encode($output, JSON_PRETTY_PRINT);
Can any one guide me where I'm wrong that i can fix the issue. I will appreciate. Thanks
Every time you loop through your fetch comment loop you are resetting the the value of $NewsfeedRes['comments']. You need to push $rowPComment into an array.
If you want $rowNews and comment to be in the same array try:
while($rowNews = mysqli_fetch_assoc($NewsfeedQuery)){
$PushComment = "SELECT cmt.`cmt_id`,cmt.`cmt_comment`,cmt.`date_added`,us.`u_username`,us.`u_image` FROM `ws_comments` AS cmt LEFT JOIN `ws_user` AS us ON cmt.`u_id`=us.`u_id` WHERE cmt.`cmt_target_id` = ".$rowNews['nf_id']." AND cmt.`cmt_table_name`='ws_newsfeed'";
//echo $PushComment; This giving me correct query
$PushCommentQuery = mysqli_query($this->connection,$PushComment);
while($rowPComment = mysqli_fetch_assoc($PushCommentQuery)){
$rowNews['comments'][] = $rowPComment;
}
$NewsfeedRes[] = $rowNews;
}
If you want a seperate array for comments and news feed try:
while($rowNews = mysqli_fetch_assoc($NewsfeedQuery)){
$NewsfeedRes["newsfeed"][] = $rowNews;
$PushComment = "SELECT cmt.`cmt_id`,cmt.`cmt_comment`,cmt.`date_added`,us.`u_username`,us.`u_image` FROM `ws_comments` AS cmt LEFT JOIN `ws_user` AS us ON cmt.`u_id`=us.`u_id` WHERE cmt.`cmt_target_id` = ".$rowNews['nf_id']." AND cmt.`cmt_table_name`='ws_newsfeed'";
//echo $PushComment; This giving me correct query
$PushCommentQuery = mysqli_query($this->connection,$PushComment);
while($rowPComment = mysqli_fetch_assoc($PushCommentQuery)){
$NewsfeedRes['comments'][] = $rowPComment;
}
}

Take information from preexisting database list and display it "on the fly" in styled text

I have a question that I am having a hard time articulating so if my title needs to be changed please let me know. ;)
I am working on a classifieds website. Users are asked to select the city they want to see ads that are within that city. Those cites are listed under their parent countries like so
CANADA - 155 posts-Toronto-Halifax-ect...
USA - 78 posts-Detroit-Chicago-ect...
So thats great, the list populates correctly and everything works as far as that is concerned.The problem I am having is that I would like to isolate the countries from the list with their post counts on the fly to use elsewhere in the site. I cant figure out how to separate that information from the list itself. I understand that this code: <div><?php echo $country['countryname']; ?> <?php if($show_region_adcount) echo "(".(0+$country_adcounts[$country['countryid']]).")"; ?></div>is what pulls that information in that I want into the list but I just want a page on my site to have basic text saying something to the effect of"Currently Canada has 155 posts and USA has 78"
I guess what I am asking is how do I pull in a single country name and its post counts individually without calling in the whole list? I am aware that this should be simple, but I am self taught everything so please excuse me if this seems silly.I have included all of the code below as I know this is helpful
<?php
require_once("initvars.inc.php");
require_once("config.inc.php");
?>
<table width="100%"><tr><td valign="top">
<?php
// Show city list
if($location_sort)
{
$sort1 = "ORDER BY countryname";
$sort2 = "ORDER BY cityname";
}
else
{
$sort1 = "ORDER BY c.pos";
$sort2 = "ORDER BY ct.pos";
}
if ($show_region_adcount || $show_city_adcount)
{
// First get ads per city and country
$country_adcounts = array();
$city_adcounts = array();
$sql = "SELECT ct.cityid, c.countryid, COUNT(*) as adcnt
FROM $t_ads a
INNER JOIN $t_cities ct ON ct.cityid = a.cityid AND ($visibility_condn)
INNER JOIN $t_countries c ON ct.countryid = c.countryid
WHERE ct.enabled = '1' AND c.enabled = '1'
GROUP BY ct.cityid";
$res = mysql_query($sql) or die(mysql_error().$sql);
while($row=mysql_fetch_array($res))
{
$country_adcounts[$row['countryid']] += $row['adcnt'];
$city_adcounts[$row['cityid']] += $row['adcnt'];
}
}
$sql = "SELECT * FROM $t_countries c INNER JOIN $t_cities ct ON c.countryid = ct.countryid AND ct.enabled = '1' WHERE c.enabled = '1' GROUP BY c.countryid $sort1";
$resc = mysql_query($sql);
$country_count = mysql_num_rows($resc);
//$split_at = ($country_count%3?((int)($country_count/3))+2:($country_count/3)+1);
$percol = floor($country_count/$location_cols);
$percolA = array();
for($i=1;$i<=$location_cols;$i++) $percolA[$i]=$percol+($i<=$country_count%$location_cols?1:0);
$i = 0; $j = 0;
$col = 1;
while($country = mysql_fetch_array($resc))
{
/* Begin Version 5.0 */
$country_url = buildURL("main", array((0-$country['countryid']), $country['countryname']));
/* End Version 5.0 */
?>
<div><?php echo $country['countryname']; ?> <?php if($show_region_adcount) echo "(".(0+$country_adcounts[$country['countryid']]).")"; ?></div>
<?php
if($country['countryid'] == $xcountryid || !$expand_current_region_only)
{
$sql = "SELECT * FROM $t_cities ct WHERE countryid = $country[countryid] AND enabled = '1' $sort2";
$resct = mysql_query($sql);
/* Begin Version 5.0 */
$citycount = mysql_num_rows($resct);
/* End Version 5.0 */
while($city=mysql_fetch_array($resct))
{
/* Begin Version 5.0 */
if ($shortcut_regions && $citycount == 1
&& $city['cityname'] == $country['countryname']) {
continue;
}
$city_url = buildURL("main", array($city['cityid'], $city['cityname']));
/* End Version 5.0 */
?>
<?php echo $city['cityname']; ?> <?php if($show_city_adcount) echo "(".(0+$city_adcounts[$city['cityid']]).")"; ?><br>
<?php
}
}
?>
<?php
$i++; $j++;
//if($i%$split_at == 0) echo "</td><td valign=\"top\">";
if ($j%$percolA[$col]==0 && $i<$country_count) { echo "</td><td valign=\"top\">"; $col++; $j=0; }
}
?>
</td></tr></table>
I really appreciate the help you guys give and its my birthday today, so maybe someone can throw this old dog a bone! Thanks in advance.
Hope this query is of help.
SELECT c.name, c.countryid, COUNT(a.id)
FROM country AS c, city AS ct, ads AS a
WHERE c.enabled = '1' AND ct.countryid = c.countryid
AND ct.enabled = '1' AND a.cityid = ct.cityid
GROUP BY c.countryid;
using jquery after all you dom load
$("a.citylist_country").each(function () {
//use this country name
alert( $(this).html());
});

PHP while loop to get values from mysql data base if it equals the $i value

I'm trying to get a value to be inserted into a table on a webpage if the value equals $i.
$i starts at a number and decreases every loop. i can get it to work but it outputs multiple lines for each $i equivalent to the results in the table
I've reworked the code using everyones feedback to get this.
Echo "<tr><th colspan='3'><center>$rackname</th> </tr>" ;
for ($i=$RUtotal; $i > 0; $i--)
{
echo" <tr class='rackbg'><td class='i'><center>$i</td>" ;
$sql1 = "SELECT racks.rackID, racks.rackname, devices.deviceID, devices.deviceName, racks.rackRU, devices.deviceRU, devices.RUcount
FROM racks LEFT JOIN devices ON racks.rackID = devices.rackID
WHERE devices.rackID = '$rackID'";
$query1 = mysql_query($sql1);
while ($row = mysql_fetch_assoc($query1))
{
$deviceru = $row['deviceRU'];
$deviceID = $row['deviceID'];
$device = $row['deviceName'];
$deviceRUC = $row['RUcount'];
if ($deviceru == $i)
{
echo '<td class="device" rowspan='.$deviceRUC.'><a onclick=window.location="/devices.php?id='.$deviceID.'">'.$device.'</a></td><td rowspan='.$deviceRUC.'></td></tr>';
}
else
{
;
}
}
}
Echo "<tr class='rackb'><th colspan='3'>a</th></tr> " ;
This works to a degree (picture1) but when i add echo "" to the else statement it displays all wrong. (picture 2)
Any help would be greatly appreciated
Picture1 - http://imageshack.us/photo/my-images/263/examplewq.png/
Picture2 - http://imageshack.us/photo/my-images/269/example2jp.png/
I can't quite see what you're trying to do but what it looks like to me is that you want all the items from racks joined with their relevant device and displayed in order of deviceRU. Does this help:
echo "<tr><th colspan='3'><center><b>$rackname</th></tr>" ;
$sql1 = "SELECT racks.rackID, racks.rackname, devices.deviceID, devices.deviceName, racks.rackRU, devices.deviceRU, devices.RUcount
FROM racks LEFT JOIN devices ON racks.rackID = devices.rackID
WHERE racks.rackID = '$rackID' AND devices.deviceRU <= ".intval($RUtotal)."
ORDER BY devices.deviceRU;"
$query1 = mysql_query($sql1);
while ($row = mysql_fetch_array($query1))
{
$deviceru = $row['deviceRU'];
$deviceID = $row['deviceID'];
$device = $row['deviceName'];
$deviceRUC = $row['RUcount'];
echo'<tr class="rackbg"><td class="i">'.$i.'</td><td class="device">'.$device.'</td><td></td></tr>';
}
I've used a LEFT (inner) JOIN in the SQL instead of the outer join that was there before as it'll return less results and might solve your problem. I've ordered the results by deviceRU and only returned results which have deviceRU less than or equal to $RUtotal (as I think the example was showing).
I've also removed the tags, these should be replaced by using CSS to centre either all td elements or centering class="device" and class="i" e.g.:
.device, .i {
text-align: center;
}
I've also swapped your abc to abc which is the correct format for a link.
Could you describe more of the context as it's difficult to see your intention from your post.
Mat
As Peetz said, you don't need nested loop. You need something like:
$i = $RUtotal;
// ...
while ($row = mysql_fetch_array($query1)) {
// ...
if ($deviceru == $i) {
// ...
} else {
// ...
}
// ...
$i--;
}
This is looping $i times, within the outer while loop. This means you are getting the table repeated over and over again.
I suggest you remove the outer while loop.

Mystified about making a PHP double-query dynamic list work. Using PHP 5.3.5

I'm trying to create a dynamic list (5 row results) in php by first getting data from one table then using a resulting var to get the latest uploaded "image_links" (just 1 from each of the 5 artists) from another table -- then echo out.
The code here gives me the only one row with the latest image. When I comment out the "// get the latest image link uploaded ///" section of the code I get the 5 rows of different artists I really want but, of course, w/o images. I tried (among a bunch of things) mysql_result() w/o the while statement but that didn't work.
So what am I missing?
Thanks
Allen
//// first get the artists followed ////////////////
$QUERY= "SELECT * FROM followArtist WHERE user_id = $user_id ";
$res = mysql_query($QUERY);
$num = mysql_num_rows($res);
if($num>0){
while($row = mysql_fetch_array($res)){
$artist_name = $row['artist_name'];
$artist_id = $row['artist_id'];
$date_lastSent = $row['date_lastSent'];
$date_artPosted = $row['date_artPosted'];
$date_notePosted = $row['date_notePosted'];
//// get new notice data /////
if ($date_artPosted >= $date_lastSent) {
$artp = "new artwork posted";
}else{
$artp = "";
}
if ($date_notePosted >= $date_lastSent) {
$notep = "news/announcement posted";
}else{
$notep = "";
}
if ($artp!="" && $notep!="") {
$and = " and<br />";
}else{
$and = "";
}
if ($artp=="" && $notep=="") {
$no = "No new images or news posted since your<br /> last visit, but we'll let you know when there is.";
}else{
$no = "";
}
//////// get the latest image link uploaded ////////////////////////////////////
$QUERY2="SELECT image_link FROM artWork WHERE artist_id ='$artist_id' AND make_avail = '1' ";
//ORDER BY date_submit DESC
$res = mysql_query($QUERY2);
$num = mysql_num_rows($res);
if($num>0 ){
while($row = mysql_fetch_assoc($res)){
mysql_fetch_assoc($res);
$image_link= $row['image_link'];
}
//////// end of get the latest images uploaded ////////////////////////////////
echo "<tr align=\"center\" height=\"115px\">
<td align=\"left\" width=\"15%\"> <a href=\"process_artist_idImages.php?artist_id=$artist_id&search=search\">
<img src=slir/w115-h115/$path$image_link /></a></td>
<td align=\"center\" width=\"80%\"
<span class=\"deviceMedLtGrayFont\">$artist_name</span>
<br /><br />
<a href=\"process_artist_idImages.php?artist_id=$artist_id&search=search\"/>
$artp</a>
<a href=\"process_artist_idImages.php?artist_id=$artist_id&search=search\"/>
$and$no</a>
<a href=\"process_artist_idImages.php?artist_id=$artist_id&search=search\"/>
$notep</a>
</td>
</tr>";
} //// end bracket for getting latest image link
} ///loop end for getting followed artist data
} ///end: if ($num>0) clause<code>
If I read your code correctly, I see you looping using data from query1 in the control structure, and a lookup on query2 within each loop. You are reusing the variables in your loop1 control structure for query2 ($num and the query handle ($res)) for the second loop. Probably not desirable within the loop.
You're sharing the variables $num and $res between the two queries ... your original properties will be overwritten when the second query is run. Use different property names for the inner query.
Example of problem:
$result = QueryA();
while( $row = $result->getRow() )
{
// -- You're overwriting the original result here ...
$result = QueryB();
// Next time the loop runs, it will run using the result from QueryB();
}
So change ...
$res = mysql_query($QUERY2);
$num = mysql_num_rows($res);
if($num>0 )
{
while($row = mysql_fetch_assoc($res))
{
mysql_fetch_assoc($res);
$image_link= $row['image_link'];
}
to
$res2 = mysql_query($QUERY2);
$num2 = mysql_num_rows($res2);
if($num2 > 0)
{
while($row2 = mysql_fetch_assoc($res2))
{
$image_link= $row2['image_link'];
}
this is a mess - as others have said you're using the same variables for different purposes.
That you're storing integers which seem to represent enumerations in a char field is bad.
You're iterating through the second result set to find the last record (from an unsorted result set!).
You only need one query - not 2.
SELECT f.artist_name, f.artist_id, f.dateLastSent....
SUBSTR(
MAX(
CONCAT(DATE_FORMAT(date_submit, '%Y%m%d%H%i%s'), a.image_link)
), 15) as img_link
FROM followArtist f
LEFT JOIN artWork a
ON (a.artist_id=f.artist_id AND a.make_avail = '1')
WHERE user_id = $user_id
GROUP BY f.artist_name, f.artist_id, f.dateLastSent....

Categories