View Full Version : Joining querys?
03-26-2009, 02:38 PM
Hello, i was just wondering if theres a way to join a query? like this
"SELECT `id` FROM `users` WHERE `username` = '$user' AND SELECT `id` from `films` WHERE `title` = '$new_row['last_movie']";
something like this?
03-26-2009, 02:50 PM
That won't work.
What is it you're trying to accomplish, are the tables related to each other are they completely independant?
03-26-2009, 02:51 PM
Do you have any field relating your two tables?
03-26-2009, 03:10 PM
Well what it is is that im doing two query's, and ive been told it's unsafe and i was just wondering if there's an easier way there all on the same database but different tables
<hr color='silver' width='100%'/>
<b>Last Movie:</b> <a href="#"><?php
$id = $_GET['id'];
$new = "SELECT * FROM `users` WHERE `id` = '".$id."' ORDER BY '".$id."' DESC LIMIT 10";
$new_res = mysql_query($new) or die(mysql_error());
$new_row = mysql_fetch_assoc($new_res);
$imov = "SELECT `id` FROM `films` WHERE `title` = '".$new_row['last_movie_id']."'";
$imov_res = mysql_query($imov) or die(mysql_error());
$imov_row = mysql_fetch_array($imov_res);
echo '<a href="preview.php?id=', $imov_row['id'] ,'">',$new_row['last_movie_id'],'</a>';
03-26-2009, 05:43 PM
The 'unsafe' part is you're $id variable.
The tables are however related where films.title = users.last_movie_id.
// I assumed $id is a string since its string quoted in the query:
$_GET['id'] = stripslashes($_GET['id']);
$id = mysql_real_escape_string($_GET['id']);
$sQry = 'SELECT u.last_movie_id, f.id FROM users u
films f on (f.title = u.last_movie_id)
WHERE u.id = \'' . $id . '\'
ORDER BY u.id DESC';
//LIMIT 10'; // This limit here is unecessary, since I would assume that last_movie_id has a 0 - 1 multiplicity
// edit: I guess I should add the rest
$obQry = @mysql_query($sQry) or die(mysql_error());
if (mysql_num_rows($obQry) == 1) // This should be true so long as the user can only have one last movie
$result = mysql_fetch_assoc($obQry);
printf('<a href="preview.php?id=%s">%s</a>', $result['id'], $result['last_movie_id']);
Powered by vBulletin® Version 4.2.2 Copyright © 2016 vBulletin Solutions, Inc. All rights reserved.