array conversion

array conversion

am 19.02.2010 06:20:12 von Dasn

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.


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

Re: array conversion

am 19.02.2010 06:58:28 von Paul M Foster

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

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

Re: array conversion

am 19.02.2010 07:19:33 von Larry Garfield

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

Re: array conversion

am 19.02.2010 08:26:49 von Adam Richardson

--001636c5ba99bcb188047fef01fd
Content-Type: text/plain; charset=ISO-8859-1

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

--001636c5ba99bcb188047fef01fd--

Login Script: mysql_num_rows(): supplied argument is not a valid MySQL result resource

am 19.02.2010 09:30:59 von David Hutto

--0-350886073-1266568259=:68869
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable

The following script is supposed to validate a username and password in a m=
ysql db.=A0 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 r=
esource in /var/www/login.php on line 24
=0A
=0AWarning: Cannot modify header information - headers already sent by (ou=
tput 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 =3D mysql_query("SELECT * FROM 'userinfo' WHERE `user` =3D '$user=
' AND `pass` =3D '$pass`");

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

=A0 $user =3D $_POST['user']; //pulls the username from the form
=A0 $pw =3D $_POST['pass']; //pulls the pass from the form
=A0 $pass =3D 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 ne=
eded to show at this point for the current problem:

$act =3D $_GET['act']; //retrives the page action
if(empty($act)) //if there is no action
{
=A0 echo('

inform" id=3D"loginform">
=A0

Username
=A0
=A0


=A0

Password
=A0
=A0


=A0


=A0
=A0


=A0
');
}
elseif($act == "auth") //if our page action =3D auth
{
=A0 $user =3D $_POST['user']; //pulls the username from the form
=A0 $pw =3D $_POST['pass']; //pulls the pass from the form
=A0 $pass =3D md5($pw); //makes our password an md5
=A0 include("connect.php"); //connects to our mysql database
=A0 $login =3D mysql_query("SELECT * FROM `userinfo` WHERE `user` =3D '$use=
r' AND `pass` =3D '$pass`"); //selects info from our table if the row has t=
he same user and pass that our form does
=A0 if(!mysql_num_rows($login)) //if the username and pass are wrong
=A0 {
      =A0 header("Location: login.php");=A0 //redirects to our =
login page
      =A0 die(); //stops the page from going any further
=A0 }
=A0 else
=A0 {
      =A0 setcookie("user", $user, time()+3600);//sets our user=
cookie
              =A0 setcookie("pass", $pass, time=
()+3600);//sets our pass cookie
              =A0 header("Location: memprar.php=
");//instead of yourpage.php it would be your protected page
=A0 }=20
}
?>
=0A
--0-350886073-1266568259=:68869--

Re: Login Script: mysql_num_rows(): supplied argument is not a valid MySQL result resource

am 19.02.2010 11:01:15 von David Hutto

--0-606529243-1266573675=:10202
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable



--- On Fri, 2/19/10, David Hutto wrote:

From: David Hutto
Subject: Login Script: mysql_num_rows(): supplied argument is not a valid M=
ySQL result resource
To: php-general@lists.php.net
Date: Friday, February 19, 2010, 3:30 AM

The following script is supposed to validate a username and password in a m=
ysql db.=A0 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 r=
esource in /var/www/login.php on line 24
=0A
=0AWarning: Cannot modify header information - headers already sent by (ou=
tput 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 =3D mysql_query("SELECT * FROM 'userinfo' WHERE `user` =3D '$user=
' AND `pass` =3D '$pass`");

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

=A0 $user =3D $_POST['user']; //pulls the username from the form
=A0 $pw =3D $_POST['pass']; //pulls the pass from the form
=A0 $pass =3D 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=0A full login.=
php page.

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

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

$act =3D $_GET['act']; //retrives the page action
if(empty($act)) //if there is no action
{
=A0 echo('

inform" id=3D"loginform">
=A0

Username
=A0
=A0


=A0

Password
=A0
=A0


=A0


=A0
=A0


=A0
');
}
elseif($act == "auth") //if our page action =3D auth
{
=A0 $user =3D $_POST['user']; //pulls the username from the form
=A0 $pw =3D $_POST['pass']; //pulls the pass from=0A the form
=A0 $pass =3D md5($pw); //makes our password an md5
=A0 include("connect.php"); //connects to our mysql database
=A0 $login =3D mysql_query("SELECT * FROM `userinfo` WHERE `user` =3D '$use=
r' AND `pass` =3D '$pass`"); //selects info from our table if the row has t=
he same user and pass that our form does
=A0 if(!mysql_num_rows($login)) //if the username and pass are wrong
=A0 {
      =A0 header("Location: login.php");=A0 //redirects to our =
login page
      =A0 die(); //stops the page from going any further
=A0 }
=A0 else
=A0 {
      =A0 setcookie("user", $user, time()+3600);//sets our user=
cookie
              =A0 setcookie("pass", $pass, time=
()+3600);//sets our pass=0A cookie
              =A0 header("Location: memprar.php=
");//instead of yourpage.php it would be your protected page
=A0 }=20
}
?>

=0A
--0-606529243-1266573675=:10202--

Re: Login Script: mysql_num_rows(): supplied argument is nota valid MySQL result resource

am 19.02.2010 11:34:46 von Ashley Sheridan

--=-8Xtv9+EYm9YnBlPWmut5
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

On Fri, 2010-02-19 at 00:30 -0800, David Hutto wrote:

> 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:
>
> > $act = $_GET['act']; //retrives the page action
> if(empty($act)) //if there is no action
> {
> echo('


>

Username
>
>


>

Password
>
>


>


>
>


>
');
> }
> 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
> }
> }
> ?>
>
>
>
>


First, please create a new email when sending to the list and don't just
reply to the last one, as those of us with email clients that group by
threads get confused when the subject line appears to change mid-thread!

On to your question, you've got an error with your query, so it will
never work:

"SELECT * FROM `userinfo` WHERE `user` = '$user' AND `pass` =
'$pass`" // change that last back tick after $pass!

Lastly; protect your queries! That $user variable is open to injection.
Replacing it with something like $user =
mysql_real_escape_string($_POST['user']); Your $pass is protected (I
believe) because of what you're doing with the hash, but I'm not an
expert in these things, so it could be that this may not be enough.

Thanks,
Ash
http://www.ashleysheridan.co.uk



--=-8Xtv9+EYm9YnBlPWmut5--

Re: array conversion

am 19.02.2010 11:48:28 von Richard Quadling

On 19 February 2010 07:26, Adam Richardson wrote:
> Or,
>
> function new_arr(array $arr)
> {
>    $count =3D count($arr);
>    if ($count % 2 !=3D 0) throw new Exception('The new_arr() fu=
nction
> requires an even number of elements.');
>    for ($i =3D 0; $i < $count; $i +=3D 2)
>    {
>        $new_arr[$arr[$i]] =3D $arr[$i + 1];
>    }
>    return $new_arr;
> }
>
> $test =3D new_arr(array('k1', 'v1', 'k2', 'v2', 'k3', 'v3'));
>
> exit(var_dump($test));
>
> On Fri, Feb 19, 2010 at 1:19 AM, Larry Garfield w=
rote:
>
>> 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] =3D> key1
>> > >     [1] =3D> value1
>> > >     [2] =3D> key2
>> > >     [3] =3D> value2
>> > > )
>> > >
>> > > to
>> > >
>> > >
>> > > Array
>> > > (
>> > >     [key1] =3D> value1
>> > >     [key2] =3D> 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 =3D array($key1, $value1, $key2, $value2);
>> > $b =3D array();
>> > $numitems =3D count($a);
>> >
>> > for ($i =3D 0; $i < $numitems; $i++) {
>> >       if ($i % 2 == 0) {
>> >               $saved_key =3D $a[$i]=
;
>> >       }
>> >       elseif ($i % 2 == 1) {
>> >               $b[$saved_key] =3D $a=
[$i];
>> >       }
>> > }
>> >
>> > Code is crude and untested, but you get the idea.
>> >
>> > Paul
>>
>> This would be even shorter, I think:
>>
>> foreach ($items as $i =3D> $value) {
>>  $temp[$i % 2][] =3D $value;
>> }
>> $done =3D 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
>>
>>
>

I'd say that this cat is well and truly skinned!
>
> --
> Nephtali:  PHP web framework that functions beautifully
> http://nephtaliproject.com
>



--=20
-----
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=3DZEND002498&r=3D213474=
731
ZOPA : http://uk.zopa.com/member/RQuadling

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

Re: Login Script: mysql_num_rows(): supplied argument is not a valid MySQL result resource

am 19.02.2010 12:23:02 von David Hutto

--0-829179540-1266578582=:69430
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable



--- On Fri, 2/19/10, Ashley Sheridan wrote:

From: Ashley Sheridan
Subject: Re: [PHP] Login Script: mysql_num_rows(): supplied argument is not=
a valid MySQL result resource
To: "David Hutto"
Cc: php-general@lists.php.net
Date: Friday, February 19, 2010, 5:34 AM

=0A =0A =0AOn Fri, 2010-02-19 at 00:30 -0800, David Hutto wrote:=0A=
=0AThe following script is supposed to validate a username and password in =
a mysql db.=A0 When entering the username and password of a preregistered u=
ser, I get the following errors:

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



Warning: Cannot modify header information - headers already sent by (outpu=
t 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 =3D mysql_query("SELECT * FROM 'userinfo' WHERE `user` =3D '$user=
' AND `pass` =3D '$pass`");

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

=A0 $user =3D $_POST['user']; //pulls the username from the form
=A0 $pw =3D $_POST['pass']; //pulls the pass from the form
=A0 $pass =3D 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 ne=
eded to show at this point for the current problem:

$act =3D $_GET['act']; //retrives the page action
if(empty($act)) //if there is no action
{
=A0 echo('

inform" id=3D"loginform">
=A0

Username
=A0
=A0


=A0

Password
=A0
=A0


=A0


=A0
=A0


=A0
');
}
elseif($act == "auth") //if our page action =3D auth
{
=A0 $user =3D $_POST['user']; //pulls the username from the form
=A0 $pw =3D $_POST['pass']; //pulls the pass from the form
=A0 $pass =3D md5($pw); //makes our password an md5
=A0 include("connect.php"); //connects to our mysql database
=A0 $login =3D mysql_query("SELECT * FROM `userinfo` WHERE `user` =3D '$use=
r' AND `pass` =3D '$pass`"); //selects info from our table if the row has t=
he same user and pass that our form does
=A0 if(!mysql_num_rows($login)) //if the username and pass are wrong
=A0 {
      =A0 header("Location: login.php");=A0 //redirects to our =
login page
      =A0 die(); //stops the page from going any further
=A0 }
=A0 else
=A0 {
      =A0 setcookie("user", $user, time()+3600);//sets our user=
cookie
              =A0 setcookie("pass", $pass, time=
()+3600);//sets our pass cookie
              =A0 header("Location: memprar.php=
");//instead of yourpage.php it would be your protected page
=A0 }=20
}
?>



=20

=0AFirst, please create a new email when sending to the list and don't just=
reply to the last one, as those of us with email clients that group by thr=
eads get confused when the subject line appears to change mid-thread!
=0A
=0AOn to your question, you've got an error with your query, so it will nev=
er work:
=0A
=0A"SELECT * FROM `userinfo` WHERE `user` =3D '$user' AND `pass` =3D '$pass=
`"  =A0 // change that last back tick after $pass!
=0A
=0ALastly; protect your queries! That $user variable is open to injection. =
Replacing it with something like $user =3D mysql_real_escape_string($_POST[=
'user']); Your $pass is protected (I believe) because of what you're doing =
with the hash, but I'm not an expert in these things, so it could be that t=
his may not be enough.
=0A
Thanks,
=0AAsh
=0Ahttp://www.ashleysheridan.co.uk
=0A
=0A

Apologies for hijacking the thread, I hit reply all in a randomly picked em=
ail and deleted the info/subject line, guess that doesn't work.

Thanks for the advice, it's almost working right, all things considered.

David

--0-829179540-1266578582=:69430--

Re: array conversion

am 19.02.2010 16:52:01 von TedD

At 10:48 AM +0000 2/19/10, Richard Quadling wrote:
>On 19 February 2010 07:26, Adam Richardson wrote:
> Or,

Code fight!!!

http://www.webbytedd.com/ccc/array/

After reviewing the entries, mine does not provide any significant
difference. I did it as a mental exercise after looking at several
built-in array functions (array_flip(), array_combine(), etc. ) that
I thought might solve the problem, but didn't.

tedd
--
-------
http://sperling.com http://ancientstones.com http://earthstones.com

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

Re: array conversion

am 19.02.2010 17:56:56 von Richard Quadling

On 19 February 2010 15:52, tedd wrote:
> At 10:48 AM +0000 2/19/10, Richard Quadling wrote:
>>
>> On 19 February 2010 07:26, Adam Richardson wrote:
>>  Or,
>
> Code fight!!!
>
> http://www.webbytedd.com/ccc/array/
>
> After reviewing the entries, mine does not provide any significant
> difference. I did it as a mental exercise after looking at several built-=
in
> array functions (array_flip(), array_combine(), etc. ) that I thought mig=
ht
> solve the problem, but didn't.
>
> tedd
> --
> -------
> http://sperling.com  http://ancientstones.com  http://earthston=
es.com
>

Just wanting to join in.

$array =3D array
(
'key1',
'value1',
'key2',
'value2',
);

$result =3D array();
while(!is_null($result[array_shift($array)] =3D array_shift($array)));
array_pop($result);
print_r($result);
?>

outputs ...

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



--=20
-----
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=3DZEND002498&r=3D213474=
731
ZOPA : http://uk.zopa.com/member/RQuadling

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

Re: Login Script: mysql_num_rows(): supplied argument is not a validMySQL result resource

am 19.02.2010 18:30:26 von Mark Cilissen

David Hutto schreef:
>
> --- On Fri, 2/19/10, David Hutto wrote:
>
> From: David Hutto
> Subject: Login Script: mysql_num_rows(): supplied argument is not a valid MySQL result resource
> To: php-general@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:
>
> > $act = $_GET['act']; //retrives the page action
> if(empty($act)) //if there is no action
> {
> echo('


>

Username
>
>


>

Password
>
>


>


>
>


>
');
> }
> 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
> }
> }
> ?>
>
>
>
>
>
>
>
>
>

The query should be:
SELECT * FROM `userinfo` WHERE `user` = '$user' AND `pass` = '$pass'

Remember: ` for tables and columns, ' for strings.
Also, look up SQL Injection, as your script contains a huge vulnerability.
This can be fixed using mysql_real_escape_string, so it is this:
ELECT * FROM `userinfo` WHERE `user` =
'".mysql_real_escape_string($user)."' AND `pass` =
'".mysql_real_escape_string($pass)."'

--
Kind regards,
Mark Cilissen / Pixlism

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

Re: Re: Login Script: mysql_num_rows(): supplied argument isnot a valid MySQL result resource

am 19.02.2010 18:55:08 von Ashley Sheridan

--=-MtnbuFYDN3nTQGaKLTqC
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

On Fri, 2010-02-19 at 18:30 +0100, Mark Cilissen wrote:

> David Hutto schreef:
> >
> > --- On Fri, 2/19/10, David Hutto wrote:
> >
> > From: David Hutto
> > Subject: Login Script: mysql_num_rows(): supplied argument is not a valid MySQL result resource
> > To: php-general@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:
> >
> > > > $act = $_GET['act']; //retrives the page action
> > if(empty($act)) //if there is no action
> > {
> > echo('


> >

Username
> >
> >


> >

Password
> >
> >


> >


> >
> >


> >
');
> > }
> > 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
> > }
> > }
> > ?>
> >
> >
> >
> >
> >
> >
> >
> >
> >
>
> The query should be:
> SELECT * FROM `userinfo` WHERE `user` = '$user' AND `pass` = '$pass'
>
> Remember: ` for tables and columns, ' for strings.
> Also, look up SQL Injection, as your script contains a huge vulnerability.
> This can be fixed using mysql_real_escape_string, so it is this:
> ELECT * FROM `userinfo` WHERE `user` =
> '".mysql_real_escape_string($user)."' AND `pass` =
> '".mysql_real_escape_string($pass)."'
>
> --
> Kind regards,
> Mark Cilissen / Pixlism
>


I did cover all of those points and give the same sanitisation
suggestion in the email I sent to this question earlier!

Thanks,
Ash
http://www.ashleysheridan.co.uk



--=-MtnbuFYDN3nTQGaKLTqC--

Re: Re: Login Script: mysql_num_rows(): supplied argument isnota valid MySQL result resource

am 19.02.2010 21:00:49 von Mark Cilissen

Ashley Sheridan schreef:
> On Fri, 2010-02-19 at 18:30 +0100, Mark Cilissen wrote:
>
>> David Hutto schreef:
>>> --- On Fri, 2/19/10, David Hutto wrote:
>>>
>>> From: David Hutto
>>> Subject: Login Script: mysql_num_rows(): supplied argument is not a valid MySQL result resource
>>> To: php-general@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:
>>>
>>> >>> $act = $_GET['act']; //retrives the page action
>>> if(empty($act)) //if there is no action
>>> {
>>> echo('


>>>

Username
>>>
>>>


>>>

Password
>>>
>>>


>>>


>>>
>>>


>>>
');
>>> }
>>> 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
>>> }
>>> }
>>> ?>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>> The query should be:
>> SELECT * FROM `userinfo` WHERE `user` = '$user' AND `pass` = '$pass'
>>
>> Remember: ` for tables and columns, ' for strings.
>> Also, look up SQL Injection, as your script contains a huge vulnerability.
>> This can be fixed using mysql_real_escape_string, so it is this:
>> ELECT * FROM `userinfo` WHERE `user` =
>> '".mysql_real_escape_string($user)."' AND `pass` =
>> '".mysql_real_escape_string($pass)."'
>>
>> --
>> Kind regards,
>> Mark Cilissen / Pixlism
>>
>
>
> I did cover all of those points and give the same sanitisation
> suggestion in the email I sent to this question earlier!
>
> Thanks,
> Ash
> http://www.ashleysheridan.co.uk
>
>
>

Didn't see it, it was in another thread.

--
Kind regards,
Mark Cilissen / Pixlism

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

Re: array conversion

am 20.02.2010 12:18:52 von Clancy

Or:

$a = array ('Cats', 'white', 'Dogs', 'black', 'Mice', 'grey', 'Camels', 'brown');
$b = ''; // Just in case it has some leftover value
$k = 2* (int) (count ($a)/2); // ensure even no of terms
$i = 0; while ($i < $k)
{
$b[$a[$i++]] = $a[$i++]; // ***
}

And this works:
$i = 0; $k = array_keys($b);
while ($i < count($b)) { echo '

'.$i.': '.$k[$i].' = '. $b[$k[$i++]].'
'; }

0: Cats = white
1: Dogs = black
2: Mice = grey
3: Camels = brown

( *** I have always been wary of using statements like this because I was unsure when the
incrementing would occur, so I tried it.)

Clancy

On Fri, 19 Feb 2010 02:26:49 -0500, simpleshot@gmail.com (Adam Richardson) wrote:

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

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