Today's Posts Follow Us On Twitter! TFL Members on Twitter  
Forum search: Advanced Search  
Navigation
Marketplace
  Members Login:
Lost password?
  Forum Statistics:
Forum Members: 24,254
Total Threads: 80,792
Total Posts: 566,472
There are 1636 users currently browsing (tf).
 
  Our Partners:
 
  TalkFreelance     Design and Development     Programming     PHP and MySQL :

Repeating and Single Queries

Thread title: Repeating and Single Queries
Closed Thread    
    Thread tools Search this thread Display Modes  
07-07-2008, 06:03 AM
#1
Jako is offline Jako
Jako's Avatar
Status: Jakowenko.com
Join date: Jun 2005
Location: Michigan
Expertise:
Software:
 
Posts: 2,199
iTrader: 3 / 100%
 

Jako is on a distinguished road

  Old  Repeating and Single Queries

I was running this query on my website.

PHP Code:
<? include('variables.php'); ?>
<? 
$id
=$_GET['id'];
$query "SELECT * FROM players, national WHERE players.national='$id' AND national.national='$id'";
if (
$r mysql_query ($query)) {
while (
$row mysql_fetch_array ($r)) { 

?>
and at the bottom of the page I had something like

PHP Code:
<? } } die() ?>
This loop was pulling everything from the database that met that query. So everything was being repeated though I only wanted a section of the page repeat, while the rest only gets posted a single time.

How would I go about doing this?

01-12-2009, 07:24 PM
#2
trench is offline trench
Status: I'm new around here
Join date: Nov 2008
Location:
Expertise:
Software:
 
Posts: 13
iTrader: 0 / 0%
 

trench is on a distinguished road

  Old

Read up on INNER/LEFT/RIGHT JOIN mysql querys.

01-19-2009, 04:59 PM
#3
paitken is offline paitken
Status: I'm new around here
Join date: Nov 2008
Location: Tampa, FL, USA
Expertise:
Software:
 
Posts: 14
iTrader: 0 / 0%
 

paitken is on a distinguished road

Send a message via ICQ to paitken Send a message via AIM to paitken Send a message via MSN to paitken Send a message via Yahoo to paitken

  Old

It would be a little better if you would have named your columns a little better.
I would name the 'national' column inside of 'national', 'national_id' or 'id', and make it auto increment.

It's a bit difficult to understand what you're doing. The code you have would of course get all the results from the query. If you just wanted ONE, you would do LIMIT 1 at the end of the query.

trench: A join in this case would be overkill. It would be just as fast if he had the columns properly indexed. It would be useful if he were trying to do multiple joins.

02-20-2009, 02:57 PM
#4
ditch182 is offline ditch182
Status: Junior Member
Join date: Jul 2008
Location: NC
Expertise:
Software:
 
Posts: 70
iTrader: 0 / 0%
 

ditch182 is on a distinguished road

  Old

paitken is right, here. What you've got is an implicit join anyway. You could either use LIMIT to return only the first result, or think about using foreach to iterate through your array of results.

Closed Thread    


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

  Posting Rules  
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump:
 
  Contains New Posts Forum Contains New Posts   Contains No New Posts Forum Contains No New Posts   A Closed Forum Forum is Closed