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.")";
Related
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'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\";");
I have a URL that is dynamically generated:
https://mywebsite.co.uk/report.php?taskid=25&rep=1&rep=2&rep=3
I can retrieve the variable for the task ID and rep fine:
if (isset($_GET['taskid'])) { $taskid = $_GET['taskid']; }
if (isset($_GET['rep'])) { $referenceID = $_GET['rep']; }
What I'm trying to do is create an SQL statement on the page that selects a row based on the rep number in the URL. For example:
SELECT TASK_ID, ID, NAME FROM mytable WHERE TASK_ID = $taskid AND ID = $referenceID
However, when I echo the result of $referenceID it is always the last rep, so in this case 3. How do I select the rows from the database where ID = 1,2 & 3?
I then want to display each row, so it would be something like:
<table>
$result = mysqli_query($con,"SELECT TASK_ID, ID, NAME FROM mytable WHERE TASK_ID = $taskid AND ID = $referenceID");
while($row = mysqli_fetch_array($result))
{
$ID = $row['ID'];
$NAME = $row['NAME'];
print "<tr><td>$ID</td><td>$NAME</td></tr>";
}
</table>
This query should return 3 rows in the table with the ID AND NAME in each row.
Your help would be appreciated.
First, you need to change your parameter name from rep to rep[]
This will cause PHP $_GET['rep'] to return an array.
Then you need to implode the array to obtain a string with commas:
if (isset($_GET['rep'])) {
$referenceID = implode(',',$_GET['rep']);
}
You have to change your SQL syntax to this:
SELECT TASK_ID, ID, NAME FROM mytable WHERE TASK_ID = $taskid AND ID IN ($referenceID)
try this query
$result = mysqli_query($con,"SELECT TASK_ID, ID, NAME FROM mytable WHERE TASK_ID = $taskid AND (ID <= $referenceID and ID>= 1)");
You should use array-style URL parameters, e.g.
https://mywebsite.co.uk/report.php?taskid=25&rep%5B%5D=1&rep%5B%5D=2&rep%5B%5D=3
Then $_GET['rep'] will be an array, and you can do:
$referenceIDs = implode(',', array_map(array($con, 'real_escape_string'), $_GET['rep']));
$sql = "SELECT TASK_ID, ID, NAME
FROM mytable
WHERE TASK_ID = $taskid
AND ID IN ($referenceIDs)";
I have this very simple function:
function getCatName($id){
$sql = "SELECT * FROM biznet_category WHERE ID ='".$id."';";
$res = mysql_query ($sql) or die (mysql_error ());
$row = mysql_fetch_assoc ($res);
$name = $row["Name"];
return $name;
}
So with this function I should be able to get the category name, but it doesn't work with the parameter. If I put 8 or 9, the categoryname is displayed correctly.
The id is also passed on like it should, when I print it out, it shows 8 or 9.
I know the solution is quite simple, I just don't see it.
To fix remove the quotes and check the column name for case id or ID. Since the query string is in double quotes you don't have to use the . join
$sql = "SELECT * FROM biznet_category WHERE ID = $id";
You can use curly brackets which I find easier to read
$sql = "SELECT * FROM biznet_category WHERE ID = {$id}";
If you were querying a string rather than an integer you can simply do
$sql = "SELECT * FROM biznet_category WHERE ID = '{$id}'";
$sql = "SELECT * FROM biznet_category WHERE ID ='".$id."';";
To
$sql = "SELECT * FROM biznet_category WHERE ID = ".$id;
Try this
$sql = "SELECT * FROM biznet_category WHERE ID = ".$id;
Is the column name ID spelt correctly?