Hooo Posted August 20, 2009 Share Posted August 20, 2009 Ok, problem is simply, I can't echo a field from a mysql table I have. Code is: <?php include 'config.php'; include 'opendb.php'; session_start(); $data = mysql_query("SELECT * FROM Users"); $info = mysql_fetch_array($data) or die(mysql_error()); if(isset($_SESSION['usname'])) { ?> <html> <body> Login Successful<br /><br /> Your age is:<br /><br /> <a href='logout.php'>Logout</a> </body> </html> <?php } else { echo '<meta http-equiv="refresh" content="2;url=index.php">'; } ?> The table as you can see is called Users, and what I would like to echo is say the Age, field name is also age. I have tried as you can see with the $data and $info variables, however i'm having no luck myself. Also, is it possible to display for instance the variable made within the HTML text? So after where it says "Your age is:" put the age? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/171156-solved-mysql-array-help/ Share on other sites More sharing options...
Bricktop Posted August 20, 2009 Share Posted August 20, 2009 Hi Hooo, Using your example you would need to output data as $data[age]. For example: echo ' '.$data[age].' '; Also, don't use raw HTML code in the middle of a PHP statement, you need to echo it. Like: echo '<body>'; echo 'Login Successful<br /><br />'; echo 'Your age is:<br /><br />'; echo '<a href='logout.php'>Logout</a>'; echo '</body>'; echo '</html>'; Quote Link to comment https://forums.phpfreaks.com/topic/171156-solved-mysql-array-help/#findComment-902576 Share on other sites More sharing options...
Hooo Posted August 20, 2009 Author Share Posted August 20, 2009 <?php include 'config.php'; include 'opendb.php'; session_start(); $data = mysql_query("SELECT * FROM Users"); $info = mysql_fetch_array($data) or die(mysql_error()); if(isset($_SESSION['usname'])) { echo '<html>'; echo '<body>'; echo 'Login Successful<br /><br />'; echo 'Your age is: $info['age']<br /><br />'; echo '<a href='logout.php'>Logout</a>'; echo '</body>'; echo '</html>'; } else { echo '<meta http-equiv="refresh" content="2;url=index.php">'; } ?> The page then doesn't display at all. Sorry for being an idiot at this Quote Link to comment https://forums.phpfreaks.com/topic/171156-solved-mysql-array-help/#findComment-902627 Share on other sites More sharing options...
wildteen88 Posted August 20, 2009 Share Posted August 20, 2009 This echo 'Your age is: $info['age']<br /><br />'; Needs to be echo 'Your age is: ' . $info['age'] . '<br /><br />'; Quote Link to comment https://forums.phpfreaks.com/topic/171156-solved-mysql-array-help/#findComment-902642 Share on other sites More sharing options...
Hooo Posted August 20, 2009 Author Share Posted August 20, 2009 Fixed that, however there is still something wrong: The website cannot display the page HTTP 500 Most likely causes: The website is under maintenance. The website has a programming error. Normally it's something stupid I miss/leave out.. Sorry again x Quote Link to comment https://forums.phpfreaks.com/topic/171156-solved-mysql-array-help/#findComment-902658 Share on other sites More sharing options...
seopaul Posted August 20, 2009 Share Posted August 20, 2009 looking at your logic there, i can see 2 issues, first the sql query selects * from users so your getting every user, so you need to add a where clause, and the isset() check could do with being higher up... below is a edited copy of what you posted... <?php include 'config.php'; include 'opendb.php'; session_start(); if(isset($_SESSION['usname'])) { $data = mysql_query("SELECT * FROM Users WHERE usname = '{$_SESSION['usname']}'"); $info = mysql_fetch_array($data) or die(mysql_error()); echo '<html>'; echo '<body>'; echo 'Login Successful<br /><br />'; echo "Your age is: {$info['age']}<br /><br />"; echo '<a href='logout.php'>Logout</a>'; echo '</body>'; echo '</html>'; } else { echo '<meta http-equiv="refresh" content="2;url=index.php">'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/171156-solved-mysql-array-help/#findComment-902665 Share on other sites More sharing options...
Hooo Posted August 20, 2009 Author Share Posted August 20, 2009 Just pretty much copy/pasted what you did above this post, and still the page is not loading at all. Quote Link to comment https://forums.phpfreaks.com/topic/171156-solved-mysql-array-help/#findComment-902689 Share on other sites More sharing options...
mikesta707 Posted August 20, 2009 Share Posted August 20, 2009 there is no need to echo HTML, you can just put raw HTML between your php code blocks, and everything will stay the same. In fact, I would suggest instead of echoing the HTML and BODY tag, you put them up top. What I suspect is happening is that your if statement is running false, and the HTML and body tag are not being output, so the page is not valid HTML. try <html> <body> <?php if(isset($_SESSION['usname'])) { $data = mysql_query("SELECT * FROM Users WHERE username = '" .$_SESSION['usname'] . "'"); $info = mysql_fetch_assoc($data) or die(mysql_error()); echo "Login Successful<br /><br />"; echo "Your age is: " . $info['age'] . "<br /><br />"; echo "<a href='logout.php'>Logout</a>"; } ?> </body> </html> That should fix your HTML 500 error. By the way this script assumes that the your table has a username field that stores the usernames and an age field that stores the age. Quote Link to comment https://forums.phpfreaks.com/topic/171156-solved-mysql-array-help/#findComment-902709 Share on other sites More sharing options...
Hooo Posted August 20, 2009 Author Share Posted August 20, 2009 The table does indeed have a field for ID/Name/Age/Email. Will try the above. -- <html> <body> <?php include 'config.php'; include 'opendb.php'; session_start(); if(isset($_SESSION['usname'])) { $data = mysql_query("SELECT * FROM Users WHERE usname = '" .$_SESSION['usname'] . "'"); $info = mysql_fetch_assoc($data) or die(mysql_error()); echo "Login Successful<br /><br />"; echo "Your age is: " . $info['age'] . "<br /><br />"; echo "<a href='logout.php'>Logout</a>"; } else { echo '<meta http-equiv="refresh" content="2;url=index.php">'; } ?> </body> </html> The page now appears (woop) but, this is what I see: -- Login Successful Your age is: Logout -- Quote Link to comment https://forums.phpfreaks.com/topic/171156-solved-mysql-array-help/#findComment-902719 Share on other sites More sharing options...
mikesta707 Posted August 20, 2009 Share Posted August 20, 2009 are you absolutely sure that the age columns is named 'age'? could it be Age? AGE? AGe? AgE? aGe? etc. Quote Link to comment https://forums.phpfreaks.com/topic/171156-solved-mysql-array-help/#findComment-902733 Share on other sites More sharing options...
Hooo Posted August 20, 2009 Author Share Posted August 20, 2009 ... Thanks userage... Sorry to waste your time. Quote Link to comment https://forums.phpfreaks.com/topic/171156-solved-mysql-array-help/#findComment-902758 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.