Cookies or Sessions

Sorry Dave, it might help to add a space before WHERE in the query
string as follows:

// Set the query string to a PHP variable
$Query = “SELECT * FROM " . $TableName . " WHERE username='” .
$username . “'”;

On Dec 21, 2009, at 10:14 AM, Mike B wrote:

Dave,
There are different ways to get information from a database but what
you are trying wouldn’t work because your code would not do
anything :slight_smile: are you not getting any errors from it? anyway…

I think the best thing to do is to get a book like PHP and MySQL Web
Development, ISBN 0-672-32525-X, there are several mistakes you are
making that would say you should read something on php and MySql.

You can’t write query string like you have, the ‘=’ and the ‘,’ are
outside the string for example:

‘firstname’=$firstname, ‘surname’=$surname

nor can you write a string like:

“Welcome back $firstname.”&nbsp".$surname"

you need to use a ‘.’ to join two php strings or you might get away
with “Welcome back $firstname&nbsp$surname” but I personally would
use 'Welcome back ’ . $firstname . ‘&nbsp’ . $surname; or 'Welcome
back ’ . $firstname . ’ ’ . $surname;

If your query string was right then it wouldn’t do anything anyway
as you are not making the query properly, not sure what you where
using as a guide on this but change to something else… the book
above is money well spent.

The following line is passing a query string to a variable
$Query=“SELECT * FROM $TableName WHERE username=‘$username’”;

The following line using the query string in making a query to the
database, so in the this case there is no connection between your
query string and what you are using to make the query:

$Result=mysql_db_query (‘firstname’=$firstname, ‘surname’=$surname);

Try using the following code:

This is by no means complete but it adds a little checking for
failing gracefully, you would need to add some security relative to
how secure you want your database and the pages to be that are being
accessed from giving entry to someone.

if ($TableName && $username) {

   // Make the database connect here
   // NOTE: You need to place the include to your connection code or  
make the connection here

   // Set the query string to a PHP variable
   $Query = "SELECT * FROM " . $TableName . "WHERE username='" .  
$username . "'";
   // Perform query
   $Result = @mysql_query($Query) or die ($Query, __LINE__,  
mysql_error());

   $num_rows = @mysql_num_rows($Result);

   if ($num_rows==1) { // Are there any matches

       $row = mysql_fetch_array($the_rslt);

       $surname		   = stripslashes($row['surname']);
       $firstname		   = stripslashes($row['firstname']);

       print("Welcome back " . $firstname . "&nbsp" . $surname);
   } else {
       print("No user listed");
   }

  // Close the connection: mysql_close(Could have the conection  
here, depends on how you make the connection).
   mysql_close();
} else {
   print("No TableName or username");
}

HTH

On Dec 21, 2009, at 12:43 AM, DeltaDave wrote:

OK I am getting there but what I am trying to do is output the
$username that is currently being displayed as the full name of the
user which is stored in the table as firstname and surname

What I currently have is

{
print("Welcome back $username");
}

Instead I think I want something like

{
$Query="SELECT * FROM $TableName WHERE username='$username'";
$Result=mysql_db_query ('firstname'=$firstname, 'surname'=$surname);
print("Welcome back $firstname."&nbsp".$surname");
}

But of course it doesn’t work.

Please point me in the right direction - I know this is pretty
basic but I have brain freeze to go with our wintry weather.

David


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

In your SQL query, you are comparing strings of text, but you haven’t
quoted the comparison, so you’ll get an error. Depending on how your
server is set up, you may not see that error, but it’s why the thing
is failing.

SELECT * FROM foo WHERE bar = baz;

is not the same thing as

SELECT * FROM foo WHERE bar = "baz";

In the first query, the database will try to find a column in your
data called baz with the same value as the value of bar, rather than
finding a row in the data where bar equals the string value “baz”, as
in the second query.

Mike’s right – a book will help a lot.

Walter

On Dec 20, 2009, at 6:43 PM, DeltaDave wrote:

OK I am getting there but what I am trying to do is output the
$username that is currently being displayed as the full name of the
user which is stored in the table as firstname and surname

What I currently have is

{
print("Welcome back $username");
}

Instead I think I want something like

{
$Query="SELECT * FROM $TableName WHERE username='$username'";
$Result=mysql_db_query ('firstname'=$firstname, 'surname'=$surname);
print("Welcome back $firstname."&nbsp".$surname");
}

But of course it doesn’t work.

Please point me in the right direction - I know this is pretty basic
but I have brain freeze to go with our wintry weather.

David


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

I know RTFM - I have several books including

PHP5 and MySQL Bible by Tim Converse etc.

MySQL/PHP Database Applications by Greenspan & Bulger

and The PHP Anthology by Davey Shafik et al

I must admit I was trying to reuse a bit of code from elsewhere in this site but obviously I wasn’t doing it right.

Thanks for your tips and I will work on what you have given me.

David


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

OK I cheated a bit as I am running out of time to get this site live and I couldn’t get your code to work for me Mike.

Because there are only 3 users that I wanted to do this for I added the following

if (($username=="user_a"))
{
print("Welcome back user_a's fullname it is $date");
}
else
if (($username=="user_b"))
{
print("Welcome back user_b's fullname it is $date");
}
else
if (($username=="user_c"))
{
print("Welcome back user_c's fullname it is $date");
}
else.......do something else

And it seems to do the trick for just now until I have more time to get my head round this stuff.

Thanks for your guidance.

David


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Dave,
I can only take it you are having problems with the database query to
return the usernames stored there!, if you can tell us what ‘if any’
errors are you are getting then maybe we can help you sort out the
actual problem you have.

BTW, you could cut your code down a bit to:

if ($username=="user_a" || $username=="user_b" || $username=="user_c") {
	print("Welcome back " . $username . "'s full name it is " . $date);
} else {
	//.......do something else
}

HTH

On Dec 22, 2009, at 12:58 AM, DeltaDave wrote:

OK I cheated a bit as I am running out of time to get this site live
and I couldn’t get your code to work for me Mike.

Because there are only 3 users that I wanted to do this for I added
the following

if (($username=="user_a"))
{
print("Welcome back user_a's fullname it is $date");
}
else
if (($username=="user_b"))
{
print("Welcome back user_b's fullname it is $date");
}
else
if (($username=="user_c"))
{
print("Welcome back user_c's fullname it is $date");
}
else.......do something else

And it seems to do the trick for just now until I have more time to
get my head round this stuff.

Thanks for your guidance.

David


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Thanks Mike but when running it locally on MAMP I am not getting errors - just blank pages.

As to cutting down the code I have created I probably didn’t explain that properly. What I actually have is more like

if (($username=="smith_j"))
{
print("Welcome back Jimmy Smith it is $date");
}
else
if (($username=="jones_m"))
{
print("Welcome back Mike Jones it is $date");
}
else
if (($username=="blair_t"))
{
print("Welcome back Tony Blair it is $date");
}

I do appreciate your help with this but I know that I have homework to do so that I can get a greater understanding of how it all works.

I have had some success with reusing code snippets and that has got me by so far but it will be quite some time (if at all) before I can do this without a fair bit of hand holding.

Your time is valuable and I am conscious that there are many demands on it far more deserving than mine. I will sit down with the book(s) but when you are faced with a 1000 page Tome it is pretty daunting.

David


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Just an update to this - I got it working as I wanted - thanks to you guys. This is how I did it.

{
$query = "SELECT * FROM $TableName where username = '$username'";

$result = mysql_query($query);
     $row = mysql_fetch_array($result);

$firstname=($row['firstname']);
$surname=($row['surname']);
print("Welcome back $firstname $surname it is $date");
}
else.....

Just a quick thought should these two lines

$firstname=($row['firstname']);
$surname=($row['surname']);

Actually be

$firstname=stripslashes($row['firstname']);
$surname=stripslashes($row['surname']);

It is working OK though.

David


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

There are other ways of doing this relative to how your server is set
up ‘magic_quotes’ but I don’t want to complicate things too much so
using stripslashes will do for your needs.

Basically what it will do is remove any slash that should have been
placed before an apostrophie when a database entry was made, so if you
had say O’Reilly as a surname then it will probably have been entered
into the databse as O’Reilly so using stripslashes will clean ‘or
bring’ the name back to O’Reilly

$firstname=stripslashes($row['firstname']);
$surname=stripslashes($row['surname']);

It might be good to check if $firstname and $surname have a value
before printing the welcome message:

$firstname=stripslashes($row['firstname']);
$surname=stripslashes($row['surname']);

if ($firstname && $surname) {
     print("Welcome back $firstname $surname it is $date");
} else.....

Glad you got it working :slight_smile:

On Dec 24, 2009, at 12:23 AM, DeltaDave wrote:

Just an update to this - I got it working as I wanted - thanks to
you guys. This is how I did it.

{
$query = "SELECT * FROM $TableName where username = '$username'";

$result = mysql_query($query);
    $row = mysql_fetch_array($result);

$firstname=($row['firstname']);
$surname=($row['surname']);
print("Welcome back $firstname $surname it is $date");
}
else.....

Just a quick thought should these two lines

$firstname=($row['firstname']);
$surname=($row['surname']);

Actually be

$firstname=stripslashes($row['firstname']);
$surname=stripslashes($row['surname']);

It is working OK though.

David


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Thanks Mike

D


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options