PHP MySQL Getting and using of value

September 22, 2010 at 14:07:46
Specs: Windows XP
Hello ! I need to do the fallowing thing with PHP MySQL:

The script should use two tables - account and characters

First it should - SELECT `id` FROM `accounts` WHERE `username`='$username';

And then it should use that returned id to get name from characters Database with that id

Please help, cant get this work ;(


See More: PHP MySQL Getting and using of value

Report •


#1
September 22, 2010 at 15:09:56
$sql = mysql_query("select id from tbl_name where username='" . $username . "' limit 1;") or die(mysql_error());

if(mysql_num_rows($sql) == 1) {
   $row = mysql_fetch_row($sql);
   unset($sql);

   $sql = mysql_query("select * from tbl_name where id='" . $row[0] . "';") or die(mysql_error());

   while($row = mysql_fetch_assoc($sql)) {
      foreach($row as $key => $val) {
         echo $key . " = " . $val . "<br />";
      }
   }
}

I don't know if that's the correct syntax or not as I currently can't test it (don't have sql installed), but hopefully, it's close enough to get you going.

-------------------------------------------
I have no aspiration for preaching. ;-)


Report •

#2
September 23, 2010 at 18:21:36
Use a subquery:

SELECT
*
FROM
characters
WHERE
id = (
SELECT
id
FROM
accounts
WHERE
username = '$username'
)


Report •

#3
September 25, 2010 at 10:41:40
Subquery looks nice, bet its not working for some reason :(

$get_chars = "SELECT * FROM characters WHERE account=(SELECT id FROM auth.account WHERE username='$_SESSION[MM_Username]')";

$get_chars2 = mysql_query($get_chars);

while($get_chars3 = mysql_fetch_array($get_chars2))
{
echo "".$get_chars3['name']."";
}


Report •

Related Solutions

#4
September 25, 2010 at 11:12:00
Hmm, it looks ok. Does it give you an error message??

Add this after the mysql_query call:


if ( mysql_error() )
echo mysql_error();


Report •

#5
September 25, 2010 at 12:25:20
Thanks ir helped, and i fixed it, strange, but i had to connect one more time to DB:

include("config/configc.php");
$con = mysql_connect($cHost.":".$cPort, $cUsername, $cPass) or die(mysql_error());
mysql_select_db($cDatabase) or die(mysql_error());


Report •


Ask Question