Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/171156-solved-mysql-array-help/
Share on other sites

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>';

<?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 :)

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">';

 

}

 

?>

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.

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

 

--

 

:-[

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.