Query 2 tables join - php

I want to get all the films whose genre is equal to ID_GENRE = 8. This is the query that I'm doing:
http://l4c.me/fullsize/2-tablas-1434140362.png
$query_GetSimilar = sprintf("SELECT * FROM z_movie,z_movie_genre ORDER BY z_movie.visits DESC WHERE z_movie_genre.id_genre = 8 LIMIT 18");
$GetSimilar = mysql_query($query_GetSimilar, conect::dbconect()) or die(mysql_error());
$row_GetSimilar = mysql_fetch_assoc($GetSimilar);
$totalRows_GetSimilar = mysql_num_rows($GetSimilar);
But I jump the next error
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE z_movie_genre.id_genre = 8 LIMIT 18' at line 1

Note:
WHERE should be first before ORDER BY
You can use INNER JOIN to get two connecting tables, assuming that they have connecting id/column.
For example, both z_movie and z_movie_genre table has the column id_genre to connect each other, you can try this:
$query_GetSimilar = sprintf("SELECT * FROM z_movie
INNER JOIN z_movie_genre ON z_movie.id_genre = z_movie_genre.id_genre
WHERE z_movie_genre.id_genre = 8
ORDER BY z_movie.visits DESC
LIMIT 18");

Make a simple search, and see if you can put 'order by' before 'where'.

ok, now my query is:
$query_GetSimilar = sprintf("SELECT * FROM z_movie,z_movie_genre WHERE z_movie_genre.id_genre=8 ORDER BY z_movie.visits DESC LIMIT 18");
$GetSimilar = mysql_query($query_GetSimilar, conect::dbconect()) or die(mysql_error());
$row_GetSimilar = mysql_fetch_assoc($GetSimilar);
$totalRows_GetSimilar = mysql_num_rows($GetSimilar);
but I do not have the expected results

Related

How to get Simple Table data in multiple SQL query php

i am trying to get data from one table, I received data from one table but there is some problem in my first table i have 6 query and i allow max 12 query in result. In my query i received repeat query in my SQL please have a look. sorry for bad English.
$query = $this->db->query("SELECT * FROM yt_sub,done WHERE yt_sub.current < yt_sub.total AND done.link != yt_sub.url AND done.uid != '$id' ORDER BY RAND() LIMIT 12");
Using this SQL query i received 12 rows but according to this sql only 6 rows pass throw it, but i received 6 rows.
Use DISTINCT see below eg
$query = $this->db->query("SELECT DISTINCT * FROM yt_sub,done WHERE yt_sub.current < yt_sub.total AND done.link != yt_sub.url AND done.uid != '$id' ORDER BY RAND() LIMIT 12");
use GROUP BY
$query = $this->db->query("SELECT * FROM yt_sub,done WHERE yt_sub.current < yt_sub.total AND done.link != yt_sub.url AND done.uid != '$id' GROUP BY done.uid ORDER BY RAND() LIMIT 12");

SQL query with join shows only one result

I have sql query which should shows all records from table swt_modules, but it shows only first row.
$query1 = mysql_query ("SELECT swt_modules.id, swt_modules.name, swt_exam_regs.name AS exam_regs
FROM `swt_modules`
JOIN `swt_exam_regs`
USING ( `id` )
WHERE swt_exam_regs.id = swt_modules.exam_regulation
ORDER BY swt_modules.name DESC , swt_modules.id DESC
LIMIT " . $limit . "");
while ($fetch1 = mysql_fetch_array ($query1))
{
...
}
I have in this table (swt_modules) 3 rows and in each of them value of field "exam_regulation" is 1. In table swt_exam_regs I have only 1 row with 2 columns - id and name. Swt_modules.id stores id number. Which join I should use to be able to see all records?
I would also suggest using mysqli or pdo instead of the now deprecated mysql.
$query1 = mysql_query ("
SELECT
swt_modules.id,
swt_modules.name,
swt_exam_regs.name AS exam_regs
FROM swt_modules
LEFT JOIN swt_exam_regs on swt_exam_regs.id = swt_modules.exam_regulation
ORDER BY
swt_modules.name DESC,
swt_modules.id DESC
LIMIT $limit");
You need to use LEFT JOIN instead of INNER JOIN. Change your query as below. Notice that, I have removed LIMIT since you are trying to fetch all rows.
SELECT swt_modules.id, swt_modules.name, swt_exam_regs.name AS exam_regs
FROM `swt_modules`
LEFT JOIN `swt_exam_regs`
ON swt_exam_regs.id = swt_modules.exam_regulation
ORDER BY swt_modules.name DESC , swt_modules.id DESC

Error when using "ORDER BY count" in query

I'm using the following query to display some information:
$result = mysqli_query ($con,"SELECT * FROM files,members,member_group WHERE files.member_id = members.member_id AND members.member_id = member_group.member_id AND group_id='$id' ORDER BY count DESC ");
My issue is it works fine when I leave out ORDER BY count DESC but when it is there I get the following error:
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /proj/co600/project/repo/public_html/select_field3.php on line 227
Count is a column in my database which records the number of times a publication is downloaded.
count is an aggregate function, so you need to surround it with backticks.
To get a clear cut picture of your error.. You need to change your code like..
$result = mysqli_query ($con,"SELECT * FROM files,members,member_group WHERE files.member_id = members.member_id AND members.member_id = member_group.member_id AND group_id='$id' ORDER BY count DESC ");
if(!$result)
{
die(mysqli_error($con));
}
You are having MySQL reserved keyword as column name in table.
Use Below query:
$result = mysqli_query ($con,"SELECT * FROM files,members,member_group WHERE files.member_id = members.member_id AND members.member_id = member_group.member_id AND group_id='$id' ORDER BY `count` DESC ");

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);
?>

SQL_CALC_FOUND_ROWS not working

so I execute query:
SELECT SQL_CALC_FOUND_ROWS * FROM table l LIMIT 10, 20;
which returns 20 rows from table, and there are a total 553 rows in the table
Then i immediately execute SELECT FOUND_ROWS();
But this instead only returns the number 1, despite the fact that there are 553 rows in my table (it's supposed to return 553, am I correct?)
what did I do wrong?
I suspect you have a syntax error, as names in SQL are not supposed to contain spaces. Try adding square brackets around [table 1], if that is the name of your table.
Remove your limit, I think there it is not showing 20 rows; it is showing only 10 rows.
$sql = "SELECT SQL_CAL_FOUND_ROWS * FROM users "; //don;t write table then table name
$result = mysql_query($sql);
//Use these according to your requirements:
$sql = "SELECT SQL_CAL_FOUND_ROWS * FROM users ";
$result = mysql_query($sql);
$sql = "SELECT FOUND_ROWS() AS `found_rows`";
$rows = mysql_query($sql);
$rows = mysql_fetch_assoc($rows);
$total_rows = $rows['found_rows'];

Categories