Copyright © 2005-2008 Andrea Tincani

AndreaPHP | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Previous Topic (Deleting only the old record)Next Topic (Newbie To PHP) New Topic New Poll Post Reply
AndreaPHP Forum : Database : Simplified SQL command
Poster Message
neutrall
Level: Scholar


Registered: 28-03-2004
Posts: 43

icon Simplified SQL command

I have the following function in a class :


<?php
function RaceName()    {
//Return the User Name
  
$id $this->Id;
  
//Fetch the user race Id
  
$sql "SELECT Race_Id FROM User WHERE Id=$id";
  
$result $this->db->query($sql);
  
$row $result->fetch();
  
$raceid $row['Race_Id'];
  
//Fetch the race Name
  
$sql "SELECT Name FROM Races WHERE Id=$raceid";
  
$result $this->db->query($sql);
  
$row $result->fetch();
  return 
$row['Name'];
 } 
?>



I'm sure they is a way to have only one SQL statement, can anyone give me a hand?

This is what I tried, but it didn't work :


<?php $sql "SELECT Name FROM Races, User WHERE User.Id=$id AND Id=User.Race_Id"?>



Any sugestion?

____________________________
A Stick give a wise man something to think about... and a fool, something to put in is mouth.

24-05-2005 at 04:33 PM
View Profile Send Email to User Show All Posts Visit Homepage ICQ | Quote Reply
admin
Level: Administrator


Registered: 04-04-2002
Posts: 35

icon Re: Simplified SQL command


<?php $sql="SELECT Name FROM Races INNER JOIN User ON Races.Id=User.Race_Id WHERE User.Id=$id"?>



or


<?php $sql "SELECT Name FROM Races, User WHERE User.Id=$id AND Races.Id=User.Race_Id"?>



maybe you only forgot to use Races.Id since the Id column appears in all two tables it's better to specify what table it belongs and also if Name is a field of all two tables use the Tabel.Field syntax

anyway the first syntax should be better and faster

____________________________
AndreaVB

24-05-2005 at 05:29 PM
View Profile Send Email to User Show All Posts Visit Homepage | Quote Reply
AndreaPHP Forum : Database : Simplified SQL command
Previous Topic (Deleting only the old record)Next Topic (Newbie To PHP) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
Partners: AndreaVB

Copyright © 2005-2008 Andrea Tincani

Powered by: tForum tForum Edition b0.92p1
Originally created by Toan Huynh (Copyright © 2000)
Enhanced by the tForumHacks team.