473,654 Members | 3,107 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

loops, php and mysql data

Greetings all. I am writing a profile creator script where a user gets
a URL invite in their mail in the form of;

http://domain.com/profile-create.php...d98jadf098asdf

Things are working well except for a small annoyance in which someone
might have a solution to.

In the event that someone accesses profile-create.php without an
access_code the script generates a warning. If there is an access_code
that matches the access code entered in the MySQL database then they
are directed to the profile creation page. My problem is;

If someone accesses profile-create.php with an access_code but it
doesn't match any entries in the database I would like to generate a
warning.

When I try to do this with an else statement it produces an error for
every access code listed in the database which isn't the correct one,
so I could end up with a successful profile creation page with a bunch
of errors.

I've played with break and pattern matching but no results. Here is
the script below.
---------------------------------------------------------------<
<?php

$access_code = $_GET['access_code'];

if ($access_code) {

echo db_connect();

$result = mysql_query('SE LECT random_link FROM invites');

while ($row = mysql_fetch_arr ay($result, MYSQL_NUM)) {

$random_link_db = $row[0];

if ($random_link_d b == $access_code) {
echo profile_creator _page();
}

// Would like to insert an error warning here

}
}

else {
echo "Error!"
}
function profile_creator _page() {
echo "All's well!";
}

?>
---------------------------------------------------------------<


Any ideas?

Regards,
Luc
Jul 17 '05 #1
15 1815
On 5 Mar 2005 15:10:38 -0800, st**********@ho tmail.com (Stat) wrote:
Greetings all. I am writing a profile creator script where a user gets
a URL invite in their mail in the form of;

http://domain.com/profile-create.php...d98jadf098asdf

Things are working well except for a small annoyance in which someone
might have a solution to.

In the event that someone accesses profile-create.php without an
access_code the script generates a warning. If there is an access_code
that matches the access code entered in the MySQL database then they
are directed to the profile creation page.
OK, so the access_code was generated when the email was sent and stored in the
MySQL database?
My problem is;

If someone accesses profile-create.php with an access_code but it
doesn't match any entries in the database I would like to generate a
warning.

When I try to do this with an else statement it produces an error for
every access code listed in the database which isn't the correct one,
so I could end up with a successful profile creation page with a bunch
of errors.

I've played with break and pattern matching but no results. Here is
the script below.
---------------------------------------------------------------<
<?php

$access_code = $_GET['access_code'];


That'll raise a warning when access_code isn't passed at all; consider either:

$access_code = isset($_GET['access_code']) ? $_GET['access_code'] : '';

or

$access_code = @$_GET['access_code'];
if ($access_code) {

echo db_connect();

$result = mysql_query('SE LECT random_link FROM invites');
I thought you were looking for a specific access_code? It's somewhat defeating
the point of using a database if you fetch the entire table and match in PHP -
use a WHERE clause in the SQL.
while ($row = mysql_fetch_arr ay($result, MYSQL_NUM)) {
Also, if the invites table is empty, you'll never even get here.
$random_link_db = $row[0];

if ($random_link_d b == $access_code) {
echo profile_creator _page();
}

// Would like to insert an error warning here

}
}


You could replace it with something like:

$result = mysql_query(spr intf(
"SELECT count(*) from FROM invites WHERE random_link = '%s'",
mysql_escape_st ring($access_co de)
) or die(mysql_error ());

// This is a single group aggregate query so you're guaranteed
// one row unless the database is broken.
$row = mysql_fetch_arr ay($result, MYSQL_NUM);

if ($row[0] == 1)
{
echo profile_creator _page();
}
else
{
// Warning - got an access code, but it's not in the DB
}

--
Andy Hassall / <an**@andyh.co. uk> / <http://www.andyh.co.uk >
<http://www.andyhsoftwa re.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #2


Andy Hassall wrote:

You could replace it with something like:

$result = mysql_query(spr intf(
"SELECT count(*) from FROM invites WHERE random_link = '%s'",
mysql_escape_st ring($access_co de)
) or die(mysql_error ());


Why go to all the overhead of sprintf just to concatenate a string?
This has much less overhead:

$result = mysql_query(
'SELECT count(*) from FROM invites WHERE random_link = \'' .
mysql_escape_st ring($access_co de) . '/'')
or die(mysql_error ());
Jul 17 '05 #3
Slick! I'd never looked at it that way before. This opens up a whole
new can of proverbial worms. Thank you gentlemen.

NOTE: for those who might have a similar problem in the future, there's
an extraneous 'from' in;

'SELECT count(*) from FROM invites WHERE random_link = ...

it should be

'SELECT count(*) FROM invites WHERE random_link = ...

Jul 17 '05 #4
st**********@ho tmail.com wrote:
Slick! I'd never looked at it that way before. This opens up a whole
new can of proverbial worms. Thank you gentlemen.

NOTE: for those who might have a similar problem in the future, there's
an extraneous 'from' in;

'SELECT count(*) from FROM invites WHERE random_link = ...

it should be

'SELECT count(*) FROM invites WHERE random_link = ...

bear in mind that one is operating in a stateless asynychronous
environment. Don't make any assumptions as to the content of any
variable passed to the script
Jul 17 '05 #5
On Sat, 05 Mar 2005 20:10:09 -0500, Jerry Stuckle <js*******@attg lobal.net>
wrote:
Andy Hassall wrote:

You could replace it with something like:

$result = mysql_query(spr intf(
"SELECT count(*) from FROM invites WHERE random_link = '%s'",
mysql_escape_st ring($access_co de)
) or die(mysql_error ());


Why go to all the overhead of sprintf just to concatenate a string?
This has much less overhead:

$result = mysql_query(
'SELECT count(*) from FROM invites WHERE random_link = \'' .
mysql_escape_st ring($access_co de) . '/'')
or die(mysql_error ());


Readability mostly, particularly when you end up with more than one variable.
It's sort of a poor-man's placeholder system.

You've also demonstrated another reason in your response; you've got your
quoting wrong.

The overhead of a call to sprintf is negligable particularly compared with
external calls to a database.

--
Andy Hassall / <an**@andyh.co. uk> / <http://www.andyh.co.uk >
<http://www.andyhsoftwa re.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #6
On 5 Mar 2005 19:28:39 -0800, st**********@ho tmail.com wrote:
Slick! I'd never looked at it that way before. This opens up a whole
new can of proverbial worms. Thank you gentlemen.

NOTE: for those who might have a similar problem in the future, there's
an extraneous 'from' in;

'SELECT count(*) from FROM invites WHERE random_link = ...


Whoops :-)

--
Andy Hassall / <an**@andyh.co. uk> / <http://www.andyh.co.uk >
<http://www.andyhsoftwa re.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #7
.oO(Jerry Stuckle)
Why go to all the overhead of sprintf just to concatenate a string?
The code looks better and is easier to maintain, especially if you have
to embed multiple values into the string.
This has much less overhead:


But is ugly and buggy. And BTW: Who cares about the overhead when
creating a DB query string? That's peanuts.

Micha
Jul 17 '05 #8


Michael Fesser wrote:
.oO(Jerry Stuckle)

Why go to all the overhead of sprintf just to concatenate a string?

The code looks better and is easier to maintain, especially if you have
to embed multiple values into the string.


In your opinion. I find just the opposite.
This has much less overhead:

But is ugly and buggy. And BTW: Who cares about the overhead when
creating a DB query string? That's peanuts.


Depends on the query and the database. Some database calls can be very
quick, for instance, if handled from the cache, and may have very little
overhead. But sprintf has significantly more overhead than simple
concatenation.

Micha

Jul 17 '05 #9


Andy Hassall wrote:
On Sat, 05 Mar 2005 20:10:09 -0500, Jerry Stuckle <js*******@attg lobal.net>
wrote:

Andy Hassall wrote:
You could replace it with something like:

$result = mysql_query(spr intf(
"SELECT count(*) from FROM invites WHERE random_link = '%s'",
mysql_escape_st ring($access_co de)
) or die(mysql_error ());


Why go to all the overhead of sprintf just to concatenate a string?
This has much less overhead:

$result = mysql_query(
'SELECT count(*) from FROM invites WHERE random_link = \'' .
mysql_escape_st ring($access_co de) . '/'')
or die(mysql_error ());

Readability mostly, particularly when you end up with more than one variable.
It's sort of a poor-man's placeholder system.

You've also demonstrated another reason in your response; you've got your
quoting wrong.

The overhead of a call to sprintf is negligable particularly compared with
external calls to a database.


Depends on the actual query to the database. And sprintf has much more
overhead than simple concatenation.

Yes, there's a minor bug - I used a forward slash where I should have
used a backslash. The parser would catch that bug. A similar bug can
happen in sprintf - but the parser might not catch it. And, BTW, you
had an extra "from" in your statement (which I didn't catch, either).

As for readability - to each his own. I find simple concatenation to be
much easier to read than sprintf. Others may find otherwise.

Jerry
Jul 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1693
by: lawrence | last post by:
Am I right to say mysql_data_seek allows me to walk through a database return as I would an array? for ($1=0; $i < $num; $i++) { mysql_data_see($result, $i); $row = mysql_fetch_array($results); } I still haven't solved the problem I mentioned in another post (I can
0
6678
by: Donald Tyler | last post by:
Then the only way you can do it that I can think of is to write a PHP script to do basically what PHPMyAdmin is trying to do but without the LOCAL in there. However to do that you would need to be able to place the PHP file on the server, and I guess you probably can't do that either. Talk about catch 22... The only other way I can think of is to install MySQL on a machine you control, then import the data there using the method I...
0
3939
by: Mike Chirico | last post by:
Interesting Things to Know about MySQL Mike Chirico (mchirico@users.sourceforge.net) Copyright (GPU Free Documentation License) 2004 Last Updated: Mon Jun 7 10:37:28 EDT 2004 The latest version of this document can be found at: http://prdownloads.sourceforge.net/souptonuts/README_mysql.txt?download
5
1753
by: Hemanth | last post by:
Hello there, I'm trying to read an excel worksheet (with more than 5000 rows and 30 columns) using PHP. I'm using the "excelreader" script I found over the web - http://sourceforge.net/projects/phpexcelreader/. Seems to me, the script reads the values in all cells correctly and stores the total number of rows and columns existing in the worksheet (For example, in the excel file I'm using - total rows: 5375 and cols: 37). The problem...
3
5758
by: eieiohh | last post by:
MySQL 3.23.49 PHP 4.3.8 Apache 2.0.51 Hi All! Newbie.. I had a CRM Open Source application installed and running. Windows Xp crashed. I was able to copy the contents of the entire hard drive onto a USB External Hard Drive. I have to assume I also copied the data. I
1
2822
by: Good Man | last post by:
Hi there I've noticed some very weird things happening with my current MySQL setup on my XP Laptop, a development machine. For a while, I have been trying to get the MySQL cache to work. Despite entering the required lines to "my.ini" (the new my.cnf) through notepad AND MySQL Administrator, the cache does not work. So, today I took a peek at the 'Health' tab in MySQL Administrator.
6
5448
by: P-Rage | last post by:
Hello everyone, I was wondering what could possibly cause Internet Explorer 6 to loop a page usging a header(Location:) statement. For example, a page called 'page1.php': if((isset($_POST)) && ($name='valid')){ // insert some data into MySQL.
3
2409
by: monomaniac21 | last post by:
hi all i have a script that retrieves rows from a single table, rows are related to eachother and are retrieved by doing a series of while loops within while loops. bcos each row contains a text field they are fairly large. the net result is that when 60 or so results are reitreved the page size is 400kb! which takes too long to load. is there a way of shorterning this? freeing up the memory say, bcos what is actually displayed is not...
6
38495
Atli
by: Atli | last post by:
This is an easy to digest 12 step guide on basics of using MySQL. It's a great refresher for those who need it and it work's great for first time MySQL users. Anyone should be able to get through this without much trouble. Programming knowledge is not required. Index What is SQL? Why MySQL? Installing MySQL. Using the MySQL command line interface
0
8375
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8290
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8707
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8482
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6161
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5622
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4149
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4294
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2714
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.