Page 1 of 1

Displaying cookie info...

Posted: Mon Jan 29, 2007 7:56 pm
by Mightywayne
Edit: Note to self. {$happiness['happy']} Use brackets and stuff.

>>Heyo... So I got around to making the cookie login, and it logs in all fine, and sets the cookie. What I'm having a problem with is, is taking info from the cookie and displaying it correctly. The result turns up blank; won't show up in the table.

Code: Select all

$user = $_COOKIE['cookie'];

$query = mysql_query("SELECT * FROM users WHERE username='$user'");

?>
<!-- 700 x 600 methinx -->
<center>
  <table width="95%" height="100%" valign="top" border="1">
    <tr>
      <td height="75" colspan="2">&nbsp;<?php echo "$query" ?></td>
    </tr>
    <tr>
      <td height="400" width="10%">&nbsp;</td>
      <td height="400" width="90%">&nbsp;</td>
    </tr>
    <tr>
      <td height="25" align="center" colspan="2"><?php include("footer.html"); ?></td>
    </tr>
  </table>
Even if I replace what to echo, with $user, it doesn't display. What's up?

Posted: Mon Jan 29, 2007 8:13 pm
by Kieran Huggins
$query is a mysql resource identifier... you need to iterate over the result set with something like $row=mysql_fetch_assoc($query)

also, is your cookie really called 'cookie'?

Posted: Mon Jan 29, 2007 9:55 pm
by Mightywayne
Oh, right, that was just the test. (cookie thing :P)

Rightyo, thanks for the help.

Posted: Wed Jan 31, 2007 9:52 am
by Mightywayne
Hmm... new problem.

Your assoc_ and array_ worked fine, I think, but now for a result, I'm getting the word "array".

Code: Select all

$user = $_COOKIE['user'];

$query = mysql_query("SELECT * FROM user WHERE username='$user'");
$displayuser = mysql_fetch_array($query, MYSQL_ASSOC);
If I echo $user, it equals the value correctly; that's not a problem. Also, even if I remove ,mysql_assoc after $query, it's the same thing. When I echo it, it just gives me "Array".

Posted: Wed Jan 31, 2007 11:14 am
by feyd
It's an array... var_dump($displayuser)

Posted: Wed Jan 31, 2007 12:26 pm
by Mightywayne
Perfection, thanks.

Posted: Wed Jan 31, 2007 1:55 pm
by RobertGonzalez
Here are some comments to help guide you a bit..

Code: Select all

<?php
// Set the value of $user to the cookie value 'user'
/**
 * SIDE NOTE: What happens if this cookie is not set? 
 * Methinks you might get familiar with the undefined 
 * index notice...
 */
$user = $_COOKIE['user'];

// Retrieve a query result identifier from a SQL command
/**
 * SIDE NOTE: What happens if there is a issue with the SQL? 
 * Methinks looking into mysql_error() might be useful.
 * 
 * SIDE SIDE NOTE: You might want to spend some time on 
 * reserved field names in MySQL and look at the backtick 
 * (`) escape symbol.
 */
$query = mysql_query("SELECT * FROM user WHERE username='$user'");

// Read the result of the previous query into an associative array
/**
 * Since this is an array, you will not be able to echo it out directly. 
 * However, you could loop over it and get information about it 
 * that way.
 * 
 * Methinks that the for(), foreach() and while() constructs might be 
 * useful to you in this situation...
 */
$displayuser = mysql_fetch_array($query, MYSQL_ASSOC);
?>

Posted: Wed Jan 31, 2007 2:02 pm
by Mightywayne
Ah, awesome Everah, thanks a lot. You're always so helpful. I'll definitely look into error by turning it into a custom function, if I can.

BTW the game's coming great. ^_^ I'm going to make the "home" page tommorow.