Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.
Moderator: General Moderators
The-Master
Forum Commoner
Posts: 45 Joined: Sun Aug 07, 2005 9:51 am
Location: Israel
Post
by The-Master » Wed Aug 10, 2005 6:14 pm
I am working on an online text-based RPG as i said in my first topic, so i want to show the player his information, so i made a script but it doesn't realy work
... here is the script:
Code: Select all
{
// I THINK THE PROBLEM IS FROM HERE ----->
$sql1 = "SELECT * FROM users WHERE user = '$_SESSION[username]'";
$sql2 = "SELECT * FROM races WHERE race = '$_SESSION[username]'";
$sql3 = "SELECT * FROM classes WHERE class = '$_SESSION[username]'";
$sql4 = "SELECT * FROM RPG WHERE level = '$_SESSION[username]'";
// UNTIL HERE <-----
$result1 = mysql_query($sql1);
$result2 = mysql_query($sql2);
$result3 = mysql_query($sql3);
$result4 = mysql_query($sql4);
$a=mysql_fetch_array($result1);
$b=mysql_fetch_array($result2);
$c=mysql_fetch_array($result3);
$d=mysql_fetch_array($result4);
// this is all the information i want to view...
$id=$a["id"];
$user=$a["user"];
$race=$b["race"];
$class=$c["class"];
$level=$d["level"];
$HP=$d["HP"];
$MP=$d["MP"];
$offence=$d["offence"];
$defence=$d["defence"];
$speed=$d["speed"];
$wisdom=$d["wisdom"];
$weapon=$d["weapon"];
$weapon2=$d["weapon2"];
$armor=$d["armor"];
$item=$d["item"];
$money=$d["money"];
$resource=$d["resource"];
$clan=$d["clan"];
$clan_rank=$d["clan_rank"];
// THIS IS THE ECHO PART...
echo "<b> $user </b><br>
גזע: $race<br>
רמה: $level<br>
ניסיון: $cXP /<b> $XP </b><br>
חיים: $cHP /<b> $HP </b><br>
--------------<br>
עוצמה: $cMP /<b> $MP</b><br>
התקפה: $offence <br>
הגנה: $defence <br>
מהירות: $speed <br>
חוכמה: $wisdom <br>
--------------<br>
נשק: $weapon <br>
נשק שני: $weapon2 <br>
שיריון: $armor <br>
פריט מיוחד: $item <br>
--------------<br>
כסף: $money
משאבים: $resource
--------------<br>
שבט: $clan
תפקיד בשבט: $clan_rank ";
}
what's the
bug here
?
P.S: don't mind the "unknown" chars they are in hebrew...
feyd | you can use the
Last edited by
The-Master on Wed Aug 10, 2005 6:22 pm, edited 1 time in total.
s.dot
Tranquility In Moderation
Posts: 5001 Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana
Post
by s.dot » Wed Aug 10, 2005 6:21 pm
Code: Select all
$sql1 = "SELECT * FROM users WHERE user = '".$_SESSION['username']."'";
Notice I broke the php string and concatenated your $_SESSION['username']
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
The-Master
Forum Commoner
Posts: 45 Joined: Sun Aug 07, 2005 9:51 am
Location: Israel
Post
by The-Master » Wed Aug 10, 2005 6:26 pm
i did other similar codes in the same way i did the sql1=... and it worked, so i am not sure if this works, but i'll try...
The-Master
Forum Commoner
Posts: 45 Joined: Sun Aug 07, 2005 9:51 am
Location: Israel
Post
by The-Master » Thu Aug 11, 2005 4:21 pm
that wasn't the problem it didn't work both ways, so if anyone knows a function that takes a field from a table.
e.g: i have table
"races" and fields
"race" and
"id" , every user has an ID so the race matches to the ID and it makes a race for every user. so i need to view the race of a user in his profile, how do i do that?
i made this code but i am not sure it's correct:
Code: Select all
$query="SELECT * FROM races";
$result=mysql_query($query);
$race=mysql_result($result,"race");
and this code, i am also not sure if it's correct
:
Code: Select all
// user:
$query="SELECT * FROM users = '$_SESSION[username]'";
$result=mysql_query($query);
$id=mysql_result($result,"id");
// race:
$query="SELECT * FROM races";
$resultR=mysql_query($query);
$idr=mysql_resultR($resultR,"id");
if ($id == $idr) {
$race=mysql_result($resultR,"race");
}
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Fri Aug 12, 2005 5:17 am
some form of
JOIN
example:
Code: Select all
SELECT * FROM `users` LEFT JOIN `races` ON (`users`.`id` = `races`.`id`)
The-Master
Forum Commoner
Posts: 45 Joined: Sun Aug 07, 2005 9:51 am
Location: Israel
Post
by The-Master » Fri Aug 12, 2005 11:29 am
i will try it, it looks just like what i am looking for!
The-Master
Forum Commoner
Posts: 45 Joined: Sun Aug 07, 2005 9:51 am
Location: Israel
Post
by The-Master » Wed Aug 17, 2005 9:26 am
i tried, it writes "1"... and doesn't show the race... so i made this code, it shows the race but i gotta write the id of the user so i want it to detect the id in a var "$id" how do i do that?
this is the code i made:
Code: Select all
$id=???
$result=mysql_query("SELECT * FROM races");
$race=mysql_result($result,$id,"race");
echo "<b> '.$_SESSION['username'].' </b> - <br> race: $race ";
if i write
"1" instead of
"$id" is shows the race of the user with the id
"1" , so i need the var
"$id" to detect the user's id...
Last edited by
The-Master on Wed Aug 17, 2005 9:32 am, edited 1 time in total.
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Wed Aug 17, 2005 9:31 am
I have no idea how you are passing in the user's ID, so I'm not sure what I could help with. If you post the table structures and details how what fields map to where in each, I may be able to help more with the join..
The-Master
Forum Commoner
Posts: 45 Joined: Sun Aug 07, 2005 9:51 am
Location: Israel
Post
by The-Master » Wed Aug 17, 2005 9:35 am
the tables in the db are like this:
------------------------------------
table name | field 1 | field 2 |
------------------------------------
users | id | user |
------------------------------------
races | id | race |
------------------------------------
"id" is primary key.
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Wed Aug 17, 2005 9:40 am
...and they map to each other, how?
The-Master
Forum Commoner
Posts: 45 Joined: Sun Aug 07, 2005 9:51 am
Location: Israel
Post
by The-Master » Wed Aug 17, 2005 9:43 am
by the id, i think... cuz i don't realy understand the map thing...
Last edited by
The-Master on Wed Aug 17, 2005 9:50 am, edited 2 times in total.
nielsene
DevNet Resident
Posts: 1834 Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA
Post
by nielsene » Wed Aug 17, 2005 9:49 am
is users.race the foreign key to race.id?
The-Master
Forum Commoner
Posts: 45 Joined: Sun Aug 07, 2005 9:51 am
Location: Israel
Post
by The-Master » Wed Aug 17, 2005 9:52 am
nope
nielsene
DevNet Resident
Posts: 1834 Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA
Post
by nielsene » Wed Aug 17, 2005 10:05 am
Can you show a few sample rows of the race table?
The-Master
Forum Commoner
Posts: 45 Joined: Sun Aug 07, 2005 9:51 am
Location: Israel
Post
by The-Master » Wed Aug 17, 2005 10:46 am
nevermind i did it! it was simple as "$id = $_SESSION['username']"! i don't know it doesn't make any sense but it works! but thank you all anyway!