Re: [PHP] PDO Prevent duplicate field names?

2012-07-02 Thread Scott Baker
On 07/02/2012 03:34 PM, Matijn Woudt wrote:
> Why the  would you want to return 2 columns with the same name?
> To be short, there's no such function, so you have to:
> 1) Rename one of the columns
> 2) or, use fetch_row with numerical indexes instead of fetch_assoc.

My "real world" scenario was this

SELECT a.CustID, b.*
FROM Customer a
LEFT JOIN Sales B USING (CustID)
WHERE a.CustID = 1234;

In that case, there was a record in Customer, but not in Sales. Sales
returned CustID as NULL, which overwrote the one from Customer.

It was my mistake, and the SQL was easily fixed. But it woulda been nice
to have PHP realize there was a dupe when it was building that array to
return to me.


-- 
Scott Baker - Canby Telcom
System Administrator - RHCE - 503.266.8253



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



[PHP] PDO Prevent duplicate field names?

2012-07-02 Thread Scott Baker
$sql = "SELECT First, Last, Age, 'Foobar' AS Last;";

This is a simplified example of a SQL query where we're returning two
fields with the same name (Last). When I do a fetch_assoc with this
query I only get three fields, as the second "Last" field over writes
the first one.

I was hoping there was some method with PDO that would detect that and
throw a warning. Maybe some sort of "strict mode" that would tell me I'm
doing something stupid. Is there a way to catch this before it bites me?

It already bit me, but moving forward it'd be nice if PHP saw that
before I spent an hour debugging it again.

- Scott

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



Re: [PHP] Benchmark two functions against each other?

2011-06-27 Thread Scott Baker
On 06/27/2011 10:33 AM, Stuart Dallas wrote:
> 
> You're not going to find a service that will let you do that - allowing
> arbitrary PHP to be entered into a website to be executed is far too
> dangerous.

I would have thought so too but I found these:

http://codepad.viper-7.com/
http://codepad.org/
http://ideone.com/

> Just knocked this up, should do what you
> want:Â https://gist.github.com/1049335

Thanks I'll check it out!

-- 
Scott Baker - Canby Telcom
System Administrator - RHCE - 503.266.8253

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



[PHP] Benchmark two functions against each other?

2011-06-27 Thread Scott Baker
I have functions that I'd like to benchmark and compare. What are the
best PHP libraries or websites to do that? Something like jsperf.com but
for PHP would be ideal.

- Scott

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



[PHP] Get all the keys from a hierarchical hash

2011-06-23 Thread Scott Baker
I have a multi-tier hash (see below) and I'd like to be "search" the
hash for a given $id, and return that section, regardless of how many
layers deep it is. Sort of like how xpath works?

Once I have that I'd like get ALL the children of a given node. So I
could ask for 86, and get 36, 38, 56, etc and all THEIR children.
Basically I want *all* the ID #s that are children.

Array
(
[88] => Array
(
[109] =>
)

[86] => Array
(
[36] => Array
(
[8] =>
[121] =>
[127] =>
[135] =>
[144] =>
[161] =>
[165] =>
)

[38] => Array
(
[18] =>
[39] =>
[156] =>
[158] =>
[182] =>
)

[56] =>
[97] =>
[107] => Array
(
[240] =>
)

[115] =>
[123] =>
[146] =>
[149] =>
[223] =>
)

[157] => Array
(
[3] => Array
(
[5] => Array
(
[11] =>
)

[13] => Array
(
[6] =>
[7] =>
[98] => Array
(
[81] =>
)

)

[111] => Array
(
[10] =>
[17] =>
[110] =>
)

)

[148] => Array
(
[9] =>
[87] =>
[102] =>
[104] =>
[114] =>
[130] =>
[133] =>
[160] =>
[201] =>
[237] =>
[238] =>
)

)

)

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



Re: [PHP] Create a hierarchical hash from flat source

2011-06-22 Thread Scott Baker
On 06/22/2011 03:17 PM, Simon J Welsh wrote:
> You still need to pass the value by reference to assign_children(), so:
> $new = &$leaf[$pid];
> assign_children($pid,$list,&$new);

One last thing I fixed was that PHP was complaining that run-time pass
by reference was deprecated. I changed assign_children to be

function assign_children($id,$list,&$leaf)

Which solved that also!

-- 
Scott Baker - Canby Telcom
System Administrator - RHCE - 503.266.8253

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



Re: [PHP] Create a hierarchical hash from flat source

2011-06-22 Thread Scott Baker
On 06/22/2011 03:17 PM, Simon J Welsh wrote:
> You still need to pass the value by reference to assign_children(), so:
> $new = &$leaf[$pid];
> assign_children($pid,$list,&$new);

Aha... that was it! Thanks!

-- 
Scott Baker - Canby Telcom
System Administrator - RHCE - 503.266.8253

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



Re: [PHP] Create a hierarchical hash from flat source

2011-06-22 Thread Scott Baker
On 06/22/2011 03:06 PM, Simon J Welsh wrote:
> On further inspection, that's not the problem at all. The problem's around 
> assign_children($pid,$list,&$new);
> 
> The previous line you defined $new with $new = $leaf[$pid], *copying* that 
> node into $new. Thus the assign_children() call updates $new, but not 
> $lead[$pid].
> 
> You can fix this by either assigning $new by reference ($new =& $leaf[$pid]) 
> or by passing a reference to $lead[$pid] to assign_children instead of $new.

I updated the code:

http://www.perturb.org/tmp/tree.txt

I still get the same output though. Only one level of depth :(

-- 
Scott Baker - Canby Telcom
System Administrator - RHCE - 503.266.8253

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



[PHP] Create a hierarchical hash from flat source

2011-06-22 Thread Scott Baker
I have a bunch of records in a DB that look like

id | parent_id
--
1  | 4
2  | 4
3  | 2
4  | 0
5  | 2
6  | 1
7  | 3
8  | 7
9  | 7

I want to build a big has that looks like:

4 -> 1 -> 6
  -> 2 -> 3 -> 7 -> 9
   -> 5  -> 8

I'm like 90% of the way there, but I can't get my recursive assignment
to work. Has anyone done this before that could offer some pointers?
Here is my sample code thus far:

http://www.perturb.org/tmp/tree.txt

I can get one level of depth, but nothing more than that :(

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



[PHP] PHP intreprets trailing slashes incorrectly?

2011-05-19 Thread Scott Baker
I have a script:

http://www.perturb.org/index.php

I accidentally put a trailing / on the url and it STILL loaded:

http://www.perturb.org/index.php/

Is that a bug in URL interpretation? I've tried it on three servers and
all seem to have the same behavior. All three were Apache on Linux, but
different versions as far back as PHP 5.2.x.

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



Re: [PHP] Overriding session length in existing session?

2011-03-08 Thread Scott Baker
On 03/08/2011 09:46 AM, Marc Guay wrote:
> Hi Scott,
> 
> I'm glad you resolved your problem.  I'm curious about your method
> though, as it seems to be an entirely different approach to my own.
> How do you refer to your session data throughout the rest of the site?
>  Do you always reference the $_COOKIE variables or do you utilise
> $_SESSION's at some point?

I'll summarize... Everytime I hit a page I open a session with a session
time of 7 days. If the user logs in correctly it stores the user
information in $_SESSION, and then the site sees their login info and
lets them past the login screen. If, at the login screen, they select
public terminal it sets the session cookie length to 0 and regenerates
the cookie to expire on browser close.

Everything after that is pulled from the $_SESSION variable.

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



Re: [PHP] Overriding session length in existing session?

2011-03-07 Thread Scott Baker
On 03/04/2011 11:48 AM, Marc Guay wrote:
> I think that my suggestion is still a valid solution, someone correct
> me if I'm wrong.  Let's say your code went like this:
> 
> session_start();

I did a ton of digging and came up with session_regenerate_id()

In my header.php I start the session as normal, and IF the user selects
"public terminal" I use the call set the session cookie length as 0 and
then regenerate the session cookie.

if ($public_term) {
   session_set_cookie_params(0);
   session_regenerate_id();
}

This way it defaults to a cookie of X days, but if it's a public
terminal it resets the cookie to expire at browser close.

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



Re: [PHP] Overriding session length in existing session?

2011-03-07 Thread Scott Baker
On 03/04/2011 11:48 AM, Marc Guay wrote:
> I think that my suggestion is still a valid solution, someone correct
> me if I'm wrong.  Let's say your code went like this:
> 
> session_start();

I did a ton of digging and came up with session_regenerate_id()

In my header.php I start the session as normal, and IF the user selects
"public terminal" I use the call set the session cookie length as 0 and
then regenerate the session cookie.

if ($public_term) {
   session_set_cookie_params(0);
   session_regenerate_id();
}

This way it defaults to a cookie of X days, but if it's a public
terminal it resets the cookie to expire at browser close.

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



Re: [PHP] Overriding session length in existing session?

2011-03-04 Thread Scott Baker
On 03/04/2011 05:37 AM, Marc Guay wrote:
> Howdy.  Don't sessions expire when the browser closes as a rule?  Do
> you mean the session cookie?  Why not store the cookie, if one exists,
>  in a $_SESSION variable in your header file and then refer to that in
> the rest of your code, rather than the cookie.  Then when you want to
> destroy the session cookie, you can overwrite it and the existing
> session will still flow using the $_SESSION vars until the browser
> closes.
> 
> It's early here, I hope that sort of makes sense and that I've at
> least sort of understood the problem.

I'm setting my session cookies, and my session data on the server to
expire X days in the future. That's set globally in my header.php file.
When the use logs in it stores their login information in the session
and it's good for 7 days. I have an option on the login page "public
terminal" that I want to make clear the session on browser close. The
problem is that I've already (in header.php) told the session cookie
that it's good for 7 days. I was just trying to override that already
established session cookie.

- Scott

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



Re: [PHP] Overriding session length in existing session?

2011-03-03 Thread Scott Baker
On 03/03/2011 04:31 PM, tedd wrote:
> At 2:58 PM -0800 3/3/11, Scott Baker wrote:
>> I have a global header.php file that sets up a bunch of stuff: DB,
>> global variables, and does session_start(). My header.php looks like
>> this:
>>
>> #header.php
>> $cookie_life = (86400 * 7); // Cookies last for seven days
>> session_set_cookie_params($cookie_life,"/",".domain.com",true);
>> session_start();
>>
>> This is called globally in *all* my scripts. In another script I'd
>> really like to set the session to expire after the browser closes if a
>> uses clicks "public terminal" or something.
>>
>> I thought I could just set the session cookie to expire after the
>> browser closes. Can I override the already started session by doing
>> something like this in my index.php:
>>
>> #index.php
>> include('header.php');
>> setcookie(session_name(), '', 0 ,"/",".domain.com");
>>
>> When I do this the session expires IMMEDIATELY. I must be missing
>> something?
> 
> Simple answer -- put session_start() at the start of your code -- first
> line.

Of index.php or header.php? You lost me.

-- 
Scott Baker - Canby Telcom
System Administrator - RHCE - 503.266.8253

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



[PHP] Overriding session length in existing session?

2011-03-03 Thread Scott Baker
I have a global header.php file that sets up a bunch of stuff: DB,
global variables, and does session_start(). My header.php looks like this:

#header.php
$cookie_life = (86400 * 7); // Cookies last for seven days
session_set_cookie_params($cookie_life,"/",".domain.com",true);
session_start();

This is called globally in *all* my scripts. In another script I'd
really like to set the session to expire after the browser closes if a
uses clicks "public terminal" or something.

I thought I could just set the session cookie to expire after the
browser closes. Can I override the already started session by doing
something like this in my index.php:

#index.php
include('header.php');
setcookie(session_name(), '', 0 ,"/",".domain.com");

When I do this the session expires IMMEDIATELY. I must be missing something?

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



[PHP] Re: [PDO] Re: [PHP] PDO working via Apache but not at the command line?

2010-10-18 Thread Scott Baker
On 10/18/2010 06:27 PM, Wez Furlong wrote:
> Things to check:
> 
> - Environment: what env vars are set or not set in your Apache vs. CLI
> - Owner: are you running as the same user as your web server?
> - Do you or the web server have some kind of "rc" file that might impact
> how things run?

Wez you're a genius. When I ran it as the same user as apache it works
fine. That got me thinking that it makes a log in /tmp. Checking the log
it was only writable by the apache user. A little chmod later and now my
script runs perfectly under apache and cli.

Thanks for helping me think outside the box. I spent all day puzzled by
this, you just made my night.

- Scott

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



Re: [PHP] PDO working via Apache but not at the command line?

2010-10-18 Thread Scott Baker

On 10/18/2010 02:17 PM, a...@ashleysheridan.co.uk wrote:

It's most likely because both cli and web modules are using different
php.ini config files. See what the output of a phpinfo() call in both
browser and command line.


I didn't even think about it parsing different php.ini files. Checking 
the output of phpinfo() I see it's calling the same php.ini 
(/usr/local/lib/php.ini) though. :(


--
Scott Baker - Canby Telcom
System Administrator - RHCE - 503.266.8253

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



[PHP] PDO working via Apache but not at the command line?

2010-10-18 Thread Scott Baker
I have the following very simple script that uses PDO/FreeTDS to connect 
to a mssql server. I have PHP Version 5.3.3 running on Linux under 
Apache. When I view this script via apache/firefox I get proper output.


If I try and run this via the command line like "php db_dump.php" I get 
an error connecting to the DB:


Unable to connect to Omnia: SQLSTATE[HY000] Unable to connect: Adaptive 
Server is unavailable or does not exist (severity 9). Does Apache call 
PHP differently than the cli would? Why would Apache method work find, 
but direct from the command line cause a problem? Do I need to call php 
from the cli with some other argument?


--


$num || $num = 197804;

$dbh = omnia_connect();
$sql = "EXECUTE P_SaaS_AccountServiceFeatures_Retrieve $num";
$sth = $dbh->prepare($sql);
$sth->execute();

$foo = $sth->fetchAll(PDO::FETCH_ASSOC);

foreach ($foo as $i) {
if ($i['ItemStatus'] != 'I') {
$out .= $i['Component'] . "\n";
}
}

print $out;



function omnia_connect() {
putenv('FREETDSCONF=/etc/freetds.conf');
$db = "DB_NAME";
$user = "user";
$pass = "pass";

$dsn = "dblib:host=Omnia;dbname=$db";

try {
$dbh = new PDO($dsn, $user, $pass);
} catch (PDOException $e) {
echo 'Unable to connect to Omnia: ' . $e->getMessage();
exit;
}

return $dbh;
}

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