URGENT HELP!!

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
susi
Forum Newbie
Posts: 4
Joined: Fri Mar 21, 2003 6:56 am

URGENT HELP!!

Post by susi »

im newbie a this so bear with me..


im creating a football site which when a person registers.. they pick user/pass and get 2 pick 11 players.. this goes to my database.

when i log in using user/pass it goes to the members area.

i want it to display the 11 players on that page for that member!
im using sessions

i have the following query in my members page

$qstr = "SELECT player1 from members2 where username = [$SESSION_UNAME]";
$result = mysql_query($qstr);

is that right??

but then how do i call it??

get the name of the player1 to appear on the members area etc..

Thanking you in advance :(
matthiasone
Forum Contributor
Posts: 117
Joined: Mon Jul 22, 2002 12:14 pm
Location: Texas, USA
Contact:

Post by matthiasone »

the first thing I would do is create a function for querying the database

should look something like this:

Code: Select all

function query_db($query) 
{ 
    $database = "db_name";
    $dbusername = "username"; 
    $dbpassword = "password";
    $dbhost = "db_host";
    $err = ""; 
   $connection = mysql_connect($dbhost,$dbusername,$dbpassword) or $err .= mysql_error(); 
   $database = mysql_select_db($database, $connection) or $err .= mysql_error(); 
   $result = mysql_query($query) or $err .= mysql_error(); 
   echo $err; 
   return $result; 
   mysql_close($connection); 
}
then I would query for the players depending on how your tables are set up. Something like:

Code: Select all

$query = "SELELCT player1, player2, player3,...player11 FROM members2 WHERE username ='".$_SESSIONї'uname']."'";
$result = query_db($query);
$player = mysql_fetch_assoc($result);
echo $playerї'player1'];
.
.
.
etc
User avatar
lazy_yogi
Forum Contributor
Posts: 243
Joined: Fri Jan 24, 2003 3:27 am

Post by lazy_yogi »

I don't quite agree with you there matthiasone

your function query_db seems redundant. You are connecting over and over and over each time you execute a query. (also, your "mysql_close($connection);" statement is put after the return statement and so it is never executed at all)

I'd just put this at the top of each script that uses the database :

Code: Select all

/*** Connect to DataBase  ***/
      mysql_connect( "localhost" , "user" , "pass" ) 
            OR die( "Could not connect : ".mysql_error());
      mysql_select_db( 'dbname' )
            OR die( "Error : ".mysql_errno()." : ".mysql_error()."<br>\n");
and then to execute a query I'd use this which is the same amount of code as you use for a query but minus the use of the function:

Code: Select all

$query = "Select * FROM blah";
      $result = mysql_query($query)
            OR exit_script("Error:".mysql_errno()." : ".mysql_error()."<br>\n");
      while ($row = mysql_fetch_array($result))
             // do stuff
__________________________________________________________

And susi, the answer to your question :

Code: Select all

$query = "SELECT player1 from members2 where username = [$SESSION_UNAME]";
      $result = mysql_query($query)  // THIS EXECUTES IT
            OR exit_script("Error:".mysql_errno()." : ".mysql_error()."<br>\n");
      while ($row = mysql_fetch_array($result)) { // THIS GETS EACH ROW AT A TIME FROM THE RESULT
             // do stuff with each row eg
             print $row['player1']."<BR>";
      }
Oh and one more thing ... try and spend a couple of seconds thinking of a more suitable title for your message. Something that explains the question briefly. Instead of "URGENT HELP!!" you could use something like "How to get the results of a query". Some people may have the same problem and if they saw a title that explained their problem they are more likely to see your response thant post another redundant message.

Cheers
susi
Forum Newbie
Posts: 4
Joined: Fri Mar 21, 2003 6:56 am

Post by susi »

OK...
thanks a million!
its working now!

:)
Post Reply