PHP/MySQL problem

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
emilia
Forum Newbie
Posts: 6
Joined: Tue Nov 11, 2003 12:45 pm

PHP/MySQL problem

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


?>
User avatar
Johnm
Forum Contributor
Posts: 344
Joined: Mon May 13, 2002 12:05 pm
Location: Michigan, USA
Contact:

Post 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
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post 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..
emilia
Forum Newbie
Posts: 6
Joined: Tue Nov 11, 2003 12:45 pm

Post 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
User avatar
Jean-Yves
Forum Contributor
Posts: 148
Joined: Wed Jul 02, 2003 2:13 pm
Location: West Country, UK

Post 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.
emilia
Forum Newbie
Posts: 6
Joined: Tue Nov 11, 2003 12:45 pm

Post 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!
User avatar
Johnm
Forum Contributor
Posts: 344
Joined: Mon May 13, 2002 12:05 pm
Location: Michigan, USA
Contact:

Post 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
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Is username coming from a form? If so, read this:
viewtopic.php?t=511

Mac
Post Reply