[PHP] newbie PDO query display question

2013-06-16 Thread dealTek
Hi all,

newbie PDO question...

I think (hard to tell - buried in a wrapper class) I am doing a select query 
using  - PDO::FETCH_ASSOC like...

wrapper -- return $pdostmt->fetchAll(PDO::FETCH_ASSOC);

---

so my query is like...

$results = $db->select("mytable", "id = 201"); //just 1 exact record)

then I can loop like..
foreach ($results as $result) {
    
?>

  




  

This all works fine.

---> But since I only have 1 exact record - I don't need to LOOP anything - 
so...

Q: How do I display the columns without a loop


these fail - so what will work

echo $results["First"];
echo $results["First"][0]; ???
echo $results["First"][1]; ???





--
Thanks,
Dave
r...@musenet.com
[db-3]




--
Thanks,
Dave - DealTek
deal...@gmail.com
[db-3]



Re: [PHP] LightBox click detection

2013-06-16 Thread Micky Hulse
On Fri, Jun 14, 2013 at 8:52 AM, Tedd Sperling  wrote:
> Here's the problem --  I need to count the number of times a user activates a 
> LightBox -- how do you do that?

If you're using Google Analytics, you can use click tracking:



I recently setup click tracking on lightbox open, close and various
other modal window bits/pieces for a metered pay wall system.

For example, within the lightbox/modal window "open" method:

[code]

// Track this lightbox instance:
window._gaq.push([
'_trackEvent',  // Method.
'Paymeter Lightbox',// Group.
'Lightbox OPEN group ' + count_txt, // Append count to "action" text.
count_txt + ' OPEN event'   // Prepend count to "label" text.
]);

[/code]

The advantage to using a common tool like analytics is that it's got a
ton of powerful ways to analyze the data.

Not sure the goal of your project, but I thought I would mention.

Cheers,
M

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



Re: [PHP] newbie PDO query display question

2013-06-16 Thread dealTek

On Jun 16, 2013, at 3:37 PM, Stephen  wrote:

> Here is a sample from my code:
> 
> $photo_category_list = "";
>$SQL = "SELECT category_id, category_name FROM gallery_category ORDER by 
> category_order";
>try {
>$stmt = $dbh->prepare($SQL);
>$stmt->execute();
>while ( list( $id, $name) = $stmt->fetch(PDO::FETCH_NUM)) {
>$photo_category_list .= "$name\n";
>   }
>}
>catch (PDOException $e){
>echo 'Error getting category name. ' . $e->getMessage();
>}
>return $photo_category_list;
> 
> Try to avoid your code assuming that you will just get one record back 
> (unless you use Limit 1)
> 


Thanks Stephen,

Nice code!

you mentioned ... - to avoid your code assuming that you will just get one 
record back

But what about the case where you are deliberately just showing one record as 
in  FIND ONE PERSON like where ID = 101 ?

do you still need to loop through when you just selected/displayed 1 ITEM? 

Q: How do I display the columns without a loop in this 1 record case...?


these fail - so what will work?

echo $results["First"];
echo $results["First"][0]; ???
echo $results["First"][1]; ???



> While works better than foreach for getting the result rows from PDO








--
Thanks,
Dave - DealTek
deal...@gmail.com
[db-3]



Re: [PHP] newbie PDO query display question

2013-06-16 Thread Ashley Sheridan


dealTek  wrote:

>
>On Jun 16, 2013, at 3:37 PM, Stephen  wrote:
>
>> Here is a sample from my code:
>> 
>> $photo_category_list = "";
>>$SQL = "SELECT category_id, category_name FROM gallery_category
>ORDER by category_order";
>>try {
>>$stmt = $dbh->prepare($SQL);
>>$stmt->execute();
>>while ( list( $id, $name) = $stmt->fetch(PDO::FETCH_NUM)) {
>>$photo_category_list .= "value=$id>$name\n";
>>   }
>>}
>>catch (PDOException $e){
>>echo 'Error getting category name. ' . $e->getMessage();
>>}
>>return $photo_category_list;
>> 
>> Try to avoid your code assuming that you will just get one record
>back (unless you use Limit 1)
>> 
>
>
>Thanks Stephen,
>
>Nice code!
>
>you mentioned ... - to avoid your code assuming that you will just get
>one record back
>
>But what about the case where you are deliberately just showing one
>record as in  FIND ONE PERSON like where ID = 101 ?
>
>do you still need to loop through when you just selected/displayed 1
>ITEM? 
>
>Q: How do I display the columns without a loop in this 1 record
>case...?
>
>
>these fail - so what will work?
>
>echo $results["First"];
>echo $results["First"][0]; ???
>echo $results["First"][1]; ???
>
>
>
>> While works better than foreach for getting the result rows from PDO
>
>
>
>
>
>
>
>
>--
>Thanks,
>Dave - DealTek
>deal...@gmail.com
>[db-3]

Have you tried chaining the the fetch() method on as in:

$pdo_object->query()->fetch();
Thanks,
Ash

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



Re: [PHP] newbie PDO query display question

2013-06-16 Thread dealTek
>> 
>> 
> When you know there is only one row to be returned you can do this:
> 
> $photo_category_list = "";
>   $SQL = "SELECT category_id, category_name FROM gallery_category ORDER by 
> category_order";
>   try {
>   $stmt = $dbh->prepare($SQL);
>   $stmt->execute();
>   list( $id, $name) = $stmt->fetch(PDO::FETCH_NUM));
>  }
>   }
>   catch (PDOException $e){
>   echo 'Error getting category name. ' . $e->getMessage();
>   }
> 
> echo $id;
> echo $name;
> 
> And you could also do
> 
>   list( $result["id"], $result["name"]) = $stmt->fetch(PDO::FETCH_NUM));
> 
> 
> -- 
> Stephen
> 

Thanks very much for your help Stephen - I will try this asap!




--
Thanks,
Dave - DealTek
deal...@gmail.com
[db-3]