Page 1 of 1

Showing information on a PHP page from a DB...

Posted: Wed Aug 10, 2005 6:14 pm
by The-Master
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 :cry: ... 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>
&#1490;&#1494;&#1506;: $race<br>
&#1512;&#1502;&#1492;: $level<br>
&#1504;&#1497;&#1505;&#1497;&#1493;&#1503;: $cXP /<b> $XP </b><br>
&#1495;&#1497;&#1497;&#1501;: $cHP /<b> $HP </b><br>
--------------<br>
&#1506;&#1493;&#1510;&#1502;&#1492;: $cMP /<b> $MP</b><br>
&#1492;&#1514;&#1511;&#1508;&#1492;: $offence <br>
&#1492;&#1490;&#1504;&#1492;: $defence <br>
&#1502;&#1492;&#1497;&#1512;&#1493;&#1514;: $speed <br>
&#1495;&#1493;&#1499;&#1502;&#1492;: $wisdom <br>
--------------<br>
&#1504;&#1513;&#1511;: $weapon <br>
&#1504;&#1513;&#1511; &#1513;&#1504;&#1497;: $weapon2 <br>
&#1513;&#1497;&#1512;&#1497;&#1493;&#1503;: $armor <br>
&#1508;&#1512;&#1497;&#1496; &#1502;&#1497;&#1493;&#1495;&#1491;: $item <br>
--------------<br>
&#1499;&#1505;&#1507;: $money
&#1502;&#1513;&#1488;&#1489;&#1497;&#1501;: $resource
--------------<br>
&#1513;&#1489;&#1496;: $clan
&#1514;&#1508;&#1511;&#1497;&#1491; &#1489;&#1513;&#1489;&#1496;: $clan_rank ";
}
what's the bug here :cry: ?

P.S: don't mind the "unknown" chars they are in hebrew...


feyd | you can use the

Code: Select all

tags anytime now.. [/color]

Posted: Wed Aug 10, 2005 6:21 pm
by s.dot

Code: Select all

$sql1 = "SELECT * FROM users WHERE user = '".$_SESSION['username']."'";
Notice I broke the php string and concatenated your $_SESSION['username']

Posted: Wed Aug 10, 2005 6:26 pm
by The-Master
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...

Posted: Thu Aug 11, 2005 4:21 pm
by The-Master
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? :oops:
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 :cry: :

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");
}

Posted: Fri Aug 12, 2005 5:17 am
by feyd
some form of JOIN

example:

Code: Select all

SELECT * FROM `users` LEFT JOIN `races` ON (`users`.`id` = `races`.`id`)

Posted: Fri Aug 12, 2005 11:29 am
by The-Master
i will try it, it looks just like what i am looking for!

Posted: Wed Aug 17, 2005 9:26 am
by The-Master
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...

Posted: Wed Aug 17, 2005 9:31 am
by feyd
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..

Posted: Wed Aug 17, 2005 9:35 am
by The-Master
the tables in the db are like this:

------------------------------------
table name | field 1 | field 2 |
------------------------------------
users | id | user |
------------------------------------
races | id | race |
------------------------------------

"id" is primary key.

Posted: Wed Aug 17, 2005 9:40 am
by feyd
...and they map to each other, how?

Posted: Wed Aug 17, 2005 9:43 am
by The-Master
by the id, i think... cuz i don't realy understand the map thing...

Posted: Wed Aug 17, 2005 9:49 am
by nielsene
is users.race the foreign key to race.id?

Posted: Wed Aug 17, 2005 9:52 am
by The-Master
nope

Posted: Wed Aug 17, 2005 10:05 am
by nielsene
Can you show a few sample rows of the race table?

Posted: Wed Aug 17, 2005 10:46 am
by The-Master
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!