PHP/MySQL problem
Moderator: General Moderators
PHP/MySQL problem
Hey I am havin trouble with my website using PHP/MySQL. When the user logs in I want them to be taking to a page that displays their name and the level of exam that they are currently at and when they last logged in!
This is some of the code I have tried
echo"Welcome ";
echo $username;
//connect to database
$query = (" SELECT Levelreached FROM Users WHERE username='username'");
$result= mysql_query($query) or die ("couldn't execute query.");
echo "You are currently on level ";
echo $result;
?>
This is some of the code I have tried
echo"Welcome ";
echo $username;
//connect to database
$query = (" SELECT Levelreached FROM Users WHERE username='username'");
$result= mysql_query($query) or die ("couldn't execute query.");
echo "You are currently on level ";
echo $result;
?>
Code: Select all
$query = (" SELECT Levelreached FROM Users WHERE username='username'");
$result= mysql_query($query) or die ("couldn't execute query.");Code: Select all
<?php
$query = "select levelreached from users where username='".$username."'';
$result = mysql_query($query) or die (MySQL_Error());
$numrows = mysql_num_rows($result);
if ($numrows == 0)
{
echo 'no user found!';
}
// code here for posting...
?>also, like JohnM said, post a little more code if the above revision doesn't solve the problem..
Hey thanks for your replys and suggestions. Sorry john if I don't appear to have manners It is my first time ever using a forum and I don't know the etiquette of using one.
Back to my problem, I am using headers to carry the users name through to the next page. That is working fine. What I want to do is access the users tutorial level from the database and tell the user that they are on that level. the error I am getting is "You are on level resource id #3" instead of "you are on level 1". Does anyone know where level resource id #3 is coming from? I only started learning PHP/MySQL last week so I don't really know what I am at!
//connect to the database
$db = mysql_connect("$dbHost", "$dbUser", "$dbPass") or die ("Error connecting to database.");
mysql_select_db("$dbDatabase", $db) or die ("Couldn't select the database.");
$query = (" SELECT Levelreached FROM Users WHERE username='username'");
$result= mysql_query($query) or die ("couldn't execute query.");
echo "You are currently on level ";
echo $result;
Do I need to do something with $result before I print it out?
Any help at all would be great
Thanks
Back to my problem, I am using headers to carry the users name through to the next page. That is working fine. What I want to do is access the users tutorial level from the database and tell the user that they are on that level. the error I am getting is "You are on level resource id #3" instead of "you are on level 1". Does anyone know where level resource id #3 is coming from? I only started learning PHP/MySQL last week so I don't really know what I am at!
//connect to the database
$db = mysql_connect("$dbHost", "$dbUser", "$dbPass") or die ("Error connecting to database.");
mysql_select_db("$dbDatabase", $db) or die ("Couldn't select the database.");
$query = (" SELECT Levelreached FROM Users WHERE username='username'");
$result= mysql_query($query) or die ("couldn't execute query.");
echo "You are currently on level ";
echo $result;
Do I need to do something with $result before I print it out?
Any help at all would be great
Thanks
Hi emilia,
You need to extract the fields from the $result variable. You can use several methods, I like to use the [php_man]mysql_fetch_array()[/php_man] function:
By the way, you don't need brackets around the SQL statement.
HTH.
You need to extract the fields from the $result variable. You can use several methods, I like to use the [php_man]mysql_fetch_array()[/php_man] function:
Code: Select all
$dbRow = mysql_fetch_array($result);
echo $dbRow["LevelReached"];HTH.
- Johnm
- Forum Contributor
- Posts: 344
- Joined: Mon May 13, 2002 12:05 pm
- Location: Michigan, USA
- Contact:
Emilia,
A good place to start is to run the querry nativly in the database. I am not familiar with MySql so I can not tell you exactly how to do that but to get the exact querry being submitted use one of these:
This alone may show you where the problem is (i.e. $user may not be set as you think it is). From here you can copy and paste just the query into the db to see what results it returns.
Hope that helps,
John M
A good place to start is to run the querry nativly in the database. I am not familiar with MySql so I can not tell you exactly how to do that but to get the exact querry being submitted use one of these:
Code: Select all
<?php
die($query);
echo $query;
?>Hope that helps,
John M
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK