php-general Digest 19 Feb 2010 10:37:57 -0000 Issue 6598

Topics (messages 302183 through 302195):

Magnetic Stripe Reader or Barcode to PHP
        302183 by: Daevid Vincent
        302184 by: Matt Neimeyer
        302185 by: Phpster
        302186 by: Williams, Dewey
        302188 by: Nathan Nobbe
        302195 by: Richard Quadling

Re: Export/Write rows from DBF to CSV
        302187 by: Paul M Foster

array conversion
        302189 by: Dasn
        302190 by: Paul M Foster
        302191 by: Larry Garfield
        302192 by: Adam Richardson

Login Script: mysql_num_rows(): supplied argument is not a valid MySQL result 
resource
        302193 by: David Hutto
        302194 by: David Hutto

Administrivia:

To subscribe to the digest, e-mail:
        php-general-digest-subscr...@lists.php.net

To unsubscribe from the digest, e-mail:
        php-general-digest-unsubscr...@lists.php.net

To post to the list, e-mail:
        php-gene...@lists.php.net


----------------------------------------------------------------------
--- Begin Message ---
I'll confess that I've done almost zero research on this topic as of right
now aside from pricing readers and blank cards.

So we're opening a new super-club here in Seattle. One of the ideas we'd
like to pursue is that people are assigned a card. This can be a magstripe
card or something with a barcode. Not really sure at the moment which way
to go.

The idea being that when they enter, we "swipe/scan" their card and we can
log stats (# of visits, # guests, favorite drink so the bar has it ready,
enter into contest, etc.) I was thinking we setup a simple netbook with a
USB reader at the front door when they pay. Then we have another back at
the bar. Connect via WiFi. Have a notebook somewhere behind the scenes that
runs LAMP (or a VM thereof). No internet needed.  The backend database and
even web/GUI stuff is of course trivial with any LAMP stack.

What I don't know is how do I interface _to_ PHP from say a magstripe
reader. They're dirt cheap ($50), but I assume come with zero software.
http://www.kanecal.net/mag-stripe-reader-scanner.html . And there are
hundreds of these gadgets out there all the same basically.

I guess what I'm hoping for is some pointers, from someone who's done this
sort of thing.

My gut thought is something so simple, where there is some XP software (or
Linux I suppose) that reads the card (and generally they only have a number
encoded on them) and this is sent via a URL we define, such as
http://192.168.10.100/door.php?id=123456 or
http://192.168.10.100/bar.php?id=123456 etc. (ignore the security issues
and all that. It's trivial to encode the parameters, plus it's a closed
system with WEP/WPA/WTF). But does anyone know of such a key piece of code?

Like I said, I'm not locked into magcards. It could be a barcode reader
just as easily. Or fingerprint scanner, or facial recognition. In the end
it's just a unique "thing" that maps to a unique number. I just need the
"FM" (F*ckin' Magic -- it's a programmer term -- look it up!) that goes in
between PHP and the hardware.

http://daevid.com

"Some people, when confronted with a problem, think 'I know, I'll use
XML.'"
Now they have two problems. 


--- End Message ---
--- Begin Message ---
If the scanning device hooks into the keyboard somehow it should be
easy. I believe that's called a "keyboard wedge". Basically, you scan
and it types.

My only exposure to this is using one of the old CueCat scanners to
index my personal library into a php app I cobbled together. The
CueCat hooked in line with the keyboard. You'd bring up the scanning
page and an onload Javascript would place the cursor into the "Scan
ISBN Here" text box. Scan the book and the input went where it was
needed. I believe it also sent an "enter" keystroke which submitted
the form. It's been a couple years since I played with it so there
might have been some additional software that kicked the "enter". Then
you process the data as you would for any other text field submission.
It worked well enough to catalog a couple thousand books in a single
evening.

Hope this helps

Matt

On Thu, Feb 18, 2010 at 5:42 PM, Daevid Vincent <dae...@daevid.com> wrote:
> So we're opening a new super-club here in Seattle. One of the ideas we'd
> like to pursue is that people are assigned a card. This can be a magstripe
> card or something with a barcode. Not really sure at the moment which way
> to go.

--- End Message ---
--- Begin Message --- The inputs are simple client side devices, lime th keyboard. I used as wasp brand barcode reader and it was configured to input the value and the tab to send it to te next field. They work well and easy to set up.

Bastien

Sent from my iPod

On Feb 18, 2010, at 5:42 PM, "Daevid Vincent" <dae...@daevid.com> wrote:

I'll confess that I've done almost zero research on this topic as of right
now aside from pricing readers and blank cards.

So we're opening a new super-club here in Seattle. One of the ideas we'd like to pursue is that people are assigned a card. This can be a magstripe card or something with a barcode. Not really sure at the moment which way
to go.

The idea being that when they enter, we "swipe/scan" their card and we can log stats (# of visits, # guests, favorite drink so the bar has it ready, enter into contest, etc.) I was thinking we setup a simple netbook with a USB reader at the front door when they pay. Then we have another back at the bar. Connect via WiFi. Have a notebook somewhere behind the scenes that runs LAMP (or a VM thereof). No internet needed. The backend database and
even web/GUI stuff is of course trivial with any LAMP stack.

What I don't know is how do I interface _to_ PHP from say a magstripe
reader. They're dirt cheap ($50), but I assume come with zero software.
http://www.kanecal.net/mag-stripe-reader-scanner.html . And there are
hundreds of these gadgets out there all the same basically.

I guess what I'm hoping for is some pointers, from someone who's done this
sort of thing.

My gut thought is something so simple, where there is some XP software (or Linux I suppose) that reads the card (and generally they only have a number
encoded on them) and this is sent via a URL we define, such as
http://192.168.10.100/door.php?id=123456 or
http://192.168.10.100/bar.php?id=123456 etc. (ignore the security issues and all that. It's trivial to encode the parameters, plus it's a closed system with WEP/WPA/WTF). But does anyone know of such a key piece of code?

Like I said, I'm not locked into magcards. It could be a barcode reader just as easily. Or fingerprint scanner, or facial recognition. In the end it's just a unique "thing" that maps to a unique number. I just need the "FM" (F*ckin' Magic -- it's a programmer term -- look it up!) that goes in
between PHP and the hardware.

http://daevid.com

"Some people, when confronted with a problem, think 'I know, I'll use
XML.'"
Now they have two problems.


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


--- End Message ---
--- Begin Message ---
Bar code scanners can be setup to decode the scan code on the card and
output it as text. On your incoming site, place cursor in text box, scan
card, code on card is entered as text, hit enter, text field sent to
database where all the F* Magic takes place.

You will also need a method to create the cards and get the data into
the database associated with the number.  You can use Word or other text
program and a bar code font to print the cards with bar-code, or use
pre-printed peel and stick barcode labels to place on a card. To
Initialize the card, have an new member interface where the data for the
member is entered, along with their bar-coded number.

There are a LARGE number of bar-code codes.  Pick one of the more common
that uses alphanumeric and symbols.

Tips: Have a backup reader or hard wire the bar-code scanner into a
power outlet.  The cheaper hand held ones don't have a very good battery
life.  Print the number on the card under the bar code.  This will help
when a drink smears the bar code or it becomes unreadable.  The door
person can enter the number by hand.

Dewey Williams

> -----Original Message-----
> From: Daevid Vincent [mailto:dae...@daevid.com]
> Sent: Thursday, February 18, 2010 5:43 PM
> To: php-gene...@lists.php.net
> Subject: [PHP] Magnetic Stripe Reader or Barcode to PHP
> 
> I'll confess that I've done almost zero research on this topic as of
> right
> now aside from pricing readers and blank cards.
> 
> So we're opening a new super-club here in Seattle. One of the ideas
> we'd
> like to pursue is that people are assigned a card. This can be a
> magstripe
> card or something with a barcode. Not really sure at the moment which
> way
> to go.
> 
> The idea being that when they enter, we "swipe/scan" their card and we
> can
> log stats (# of visits, # guests, favorite drink so the bar has it
> ready,
> enter into contest, etc.) I was thinking we setup a simple netbook
with
> a
> USB reader at the front door when they pay. Then we have another back
> at
> the bar. Connect via WiFi. Have a notebook somewhere behind the scenes
> that
> runs LAMP (or a VM thereof). No internet needed.  The backend database
> and
> even web/GUI stuff is of course trivial with any LAMP stack.
> 
> What I don't know is how do I interface _to_ PHP from say a magstripe
> reader. They're dirt cheap ($50), but I assume come with zero
software.
> http://www.kanecal.net/mag-stripe-reader-scanner.html . And there are
> hundreds of these gadgets out there all the same basically.
> 
> I guess what I'm hoping for is some pointers, from someone who's done
> this
> sort of thing.
> 
> My gut thought is something so simple, where there is some XP software
> (or
> Linux I suppose) that reads the card (and generally they only have a
> number
> encoded on them) and this is sent via a URL we define, such as
> http://192.168.10.100/door.php?id=123456 or
> http://192.168.10.100/bar.php?id=123456 etc. (ignore the security
> issues
> and all that. It's trivial to encode the parameters, plus it's a
closed
> system with WEP/WPA/WTF). But does anyone know of such a key piece of
> code?
> 
> Like I said, I'm not locked into magcards. It could be a barcode
reader
> just as easily. Or fingerprint scanner, or facial recognition. In the
> end
> it's just a unique "thing" that maps to a unique number. I just need
> the
> "FM" (F*ckin' Magic -- it's a programmer term -- look it up!) that
goes
> in
> between PHP and the hardware.
> 
> http://daevid.com
> 
> "Some people, when confronted with a problem, think 'I know, I'll use
> XML.'"
> Now they have two problems.
> 
> 
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php


--- End Message ---
--- Begin Message ---
On Thu, Feb 18, 2010 at 3:42 PM, Daevid Vincent <dae...@daevid.com> wrote:

> I'll confess that I've done almost zero research on this topic as of right
> now aside from pricing readers and blank cards.
>
> So we're opening a new super-club here in Seattle. One of the ideas we'd
> like to pursue is that people are assigned a card. This can be a magstripe
> card or something with a barcode. Not really sure at the moment which way
> to go.
>
> The idea being that when they enter, we "swipe/scan" their card and we can
> log stats (# of visits, # guests, favorite drink so the bar has it ready,
> enter into contest, etc.) I was thinking we setup a simple netbook with a
> USB reader at the front door when they pay. Then we have another back at
> the bar. Connect via WiFi. Have a notebook somewhere behind the scenes that
> runs LAMP (or a VM thereof). No internet needed.  The backend database and
> even web/GUI stuff is of course trivial with any LAMP stack.
>
> What I don't know is how do I interface _to_ PHP from say a magstripe
> reader. They're dirt cheap ($50), but I assume come with zero software.
> http://www.kanecal.net/mag-stripe-reader-scanner.html . And there are
> hundreds of these gadgets out there all the same basically.
>
> I guess what I'm hoping for is some pointers, from someone who's done this
> sort of thing.
>
> My gut thought is something so simple, where there is some XP software (or
> Linux I suppose) that reads the card (and generally they only have a number
> encoded on them) and this is sent via a URL we define, such as
> http://192.168.10.100/door.php?id=123456 or
> http://192.168.10.100/bar.php?id=123456 etc. (ignore the security issues
> and all that. It's trivial to encode the parameters, plus it's a closed
> system with WEP/WPA/WTF). But does anyone know of such a key piece of code?
>
> Like I said, I'm not locked into magcards. It could be a barcode reader
> just as easily. Or fingerprint scanner, or facial recognition. In the end
> it's just a unique "thing" that maps to a unique number. I just need the
> "FM" (F*ckin' Magic -- it's a programmer term -- look it up!) that goes in
> between PHP and the hardware.
>

DISCLAIMER - THOU ART ABOUT TO BEHOLD A RANT ...

ok, so it all started out a few years back when i started a company set out
to build a wireless capture portal.  we wanted to integrate with a 2-Touch
POS which had 'gift cards' which were magcards.  the idea was someone buys a
drink, the clerk swipes the card and the customer gets a window of time on
the internet.

so heres the deal (on linux [likely similar on other OS' though not sure on
the implementation there]).  when you attach a usb card reader, barcode
scanner or similar, if you have linux USB HID (HumanInputDevice) support the
kernel magically uses the module to treat input from the device as keyboard
input; simple right...

well this only works iff you have a terminal for the output to arrive at.
if you dont it goes nowhere (likely /dev/null or similar).  so if you want
something quick and dirty, a laptop or any box w/ a full window manager or
just a terminal will capture the output.  but thats for newbs (haha)...

the way we rolled w/ my product was w/ a linux server, running as the local
gateway w/ a full lamp stack to provide a UI for the business owner and the
customers.  new problem - how to capture the output of the device w/o a
terminal or UI on the server.  to cut to the chase we ended up developing a
*user space* driver for the card reader via libusb (which is cross-platform
btw).  i started out trying the java wrappers, but they all sucked ass.. so
ended up going w/ C.

basically, we wrote a simple single threaded daemon which sits and listens
for card swipes, once the card is swiped, the daemon uses the C system()
function to invoke a configurable executable program (which in our case was
a php script :)) and passes the captured info from the card via the shell.

the code is semi-generic but frankly we stopped off where our needs did at
the time.  the only real limitations are:
. it only provides a fraction of the info from the card (all info could be
passed ideally)
. it has hardcoded values for a minimag card reader (easily changed - these
are just values to identify the proper hardware in the USB tree and bind to
it)
. single threaded - it would be nice to have a main listener thread and fork
child threads to process the swipes (thats where binary from the device is
converted to ascii based on the usb2.0 spec) believe it or not you can
actually send 2 swipes before the first one is processed w/ a single thread
if you swipe it quickly enough!
. dependent on glib for some string functionality - i think it would be nice
to see it have as few dependencies beyond libusb (and pthreads if ever it
was threaded), though glib is quite common afaik

we originally ran the software on gentoo, but it could be packaged up for
pretty much any OS afaik since libusb is cross-platform.

anyways, if this is interesting at all im happy to share the code.  i was
planning to touch it up a bit at some point but dont have the time atm.  i
would be happy to help someone work with the code to tie it into their
system and put it to good use.

-nathan

--- End Message ---
--- Begin Message ---
On 18 February 2010 22:42, Daevid Vincent <dae...@daevid.com> wrote:
> I'll confess that I've done almost zero research on this topic as of right
> now aside from pricing readers and blank cards.
>
> So we're opening a new super-club here in Seattle. One of the ideas we'd
> like to pursue is that people are assigned a card. This can be a magstripe
> card or something with a barcode. Not really sure at the moment which way
> to go.
>
> The idea being that when they enter, we "swipe/scan" their card and we can
> log stats (# of visits, # guests, favorite drink so the bar has it ready,
> enter into contest, etc.) I was thinking we setup a simple netbook with a
> USB reader at the front door when they pay. Then we have another back at
> the bar. Connect via WiFi. Have a notebook somewhere behind the scenes that
> runs LAMP (or a VM thereof). No internet needed.  The backend database and
> even web/GUI stuff is of course trivial with any LAMP stack.
>
> What I don't know is how do I interface _to_ PHP from say a magstripe
> reader. They're dirt cheap ($50), but I assume come with zero software.
> http://www.kanecal.net/mag-stripe-reader-scanner.html . And there are
> hundreds of these gadgets out there all the same basically.
>
> I guess what I'm hoping for is some pointers, from someone who's done this
> sort of thing.
>
> My gut thought is something so simple, where there is some XP software (or
> Linux I suppose) that reads the card (and generally they only have a number
> encoded on them) and this is sent via a URL we define, such as
> http://192.168.10.100/door.php?id=123456 or
> http://192.168.10.100/bar.php?id=123456 etc. (ignore the security issues
> and all that. It's trivial to encode the parameters, plus it's a closed
> system with WEP/WPA/WTF). But does anyone know of such a key piece of code?
>
> Like I said, I'm not locked into magcards. It could be a barcode reader
> just as easily. Or fingerprint scanner, or facial recognition. In the end
> it's just a unique "thing" that maps to a unique number. I just need the
> "FM" (F*ckin' Magic -- it's a programmer term -- look it up!) that goes in
> between PHP and the hardware.
>
> http://daevid.com
>
> "Some people, when confronted with a problem, think 'I know, I'll use
> XML.'"
> Now they have two problems.
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

I used to develop EPOS (Electronic Point of Sale) s/w.

As already mentioned, most scanners (barcode and magswipe) connect via
the keyboard.

As far as the app is concerned, the data is coming from the keyboard.

You can normally program the barcode scanner to provide a prefix and a
suffix to the data from the scan.

This allows you to provide a "source" to the data.

For example, if you have a web page watching for keypresses, then you
want to know when the data is coming from the scanner rather than the
keyboard.

This allows you to do "out of order" processing. So, you scan 10
items, item 3 requires some additional data, but the next 7 items
aren't "lost" because you didn't deal with item 3. The webpage can
push the barcode data (AJAX maybe) to the server so they are all
logged and fed back as part of the "basket" logic.

If you have a prefix, you'll also need a suffix to tell the app that
the data has finished.


A similar situation exists with mag cards. A normal mag card (like
your credit cards) can have up to 3 "tracks" of data. Normally track 2
is the interesting one and will normally consist of the following
data.

Start Sentinel
Card Number
Mid Sentinel
Expiry Date
Possibly other data (it was a LONG time ago since I was writing EPOS
systems, so ... memory currently giving me a 404.)
End Sentinel

The sentinels aren't full characters, but are "signals" to the reader.
The reader can be programmed to issue a specific character for each
sentinel.

So again, you can read from the mag card "out of order". E.g. you can
scan your club card or your credit card at the beginning of the sale
and then add your items. All without touching the keyboard to change
the "focus" of the input. This was a major enhancement for usability
for our customers when I introduced this. And that was for a DOS app,
not GUI or Web!

If you have an account on Experts Exchange, take a look at
http://www.experts-exchange.com/Internet/Web_Development/Q_24179189.html?sfQueryTermInfo=1+barcod+javascript+rquadling
which was a question on using a barcode reader in a web app and I gave
JS code allowing for the "out of order" processing I mentioned here.


If you are generating your own cards, then the quickest/cheapest way
is to get blank cardboard "cards", print a barcode on them and then
laminate them. That way you can build your own layout relatively
cheaply.

You also don't have to use black/white bars. For one our systems
(Access control to buildings), we used infra-red detectable ink. When
it was printed, we could only just see it in the right light. The
scanners use a red laser and picked up the code easily. And the
barcode was overprinted on a photo ID card and them laminated.


Hope that all makes sense.

Regards,

Richard.
-- 
-----
Richard Quadling
"Standing on the shoulders of some very clever giants!"
EE : http://www.experts-exchange.com/M_248814.html
EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp
Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
ZOPA : http://uk.zopa.com/member/RQuadling

--- End Message ---
--- Begin Message ---
On Thu, Feb 18, 2010 at 02:31:00PM -0800, OBXer wrote:

> 
> Rahul S. Johari-2 wrote:
> >
> > Ave,
> >
> > I'm trying to retrieve data from a DBF database and write it to a CSV
> > file in a comma delimited format. I'm able to get the data and write
> > it to CSV, but it only writes the last row/record ... not all the
> > records. I know I don't have the correct code and I'm hoping someone
> > can help me...
> >
> > _____________
> > #CREATE CSV
> > $date = date('mdy');
> > $_file = 'CSV/TransferData_'.$date.'.csv';
> > $_fp = @fopen( $_file, 'w' );
> >
> >     #SELECT DBF TO OPEN - READ ONLY
> >     $db = dbase_open("mydata.dbf", 0);
> >     #PULL UP RECORD
> >     if ($db) {
> >       $record_numbers = dbase_numrecords($db);
> >       for ($i = 1; $i <= $record_numbers; $i++) {
> >             $row = dbase_get_record_with_names($db, $i);
> >
> >             #WRITE ROWS TO VARIABLE
> >             $_csv_data =
> trim($row['PHONE']).",".trim($row['DATE']).","."\n";
> > <-- THIS is where my problem is! This only writes the last row!!
> >       }
> >     }
> >
> > #WRITE TO CSV
> > @fwrite( $_fp, $_csv_data );
> > @fclose( $_fp );
> >
> >
> 
> I'm trying to adopt this piece of code for my use.  I fixed the csv_data .=
> trim error.  Does anyone know how I can fix empty fields?  Everything is
> dumping to a csv file but information is not matching up.  I don't know if I
> can insert a space or something if field is blank?
> 

What do you mean "fix" empty fields? And what do you mean by
"information is not matching up"?

You shouldn't insert spaces where there are blank fields. Just leave
them blank. The commas are supposed to delimit all the fields, including
the empty ones. Like:

John,Smith,,123 Main St,Tucson,AZ,,,,

Paul

-- 
Paul M. Foster

--- End Message ---
--- Begin Message ---
Hi guys. How to convert an array like:

Array
(
    [0] => key1
    [1] => value1
    [2] => key2
    [3] => value2
)

to


Array
(
    [key1] => value1
    [key2] => value2
)

Is there a built-in function to do this?
Please Cc me. :)
Thank you in advance.


--- End Message ---
--- Begin Message ---
On Fri, Feb 19, 2010 at 01:20:12PM +0800, Dasn wrote:

> Hi guys. How to convert an array like:
>
> Array
> (
>     [0] => key1
>     [1] => value1
>     [2] => key2
>     [3] => value2
> )
>
> to
>
>
> Array
> (
>     [key1] => value1
>     [key2] => value2
> )
>
> Is there a built-in function to do this?
> Please Cc me. :)
> Thank you in advance.

I don't believe so, but rolling your own should not be too hard:

$a = array($key1, $value1, $key2, $value2);
$b = array();
$numitems = count($a);

for ($i = 0; $i < $numitems; $i++) {
        if ($i % 2 == 0) {
                $saved_key = $a[$i];
        }
        elseif ($i % 2 == 1) {
                $b[$saved_key] = $a[$i];
        }
}

Code is crude and untested, but you get the idea.

Paul

-- 
Paul M. Foster

--- End Message ---
--- Begin Message ---
On Thursday 18 February 2010 11:58:28 pm Paul M Foster wrote:
> On Fri, Feb 19, 2010 at 01:20:12PM +0800, Dasn wrote:
> > Hi guys. How to convert an array like:
> >
> > Array
> > (
> >     [0] => key1
> >     [1] => value1
> >     [2] => key2
> >     [3] => value2
> > )
> >
> > to
> >
> >
> > Array
> > (
> >     [key1] => value1
> >     [key2] => value2
> > )
> >
> > Is there a built-in function to do this?
> > Please Cc me. :)
> > Thank you in advance.
> 
> I don't believe so, but rolling your own should not be too hard:
> 
> $a = array($key1, $value1, $key2, $value2);
> $b = array();
> $numitems = count($a);
> 
> for ($i = 0; $i < $numitems; $i++) {
>       if ($i % 2 == 0) {
>               $saved_key = $a[$i];
>       }
>       elseif ($i % 2 == 1) {
>               $b[$saved_key] = $a[$i];
>       }
> }
> 
> Code is crude and untested, but you get the idea.
> 
> Paul

This would be even shorter, I think:

foreach ($items as $i => $value) {
  $temp[$i % 2][] = $value;
}
$done = array_combine($temp[0], $temp[1]);

(Also untested, just off the cuff...)

--Larry Garfield

--- End Message ---
--- Begin Message ---
Or,

function new_arr(array $arr)
{
    $count = count($arr);
    if ($count % 2 != 0) throw new Exception('The new_arr() function
requires an even number of elements.');
    for ($i = 0; $i < $count; $i += 2)
    {
        $new_arr[$arr[$i]] = $arr[$i + 1];
    }
    return $new_arr;
}

$test = new_arr(array('k1', 'v1', 'k2', 'v2', 'k3', 'v3'));

exit(var_dump($test));

On Fri, Feb 19, 2010 at 1:19 AM, Larry Garfield <la...@garfieldtech.com>wrote:

> On Thursday 18 February 2010 11:58:28 pm Paul M Foster wrote:
> > On Fri, Feb 19, 2010 at 01:20:12PM +0800, Dasn wrote:
> > > Hi guys. How to convert an array like:
> > >
> > > Array
> > > (
> > >     [0] => key1
> > >     [1] => value1
> > >     [2] => key2
> > >     [3] => value2
> > > )
> > >
> > > to
> > >
> > >
> > > Array
> > > (
> > >     [key1] => value1
> > >     [key2] => value2
> > > )
> > >
> > > Is there a built-in function to do this?
> > > Please Cc me. :)
> > > Thank you in advance.
> >
> > I don't believe so, but rolling your own should not be too hard:
> >
> > $a = array($key1, $value1, $key2, $value2);
> > $b = array();
> > $numitems = count($a);
> >
> > for ($i = 0; $i < $numitems; $i++) {
> >       if ($i % 2 == 0) {
> >               $saved_key = $a[$i];
> >       }
> >       elseif ($i % 2 == 1) {
> >               $b[$saved_key] = $a[$i];
> >       }
> > }
> >
> > Code is crude and untested, but you get the idea.
> >
> > Paul
>
> This would be even shorter, I think:
>
> foreach ($items as $i => $value) {
>  $temp[$i % 2][] = $value;
> }
> $done = array_combine($temp[0], $temp[1]);
>
> (Also untested, just off the cuff...)
>
> --Larry Garfield
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


-- 
Nephtali:  PHP web framework that functions beautifully
http://nephtaliproject.com

--- End Message ---
--- Begin Message ---
The following script is supposed to validate a username and password in a mysql 
db.  When entering the username and password of a preregistered user, I get the 
following errors:

Warning:  mysql_num_rows(): supplied argument is not a valid MySQL result 
resource in /var/www/login.php on line 24



Warning:  Cannot modify header information - headers already sent by (output 
started at /var/www/login.php:24) in /var/www/login.php on line 26

On line 24 is:

>>>if(!mysql_num_rows($login)) //if the username and pass are wrong

--The supplied argument is $login, which is previously defined as:

>>>$login = mysql_query("SELECT * FROM 'userinfo' WHERE `user` = '$user' AND 
>>>`pass` = '$pass`");

--which is further defined above it as these values:

  $user = $_POST['user']; //pulls the username from the form
  $pw = $_POST['pass']; //pulls the pass from the form
  $pass = md5($pw); //makes our password an md

So why is the sum of those previous definitions an invalid argument for the 
mysql_query() to test for whether the username and md5 password values are 
true/equivalent to each other?

Thanks for any help you may be able to provide, below is the full login.php 
page.

David
********************************************************

This is the full login.php script, I'm pretty sure no other portions are needed 
to show at this point for the current problem:

<?php
$act = $_GET['act']; //retrives the page action
if(empty($act)) //if there is no action
{
  echo('<form action="login.php?act=auth" method="post" name="loginform" 
id="loginform">
  <p>Username
  <input type="text" name="user">
  </p>
  <p>Password
  <input type="password" name="pass">
  </p>
  <p>
  <input type="submit" name="Submit" value="Login">
  </p>
  </form>');
}
elseif($act == "auth") //if our page action = auth
{
  $user = $_POST['user']; //pulls the username from the form
  $pw = $_POST['pass']; //pulls the pass from the form
  $pass = md5($pw); //makes our password an md5
  include("connect.php"); //connects to our mysql database
  $login = mysql_query("SELECT * FROM `userinfo` WHERE `user` = '$user' AND 
`pass` = '$pass`"); //selects info from our table if the row has the same user 
and pass that our form does
  if(!mysql_num_rows($login)) //if the username and pass are wrong
  {
        header("Location: login.php");  //redirects to our login page
        die(); //stops the page from going any further
  }
  else
  {
        setcookie("user", $user, time()+3600);//sets our user cookie
                setcookie("pass", $pass, time()+3600);//sets our pass cookie
                header("Location: memprar.php");//instead of yourpage.php it 
would be your protected page
  } 
}
?>



      

--- End Message ---
--- Begin Message ---

--- On Fri, 2/19/10, David Hutto <dwightdhu...@yahoo.com> wrote:

From: David Hutto <dwightdhu...@yahoo.com>
Subject: Login Script: mysql_num_rows(): supplied argument is not a valid MySQL 
result resource
To: php-gene...@lists.php.net
Date: Friday, February 19, 2010, 3:30 AM

The following script is supposed to validate a username and password in a mysql 
db.  When entering the username and password of a preregistered user, I get the 
following errors:

Warning:  mysql_num_rows(): supplied argument is not a valid MySQL result 
resource in /var/www/login.php on line 24



Warning:  Cannot modify header information - headers already sent by (output 
started at /var/www/login.php:24) in /var/www/login.php on line 26

On line 24 is:

>>>if(!mysql_num_rows($login)) //if the username and pass are wrong

--The supplied argument is $login, which is previously defined as:

>>>$login = mysql_query("SELECT * FROM 'userinfo' WHERE `user` = '$user' AND 
>>>`pass` = '$pass`");

--which is further defined above it as these values:

  $user = $_POST['user']; //pulls the username from the form
  $pw = $_POST['pass']; //pulls the pass from the form
  $pass = md5($pw); //makes our password an md

So why is the sum of those previous definitions an invalid argument for the 
mysql_query() to test for whether the username and md5 password values are 
true/equivalent to each other?

Because basically !mysql_num_rows($login) is just if'ing the lack of a 
user/pass match, else it continues to set cookie and session variables.

If I'm looking at this wrong let me know.

Thanks for any help you may be able to provide, below is the
 full login.php page.

David
********************************************************

This is the full login.php script, I'm pretty sure no other portions are needed 
to show at this point for the current problem:

<?php
$act = $_GET['act']; //retrives the page action
if(empty($act)) //if there is no action
{
  echo('<form action="login.php?act=auth" method="post" name="loginform" 
id="loginform">
  <p>Username
  <input type="text" name="user">
  </p>
  <p>Password
  <input type="password" name="pass">
  </p>
  <p>
  <input type="submit" name="Submit" value="Login">
  </p>
  </form>');
}
elseif($act == "auth") //if our page action = auth
{
  $user = $_POST['user']; //pulls the username from the form
  $pw = $_POST['pass']; //pulls the pass from
 the form
  $pass = md5($pw); //makes our password an md5
  include("connect.php"); //connects to our mysql database
  $login = mysql_query("SELECT * FROM `userinfo` WHERE `user` = '$user' AND 
`pass` = '$pass`"); //selects info from our table if the row has the same user 
and pass that our form does
  if(!mysql_num_rows($login)) //if the username and pass are wrong
  {
        header("Location: login.php");  //redirects to our login page
        die(); //stops the page from going any further
  }
  else
  {
        setcookie("user", $user, time()+3600);//sets our user cookie
                setcookie("pass", $pass, time()+3600);//sets our pass
 cookie
                header("Location: memprar.php");//instead of yourpage.php it 
would be your protected page
  } 
}
?>





      


      

--- End Message ---

Reply via email to