Can i write two foreach in only one? - php

In my code I have this:
$im = $database->query("SELECT * FROM cms_messaggi_importanti WHERE messo_da = :id ORDER BY ID DESC", array("id"=>$functions->Utente("id")));
foreach($im as $imp){
$mex_i = $database->query("SELECT * FROM cms_messaggi WHERE id = :id ORDER BY ID DESC", array("id"=>$imp['id_mex']));
foreach($mex_i as $mex_imp){
}
}
Can I write this code in only one? Because I have to use a lot of variable with this method. Is there a solution to my problem? For example, using "JOIN"?

You can (and should) do your query in one go, and then iterate over those results:
$im = $database->query("
SELECT *
FROM cms_messaggi_importanti mi
LEFT JOIN cms_messaggi m ON m.id = mi.id_mex
WHERE messo_da = :id
ORDER BY mi.ID DESC,
m.ID DESC",
array("id"=>$functions->Utente("id")));
foreach($im as $imp){
//...
}
You will probably need to replace the SELECT * by a more precise column list, which will be the columns available in the result set.

Related

ORDER BY on joined table

I am attempting to join two tables, and also order the query results by one of the columns within the table I am joining. Everything works great until I add ORDER BY cm.num, at which point I get this error:
Call to a member function fetch_assoc() on boolean
Here is my full query:
SELECT *
FROM course
JOIN cm ON (course = cm.course)
WHERE title LIKE '%$searchTerm%'
LIMIT $limit
ORDER BY cm.num
Anyone have any suggestions?
EDIT:
Full code as requested:
function getCourses($searchTerm) {
$mysqli = new mysqli('localhost', 'scott', 'tiger', 'courses');
if ($mysqli->connect_errno) {
header("HTTP/1.1 500 Internal Server Error");
die("Failed to connect to MySQL: {$mysqli->connect_error}");
}
$mysqli->set_charset('utf8');
$courses = [];
$limit = $searchTerm == '' ? 1000 : 10;
$res = $mysqli->query("SELECT * FROM course JOIN cm ON (course = cm.course) WHERE title LIKE '%$searchTerm%' ORDER BY cm.num LIMIT $limit");
while($row = $res->fetch_assoc()) {
$course = new Course(
$row['id'],
$row['title'],
$row['href'],
$row['level'],
$row['award'],
$row['summary'],
$row['dept'],
$row['subject'],
$row['overview'],
$row['wyl'],
$row['careers']
);
array_push($courses, $course);
}
return $courses;
}
Put the limit after the order by and specify the columns specifically
i.e. JOIN cm ON (course.course = cm.course)
SELECT *
FROM course
JOIN cm ON (course.course = cm.course)
WHERE title LIKE '%$searchTerm%'
ORDER BY cm.num
LIMIT $limit
You have (course = cm.course) in your code. course is a table, and you want to join two columns. so it should be something like (course.itsColumn = cm.course)
Let me know if that helped!
Try to add an alias. It's recommended that every table in a join is referenced via a alias.
SELECT *
FROM course AS c
LEFT JOIN cm AS cm ON (c.course = cm.course)
WHERE title LIKE '%$searchTerm%'
LIMIT $limit
ORDER BY cm.num
You are using self join in your code. So can you please check with the following code :
SELECT * FROM course c
JOIN course cm ON (c.course = cm.course)
WHERE c.title LIKE '%$searchTerm%'
ORDER BY cm.num LIMIT $limit

Creating a subquery with mysqli in PHP to fetch array last 10 results in ascending order

I thought this would be simple but I'm having a tough time figuring out why this won't populate the the data array.
This simple query works fine:
$queryPrice = "SELECT price FROM price_chart ORDER BY id ASC LIMIT 50";
$resultPrice = mysqli_query($conn, $queryPrice);
$data = array();
while ($row = mysqli_fetch_array($resultPrice)) {
$data[] = $row[0];
}
But instead I want it to choose the last 10 results in Ascending order. I found on other SO questions to use a subquery but every example I try gives no output and no error ??
Tried the below, DOESN'T WORK:
$queryPrice = "SELECT * FROM (SELECT price FROM price_chart ORDER BY id DESC LIMIT 10) ORDER BY id ASC";
$resultPrice = mysqli_query($conn, $queryPrice);
$data = array();
while ($row = mysqli_fetch_array($resultPrice)) {
$data[] = $row[0];
}
I also tried specifying the table name again and using the IN, also doesn't work:
$queryPrice = "SELECT price FROM price_chart IN (SELECT price FROM price_chart ORDER BY id DESC LIMIT 10) ORDER BY id";
$resultPrice = mysqli_query($conn, $queryPrice);
$data = array();
while ($row = mysqli_fetch_array($resultPrice)) {
$data[] = $row[0];
}
In both examples my array is blank instead of returning the last 10 results and there are no errors, so I must be doing the subquery wrong and it is returning 0 rows.
The subquery doesn't select the id column, so you can't order by it in the outer query. Also, MySQL requires that you assign an alias when you use a subquery in a FROM or JOIN clause.
$queryPrice = "SELECT *
FROM (SELECT id, price
FROM price_chart
ORDER BY id DESC LIMIT 10
) x ORDER BY id ASC";
$resultPrice = mysqli_query($conn, $queryPrice) or die (mysqli_error($conn));
$data = array();
while ($row = mysqli_fetch_assoc($resultPrice)) {
$data[] = $row['price'];
}
You would have been notified of these errors if you called mysqli_error() when the query fails.
Your second query is the closest. However you need a table alias. (You would have seen this if you were kicking out errors in your sql. Note you will need to add any field that you wish to order by in your subquery. In this case it is id.
Try this:
SELECT * FROM (SELECT price, id
FROM price_chart ORDER BY id DESC LIMIT 10) as prices
ORDER BY id ASC
You must have errors, because your SQL queries are in fact incorrect.
First, how to tell you have errors:
$resultPrice = mysqli_query (whatever);
if ( !$resultprice ) echo mysqli_error($conn);
Second: subqueries in MySQL need aliases. So you need this:
SELECT * FROM (
SELECT id, price
FROM price_chart
ORDER BY id DESC LIMIT 10
) AS a
ORDER BY id ASC";
See the ) AS a? That's the table alias.

Inner/Left join with two different where clauses

i'm in the process of joining two tables together under two different conditions. For primary example, lets say I have the following nested query:
$Query = $DB->prepare("SELECT ID, Name FROM modifications
WHERE TYPE =1 & WFAbility = '0'");
$Query->execute();
$Query->bind_result($Mod_ID,$Mod_Name);
and this query:
$Query= $DB->prepare("SELECT `ModID` from `wfabilities` WHERE `WFID`=?");
$Query->bind_param();
$Query->execute();
$Query->bind_result();
while ($Query->fetch()){ }
Basically, I want to select all the elements where type is equal to one and Ability is equal to 0, this is to be selected from the modifications table.
I further need to select all the IDs from wfabilities, but transform them into the names located in modifications where WFID is equal to the results from another query.
Here is my current semi-working code.
$Get_ID = $DB->prepare("SELECT ID FROM warframes WHERE Name=?");
$Get_ID->bind_param('s',$_GET['Frame']);
$Get_ID->execute();
$Get_ID->bind_result($FrameID);
$Get_ID->fetch();
$Get_ID->close();
echo $FrameID;
$WF_Abilties = $DB->prepare("SELECT ModID FROM `wfabilities` WHERE WFID=?");
$WF_Abilties->bind_param('i',$FrameID);
$WF_Abilties->execute();
$WF_Abilties->bind_result($ModID);
$Mod_IDArr = array();
while ($WF_Abilties->fetch()){
$Mod_IDArr[] = $ModID;
}
print_r($Mod_IDArr);
$Ability_Name = array();
foreach ($Mod_IDArr AS $AbilityMods){
$WF_AbName = $DB->prepare("SELECT `Name` FROM `modifications` WHERE ID=?");
$WF_AbName->bind_param('i',$AbilityMods);
$WF_AbName->execute();
$WF_AbName->bind_result($Mod_Name);
$WF_AbName->fetch();
$Ability_Name[] = $Mod_Name;
}
print_r($Ability_Name);
See below:
SELECT ModID,
ID,
Name
FROM modifications M
LEFT JOIN wfabilities WF
ON WF.ModID = M.ID
WHERE TYPE =1 & WFAbility = '0'
To do this, you need to join your tables, I'm not quite sure what you are trying to do so you might have to give me more info, but here is my guess.
SELECT ID, Name, ModID
FROM modifications
JOIN wfabilities
ON WFID = ID
WHERE TYPE = '1'
AND WFAbility = '0'
In this version I am connecting the tables when WFID is equal if ID. You will have to tell me exactly what is supposed to be hooking to what in your requirements.
To learn more about joins and what they do, check this page out: MySQL Join
Edit:
After looking at your larger structure, I can see that you can do this:
SELECT modifications.Name FROM modifications
JOIN wfabilities on wfabilities.ModID = modifications.ID
JOIN warframes on warframes.ID = wfabilities.WFID
WHERE warframes.Name = 'the name you want'
This query will get you an array of the ability_names from the warframes name.
This is the query:
"SELECT A.ID, A.Name,B.ModID,C.Name
FROM modifications as A
LEFT JOIN wfabilities as B ON A.ID = B.WFID
LEFT JOIN warframes as C ON C.ID = B.WFID
WHERE A.TYPE =1 AND A.WFAbility = '0' AND C.Name = ?"

Is it ok to query inside a while loop?

I have two tables in one database. I am querying the first table limit by 10 then loop the results. And inside the while loop, I am doing again another query using a data from the first query as a parameter. Here is an example of the script:
<?php
$con = mysql_connect(host,username,password);
mysql_select_db(game_server);
//My first query
$q1 = mysql_query('SELECT * FROM game_characters ORDER BY score DESC LIMIT 10');
while($character = mysql_fetch_object($q1)){
//My second query
$q2 = mysql_query('SELECT * FROM game_board WHERE id="'.$character->id.'"');
$player = mysql_fetch_object($q2);
}
?>
So if I have a result of 100 rows, then the second query will execute 100 times. And I know it is not good. How can I make it better. Is there a way to do everything in one query? What if there is another query inside the while loop where a data from the second query as a parameter is used?
P.S.: I am doing a rankings system for an online game.
You can do it in one query if you use JOINs.
SELECT * FROM game_board AS b
LEFT JOIN game_characters AS c ON b.id = c.id
ORDER BY c.score DESC
LIMIT 10
You can also use nested query
SELECT * FROM game_board AS b WHERE
id IN (SELECT id FROM game_characters AS c ORDER BY score DESC LIMIT 10)
You can also put all game_character.id into an array, and use
$sql = "SELECT * FROM game_board AS b WHERE b.id IN (" . implode(', ', $game_character_ids) . ")";
Why not using JOIN?
This way there will be no queries within the while loop:
$con = mysql_connect(host,username,password);
mysql_select_db(game_server);
//My first query
$q1 = mysql_query('
SELECT *
FROM game_characters gc
LEFT JOIN game_board gb ON gc.id = gb.id
ORDER BY score DESC
LIMIT 10
');
while($character = mysql_fetch_object($q1)){
// do Your stuff here, no other query...
}
A better approach here would be to collect all the IDs in a concatenated string str in form 'id1', 'id2', 'id3', ... and use:
select * from game_board where id in (str)
What about if you do something like the following:
<?php
$con = mysql_connect(host,username,password);
mysql_select_db(game_server);
//My first query
$q1 = mysql_query('SELECT * FROM game_characters ORDER BY score DESC LIMIT 10');
while($character = mysql_fetch_object($q1)){
//My second query
$characters .= " ' $character->id ' ,"
}
$q2 = mysql_query("SELECT * FROM game_board WHERE id in (substr($characters,0,strlen($characters - 2))");
$player = mysql_fetch_object($q2);
?>

next / previous pagination query with parameters php mysql with parameters

I have the following problem: here is the structure of my tables
and here is my code:
$SQL_NEXT_PROJECT_ID ="SELECT id FROM projects WHERE id < '".$id_project."' and project_types_id='".$project_type_id."' ORDER BY id DESC LIMIT 1";
$conn->query($SQL_NEXT_PROJECT_ID);
foreach ($conn->query($SQL_NEXT_PROJECT_ID) as $rowProjectNext) {
$next_project_id = $rowProjectNext['id'];
}
$SQL_PREVIOUS_PROJECT_ID ="SELECT id FROM projects WHERE id > '".$id_project."' and project_types_id='".$project_type_id."' ORDER BY id ASC LIMIT 1";
$conn->query($SQL_PREVIOUS_PROJECT_ID);
foreach ($conn->query($SQL_PREVIOUS_PROJECT_ID) as $rowProjectPrevious) {
$previous_project_id = $rowProjectPrevious['id'];
}
the thing is that i need to put the query with an aditional condition from the 'images' table i have to evaluate the main field ='1', i have been trying to do it with a inner join but it doesnt seem to work, here i attach the query that i have been trying
$SQL_NEXT_PROJECT_ID ="SELECT projects.id FROM projects INNER JOIN images WHERE projects.id > '".$id_project."' and project_types_id='1' and main='1' ORDER BY projects.id DESC LIMIT 1";
Basically what i need to do is to create next / previous links to projects of certain type (project_types_id) based on the current id of the project ($id_project) taking into consideration the fact that the project should have a image that has the main field equal to '1'
hope i explained myself well...
Well youre not joining on anything... you need an ON clause.
SELECT pr.id FROM projects pr
INNER JOIN images img ON (pr.id = img.projects_id AND img.main = 1)
WHERE pr.id > ?
AND pr.project_types_id = 1
ORDER BY pr.id DESC LIMIT 1
at the end i did it like this
$_nextPrevious = array();
$SQL_SEARCH_VALUES="SELECT images.projects_id AS projectId FROM projects INNER JOIN images ON ( projects.id = images.projects_id ) AND project_types_id = '".$project_type_id."' AND images.main = '1' LIMIT 0 , 30";
$conn->query($SQL_SEARCH_VALUES);
foreach($conn->query($SQL_SEARCH_VALUES) as $rowNextPrevious) {
$value = $rowNextPrevious['projectId'];
array_push($_nextPrevious, $value);
}
$currentValue = array_search($id_project, $_nextPrevious);
$next = $currentValue + 1;
$previous = $currentValue - 1;
$next_project_id = $_nextPrevious[$next];
$previous_project_id = $_nextPrevious[$previous];
return array($project_name,$project_description,$project_path, $_images, $next_project_id, $previous_project_id);
using an array instead of two searches...

Categories