PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 11th, 2013, 11:17 AM
BitZoid's Avatar
BitZoid BitZoid is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 98 BitZoid User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 2 h 20 m 38 sec
Reputation Power: 1
PDO n00b

I learned PHP and MYSQL before PDO and MySQLi were the standard for querying mysql. I have never used PDO but am trying to learn. I got a connection, can select a few records, but I'm having trouble selecting this row for some reason. Maybe somebody knows why?

I'm getting error:

Fatal error: Call to a member function setFetchMode() on a non-object in /home/user/domains/example.com/public_html/index.php on line 154

Here is my code:

PHP Code:
 $sthComments $dbh->query("SELECT DATE_FORMAT(news_comments.comment_date,'%D %M %Y') AS commentDate, news_comments.news_key, news_comments.comment_name, news_comments.comment_email, news_comments.comment_comment FROM news_comments WHERE news_comments.news_key='$news_key'");  $sthComments->setFetchMode(PDO::FETCH_ASSOC); 


The last code statement in that line, is line 154. It's odd though because right above this query I used the same syntax to query a seperate table and opened a while PDO fetch_assoc loop and that seems to work.

Can anybody tell me what is going on?
__________________
-- Success achieved from tribulation --

Reply With Quote
  #2  
Old January 11th, 2013, 11:30 AM
Northie's Avatar
Northie Northie is offline
Square Peg in a Round Hole
Click here for more information.
 
Join Date: Oct 2007
Location: North Yorkshire, UK
Posts: 3,421 Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level)Northie User rank is General 44th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 11 h 37 sec
Reputation Power: 3896
without consulting my own code, or the docs, (its late) I would guess that the setFetchMode method is actually part of the $dbh object; not the $sthComments object;

However, I've never used it.

I just do

PHP Code:
 $sthComments $dbh->query($sql);

$rs $sthComments->fetchAll(PDO::FETCH_ASSOC);  

print_r($rs); 
__________________
PHP OOPS! <?php DB::Execute(SQL::makeFrom($_GET))->fetchArray()->FormatWith(Template::getInstance('default'))->printHtml(); ?>

PDO vs mysql_* functions: Find a Migration Guide Here

[ Xeneco - T'interweb Development ] - [ Are you a Help Vampire? ] - [ Read The manual! ] - [ W3 methods - GET, POST, etc ] - [ Web Design Hell ]

Reply With Quote
  #3  
Old January 11th, 2013, 11:42 AM
ManiacDan's Avatar
ManiacDan ManiacDan is offline
Likely to be eaten by a grue.
Dev Shed God 10th Plane (9500 - 9999 posts)
 
Join Date: Oct 2006
Location: Pennsylvania, USA
Posts: 9,811 ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)  Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 2 Months 3 Weeks 19 h 13 m 52 sec
Reputation Power: 6112
The error says that $sthComments is not an object. Either northie's solution is correct or your ->query() function returns FALSE on error.
__________________
HEY! YOU! Read the New User Guide and Forum Rules

"They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

"The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

Reply With Quote
  #4  
Old January 11th, 2013, 11:52 AM
Jacques1's Avatar
Jacques1 Jacques1 is online now
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,882 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 10 h 4 m 38 sec
Reputation Power: 813
Hi,

the error message says it all: $sthComments isn't an object. It's "false" because there was an error in your query.

The code generally has some issues:

Don't insert variables into query strings (it hope it's at least escaped??). PDO has prepared statements, which are the very reason to use PDO. Otherwise you might as well stick to the old mysql_ functions.

Don't prepend the table name to the columns when you only have one table. It's just useless and only clutters your query.

Reply With Quote
  #5  
Old January 11th, 2013, 12:08 PM
BitZoid's Avatar
BitZoid BitZoid is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 98 BitZoid User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 2 h 20 m 38 sec
Reputation Power: 1
Quote:
Originally Posted by Jacques1
Hi,

Don't insert variables into query strings (it hope it's at least escaped??).


I wasn't worried about the variable because it's simply a variable defined by another database query, not user input. Should I still escape the var? I thought that was the point of PDO, it had it's own magic quotes, escape, etc.. prevention built in?

I don't think my query is wrong because I've tried it with just SELECT * FROM tablename and it throws the same error.

Reply With Quote
  #6  
Old January 11th, 2013, 12:09 PM
ManiacDan's Avatar
ManiacDan ManiacDan is offline
Likely to be eaten by a grue.
Dev Shed God 10th Plane (9500 - 9999 posts)
 
Join Date: Oct 2006
Location: Pennsylvania, USA
Posts: 9,811 ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)  Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 2 Months 3 Weeks 19 h 13 m 52 sec
Reputation Power: 6112
That's the point of PDO, yes...assuming you actually bind the variables and don't just build a big string bare like you're doing. Bind them properly and you get the benefits.

If you're using the results of one query in another query, you need to use a JOIN.

Reply With Quote
  #7  
Old January 11th, 2013, 12:41 PM
BitZoid's Avatar
BitZoid BitZoid is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 98 BitZoid User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 2 h 20 m 38 sec
Reputation Power: 1
Quote:
Originally Posted by ManiacDan
That's the point of PDO, yes...assuming you actually bind the variables and don't just build a big string bare like you're doing. Bind them properly and you get the benefits.

If you're using the results of one query in another query, you need to use a JOIN.


Alright great, thanks for all of your insight. I realized I had to start cleaning things up sooner or later, I got it working by combining the query and finally gave up EquiJoin for Inner Join, and got rid of any variables in the query.

This seemed to work:

PHP Code:
/////////////// Query database for news posts ///////////////
    
$sthNews $dbh->query("SELECT n.news_key, 
        n.member_id, 
        DATE_FORMAT(news_date,'%D %M %Y') AS 
        newsDate, 
        news_title, 
        news_category, 
        news_post, 
        m.member_id,
        firstname, 
        lastname, 
        c.news_key, 
        comment_date, 
        comment_name, 
        comment_comment 
        FROM news n
        INNER JOIN members m 
        ON n.member_id = m.member_id
        INNER JOIN news_comments c
        ON c.news_key=n.news_key 
        ORDER BY n.news_date DESC LIMIT 4"
);

    
$sthNews->setFetchMode(PDO::FETCH_ASSOC); 

Last edited by BitZoid : January 11th, 2013 at 12:46 PM. Reason: added code

Reply With Quote
  #8  
Old January 11th, 2013, 01:53 PM
BitZoid's Avatar
BitZoid BitZoid is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 98 BitZoid User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 2 h 20 m 38 sec
Reputation Power: 1
Quick question. If I have more than one query on a page, should I just use the same ( $sth ) for every query. i.e. $sth = $dbh->query and after that query's results are used in my code, redefine $sth = null; and then I can reuse $sth for the next query?

Reply With Quote
  #9  
Old January 11th, 2013, 02:24 PM
Jacques1's Avatar
Jacques1 Jacques1 is online now
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,882 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 10 h 4 m 38 sec
Reputation Power: 813
Use descriptive variable names, which actually tell you the content (like $news_stmt, $member_stmt etc.). Generic or cryptic names massively reduce readability and can easily lead to mistakes. So choose sensible variable names.

Reply With Quote
  #10  
Old January 11th, 2013, 05:27 PM
E-Oreo's Avatar
E-Oreo E-Oreo is offline
Lost in code
Click here for more information.
 
Join Date: Dec 2004
Posts: 7,947 E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)  Folding Points: 945 Folding Title: Novice Folder
Time spent in forums: 2 Months 10 h 55 m 23 sec
Reputation Power: 7053
Quote:
I wasn't worried about the variable because it's simply a variable defined by another database query, not user input.

This wouldn't prevent the variable from having apostrophes in it unless you guarantee when you initially insert the value that it doesn't have them in it.

Unless you have a particular reason for using different fetch modes for different queries, I recommend just setting the default fetch mode when you initialize your connection and then not messing around with it for every statement.
__________________
PHP FAQ
How to program a basic, secure login system using PHP
Connect with me on LinkedIn


Quote:
Originally Posted by Spad
Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around

Reply With Quote
  #11  
Old January 11th, 2013, 05:34 PM
Jacques1's Avatar
Jacques1 Jacques1 is online now
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,882 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 10 h 4 m 38 sec
Reputation Power: 813
What you also might wanna do is set the PDO::ATTR_ERRMODE to PDO::ERRMODE_EXCEPTION in order to get rid of this stupid mixture of exceptions, return values and errors.

You can do this in the fourth argument of the constructor:
PHP Code:
array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION


This will throw an exception for every problem.

Reply With Quote
  #12  
Old January 11th, 2013, 07:06 PM
BitZoid's Avatar
BitZoid BitZoid is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 98 BitZoid User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 2 h 20 m 38 sec
Reputation Power: 1
Quote:
Originally Posted by Jacques1
What you also might wanna do is set the PDO::ATTR_ERRMODE to PDO::ERRMODE_EXCEPTION in order to get rid of this stupid mixture of exceptions, return values and errors.

You can do this in the fourth argument of the constructor:
PHP Code:
array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION


This will throw an exception for every problem.



I did some more reading on PDO. I havn't had time to fully study and understand it yet but I made sure the connection PDO construct in the 4th parameter had an array that included a emulate_prepares = false attribute and error mode attribute set to exception mode and made sure to try and catch all errors or exceptions.

E-oreo - The variable was just another value I inputted into the database that I was referencing from a previous query. So I know it was safe. Even so, I realized I cannot do things sloppily so I went ahead and did things the right way.

I think its my first inner join I used outside of class, I'm use to equijoins that I learned 10 years ago.

I still have tons more to read up on, concerning PDO, OOP, and some other things.

Last edited by BitZoid : January 11th, 2013 at 07:22 PM.

Reply With Quote
  #13  
Old January 11th, 2013, 08:14 PM
Jacques1's Avatar
Jacques1 Jacques1 is online now
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,882 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 10 h 4 m 38 sec
Reputation Power: 813
Quote:
Originally Posted by BitZoid
E-oreo - The variable was just another value I inputted into the database that I was referencing from a previous query. So I know it was safe.


Apart from this concrete case, which you solved:

Do not distinguish between "safe" and "unsafe" values. Believe me, this doesn't work.

The value may be safe for this particular moment, but that could change anytime in the future ("Let me add this feature real quick ..."). And chances are you'll forget to add the escaping then. Switching between escaped and raw values generally is very error-prone. You can easily make mistakes and let an unsafe value slip through, which can completely compromise the security.

You can avoid all this trouble by simply escaping every value, no matter how trivial and safe it is. Or even better: Use intelligent functionalities, which do the escaping for you. For example, many template engines have a default escaping method, so you don't need to call htmlentities() manually for every value.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > PDO n00b

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap