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

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
The-Master
Forum Commoner
Posts: 45
Joined: Sun Aug 07, 2005 9:51 am
Location: Israel

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

Post 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]
Last edited by The-Master on Wed Aug 10, 2005 6:22 pm, edited 1 time in total.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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']
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 »

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 »

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");
}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

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 »

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 »

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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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..
The-Master
Forum Commoner
Posts: 45
Joined: Sun Aug 07, 2005 9:51 am
Location: Israel

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

...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 »

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.
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

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 »

nope
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

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 »

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!
Post Reply