I'm trying to do a query based into a value obtained in a previous query. Something like that:
$variableid = 100;
$query_prev = "SELECT query FROM queries_table WHERE id = 1";
$result_prev = pg_query($pg,$query_prev);
$row_prev = pg_fetch_array($result_prev);
$final_query = $row_prev['query'];
$row_prev['query'] value would be "SELECT * FROM other_table WHERE id = $variableid";
$final_query value at this point is: "SELECT * FROM other_table WHERE id = $variableid"
/* but that I want is this value: */ "SELECT * FROM other_table WHERE id = 100"
Use if statement before you do the next select so:
<?php if ($row_prev['query']= 100){
SELECT .......... etc.
}else
SELECT from other
?>
/Hope that's what you wanted
Solved:
$variableid = 100;
$query_prev = "SELECT query FROM queries_table WHERE id = 1";
$result_prev = pg_query($pg,$query_prev);
$row_prev = pg_fetch_array($result_prev);
$query = $row_prev['query'];
eval("\$final_query = \"$query\";");
Related
I have written a code in PHP and not able to get a response from the code. Following is my code.
<?php
include_once("conectionFile.php");
$db = new Data();
$con = $db->Conn();
$val = $_REQUEST;
$lclQuery = "SELECT * FROM student_details WHERE st_id = ".$val."";
$lclResult = $con->query($lclQuery);
if($row = $lclResult->fetchAll(PDO::FETCH_ASSOC)) {
echo $res = json_encode($row);
}
?>
$lclQuery = "SELECT * FROM student_details WHERE st_id = ".$val."";
You need to change your query as it is not correct also $val is array so you need to use correct key there in order to get value for example $val = $_REQUEST['some_id'] then change query like
$lclQuery = "SELECT * FROM student_details WHERE st_id = '".$val."'";
I am doing sum with the below query but it is not giving result properly.
If there are four items and it is showing the result like:
1.000 2.000 3.000 4.000 and it should be like 10.000
I don't know where I am mistaken please help.
<?php
$order_temp = mysql_query("select * from temp_cart
where item_id = '".$mitem_idC."' and ses_mem=113 order by id");
while ($torder = mysql_fetch_array($order_temp)) {
$prITTC = $torder['item_id'];
$qtyT = $torder['qty'];
$chTP = mysql_query("
select * from temp_choices
where item_id = '".$prITTC."'
AND ses_mem = 113
AND status = 1
");
while($chGET = mysql_fetch_array($chTP)){
$fID = $chGET['id'];
$field = $chGET['choice_id'];
$order_tempCHP = mysql_query("
select sum(price) as total, id, ename, choice_id, item_id, price
from choice_price
WHERE
id IN('$field')
");
while ($torderCP = mysql_fetch_assoc($order_tempCHP)){
$totalCH = $torderCP['total'];
$tsl = $totalCH+($qtyT*$prIDTC);
$altsl = number_format($tsl, 3, '.', '');
echo $altsl;
} }
}
?>
according to my question above after trying and trying i found the solution and resolved my problem according to below code:
Thanks to #John Kugelman at MySQL query using an array
$order_temp = mysql_query("select * from temp_cart
where item_id = '".$mitem_idC."' and ses_mem=113 order by id");
while ($torder = mysql_fetch_array($order_temp)) {
$prITTD = $torder['id'];
$prITTC = $torder['item_id'];
$chTPaa = mysql_query("
select choice_id
FROM temp_choices
WHERE item_id = '$prITTC'
AND ses_mem = 113
AND status = 1
group by choice_id
");
while ($chGETaa = mysql_fetch_assoc($chTPaa)){
$temp[] = $chGETaa['choice_id'];
}
$thelist = implode(",",$temp);
$order_tempCHP = mysql_query("
select sum(price) as total
from choice_price
WHERE
id IN ($thelist)
AND
item_id = '".$prITTC."'
");
while($torderCP = mysql_fetch_assoc($order_tempCHP)){
$totalCH = $torderCP['total'];
$tsl = $totalCH+($qtyT*$prIDTC);
$altsl = number_format($tsl, 3, '.', '');
echo $altsl;
} }
I’m using this code:
$date = date('Y-m-d');
$selStat = "SELECT obqva_id, COUNT(*) as broqch FROM statist WHERE date='$date' GROUP BY obqva_id ORDER BY COUNT(*) DESC LIMIT 8 ";
$queryStat = mysqli_query($conn, $selStat);
while ($rowStat = mysqli_fetch_array($queryStat)) {
$count = $rowStat['broqch'];
$id = $rowStat['obqva_id'];
echo $id.' - '.$count.'<br>';
$selBest = "SELECT * FROM view WHERE id='$id' GROUP BY $count ";
$queryBest = mysqli_query($conn, $selBest);
**$rowView = mysqli_fetch_array($queryBest);** this problem !
$selImage = "SELECT * FROM upload WHERE obqva_id='$id'";
$queryImage = mysqli_query($conn, $selImage);
$rowImage = mysqli_fetch_array($queryImage);
?>
Which produces this output:
First and next result has a problem, three and next have no problem... why?
First number 41 is ID next number total view.
it seems that the query is not valid, or there was no result.
thats why you get a bool instead of a resource.
you can filter that by putting your mysqli_query function in an IF statement, like this:
$selBest = "SELECT * FROM view WHERE id='$id' GROUP BY $count ";
if($queryBest = mysqli_query($conn, $selBest)){
$rowView = mysqli_fetch_array($queryBest);
}
else{
$rowView = false;
}
That's that, now for the query. You are trying to group the result on a number:
SELECT * FROM view WHERE id='$id' GROUP BY $count
Not sure why you want to group, as it seems that you only want to get some info on a specific id. In that case, i would get rid of the GROUP BY statement.
I have a table schedules that contains sched_id, sc_id1, sc_id2, sc_id3, sc_id4, sc_id5, sc_id6, sc_id7, sc_id8, sc_id9, sc_id10, sched_name.
I have also table subject_current that has sc_id, sl_id, schoolyear, semister, etc... sc_id1 - scid10 is a "foreign key" from sc_id of table subject_current
Also, I have a table subject_list with sl_id, subject_code, subject_description, subject_prereq. sl_id from table subject_current is a "foreign key" from sl_id of table subject_list.
Now, what I want to do is to "echo" the subject_description from table subject_list only giving me the value of sc_id1 - sc_id10 from table schedules.
This code doesn't work:
for($jaa = 1;$jaa < 11;$jaa++){
$s_scid = "s_scid".$jaa;
$s_sublist = mysql_query("SELECT * FROM subject_current WHERE sc_id='$s_scid'");
while($rows_ss = mysql_fetch_assoc($s_sublist)){
$ss_slid = $rows_ss['sl_id'];
$ssl_sublist = mysql_query("SELECT * FROM subject_list WHERE sl_id='$ss_slid'");
while($rows_ssl = mysql_fetch_assoc($ssl_sublist)){
$ssl_slid = $rows_ssl['sl_id'];
$ssl_subdesc = $rows_ssl['subject_description'];
}
}
echo $ssl_subdesc;
}
EDIT
This is what I want to exactly happen:
$s_scid1 = $rows_s['sc_id1']; // which is a value of 1
$s_scid2 = $rows_s['sc_id2']; // which is a value of 2
$s_sublist = mysql_query("SELECT * FROM subject_current WHERE sc_id='$s_scid1'");
while($rows_ss = mysql_fetch_assoc($s_sublist)){
$ss_slid1 = $rows_ss['sl_id'];
$ssl_sublist = mysql_query("SELECT * FROM subject_list WHERE sl_id='$ss_slid1'");
while($rows_ssl = mysql_fetch_assoc($ssl_sublist)){
$ssl_slid1 = $rows_ssl['sl_id'];
$ssl_subdesc1 = $rows_ssl['subject_description'];
echo $ssl_subdesc1;
}
}
$s_sublist2 = mysql_query("SELECT * FROM subject_current WHERE sc_id='$s_scid2'");
while($rows_ss2 = mysql_fetch_assoc($s_sublist2)){
$ss_slid2 = $rows_ss2['sl_id'];
$ssl_sublist2 = mysql_query("SELECT * FROM subject_list WHERE sl_id='$ss_slid2'");
while($rows_ssl2 = mysql_fetch_assoc($ssl_sublist2)){
$ssl_slid2 = $rows_ssl2['sl_id'];
$ssl_subdesc2 = $rows_ssl2['subject_description'];
echo $ssl_subdesc2;
}
}
This is a pain to write it 10 times. So I want to loop it. But someone told me it's bad and told me about INNER JOIN. But how can I with INNER JOIN?
The way you describe it, you should do something like:
for($jaa = 1;$jaa < 11;$jaa++){
$s_scid = "s_scid".$jaa;
$the_query = "SELECT sl_id, subject_description FROM subject_list sl JOIN subject_current sc ON sl.sl_id=sc.$s_scid";
/* the above should generate a JOIN with a particular element from your master table */
$ssl_sublist = mysql_query($the_query);
while($rows_ssl = mysql_fetch_assoc($ssl_sublist)){
$ssl_slid = $rows_ssl['sl_id'];
$ssl_subdesc = $rows_ssl['subject_description'];
echo $ssl_subdesc;
}
}
i have the following query:
$sql_select_todo =
"SELECT * FROM
to_do_items
WHERE
id_project = ".$_SESSION['selected_projectId']."'
AND
id IN ('".$alleTaken."')";
$query_select_todo = mysql_query($sql_select_todo) or die(mysql_error());
while($fetch_todo = mysql_fetch_assoc($query_select_todo)) {
echo $fetch_todo['name'];
}
When i echo the query, i get this:
SELECT * FROM to_do_items WHERE id_project = '7401' AND id IN ('10193,12848')
But the output will only give back one name ($fetch_todo['name'])
But there are 2 id's, so i should get 2 names. What do i do wrong here?
Remove single quote in IN query
Change
$sql_select_todo =
"SELECT * FROM
to_do_items
WHERE
id_project = ".$_SESSION['selected_projectId']."'
AND
id IN ('".$alleTaken."')";
To
$sql_select_todo =
"SELECT * FROM
to_do_items
WHERE
id_project = ".$_SESSION['selected_projectId']."'
AND
id IN (".$alleTaken.")";