Page 1 of 1

PHP/MySQL problem

Posted: Tue Nov 11, 2003 12:45 pm
by emilia
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;


?>

Posted: Tue Nov 11, 2003 1:24 pm
by Johnm
So, first of all, post more code... that won't work as it is. Secondly, what is or is it not doing? Errors??? Blank Page????? Please be a little more descriptive and I am sure that we can help you through it.

Also, as a side note, manners are a good way to encourage people to help you.

John M

Posted: Tue Nov 11, 2003 2:32 pm
by infolock

Code: Select all

$query = (" SELECT Levelreached FROM Users WHERE username='username'"); 
$result= mysql_query($query) or die ("couldn't execute query.");
try

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...

?>
this will give us a more exact error message and help us determine the overall problem.

also, like JohnM said, post a little more code if the above revision doesn't solve the problem..

Posted: Wed Nov 12, 2003 5:38 am
by emilia
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

Posted: Wed Nov 12, 2003 6:00 am
by Jean-Yves
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:

Code: Select all

$dbRow = mysql_fetch_array($result);
  echo $dbRow["LevelReached"];
By the way, you don't need brackets around the SQL statement.

HTH.

Posted: Wed Nov 12, 2003 7:08 am
by emilia
Hey thanks I tried that and it got rid of the level resource id #3 but it still won't print out the level. I have checked the database and there is information in it about the level! I don't know what to do!

Posted: Wed Nov 12, 2003 7:18 am
by Johnm
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:

Code: Select all

<?php
die($query);
echo $query;

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

Posted: Wed Nov 12, 2003 7:22 am
by twigletmac
Is username coming from a form? If so, read this:
viewtopic.php?t=511

Mac