14 Replies - 2804 Views - Last Post: 11 August 2013 - 04:08 AM Rate Topic: -----

#1 codespook  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 135
  • Joined: 31-October 12

Fatal error call to member function query() on a non object

Posted 09 August 2013 - 10:53 PM

I've tried several things but still getting this error. $conn is the variable from connect.php, which contains the connection to the database. Its in an include statement in page.php. Function.php is also in an include statement in page.php.


<?php




//show comments



function showcomments($conn) { 
 $show = $conn->query("SELECT username, subject, contact, comment, date FROM comments WHERE page = ? ORDER BY time");
$show->execute(array($_SERVER['REQUEST_URL']));
$numRows = $show->rowCount();
}


?>




Even putting global $conn above the function comes with the error too.

Is This A Good Question/Topic? 0
  • +

Replies To: Fatal error call to member function query() on a non object

#2 exiles.prx  Icon User is offline

  • D.I.C Head

Reputation: 65
  • View blog
  • Posts: 239
  • Joined: 22-November 10

Re: Fatal error call to member function query() on a non object

Posted 09 August 2013 - 11:05 PM

Opps, missed your comment on the bottom. You need to add the global call within your function.

This post has been edited by exiles.prx: 09 August 2013 - 11:08 PM

Was This Post Helpful? 0
  • +
  • -

#3 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3390
  • View blog
  • Posts: 9,586
  • Joined: 08-June 10

Re: Fatal error call to member function query() on a non object

Posted 09 August 2013 - 11:26 PM

@exiles: nope, shouldn’t matter as long as the connection is passed as parameter.

though there are other problems with the query:
- you fetch 5 fields and only use the row count
- you do not return anything from your function
- ->query() executes immediately (and hence gives you no result), what you need here is ->prepare()
- if page is unique, your row count is at most 1

This post has been edited by Dormilich: 09 August 2013 - 11:27 PM

Was This Post Helpful? 1
  • +
  • -

#4 codespook  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 135
  • Joined: 31-October 12

Re: Fatal error call to member function query() on a non object

Posted 09 August 2013 - 11:34 PM

Prepare then execute?
Page will be unique as the comments displayed will will depend on what page the user is on.
Was This Post Helpful? 0
  • +
  • -

#5 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3390
  • View blog
  • Posts: 9,586
  • Joined: 08-June 10

Re: Fatal error call to member function query() on a non object

Posted 09 August 2013 - 11:37 PM

that’s how prepared statements work.

your function (as shown) doesn’t display anything.
Was This Post Helpful? 0
  • +
  • -

#6 codespook  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 135
  • Joined: 31-October 12

Re: Fatal error call to member function query() on a non object

Posted 10 August 2013 - 04:04 AM

Same error but this time, it's on prepare since I took out query:)

function showcomments($conn) {
$show = $conn->prepare("SELECT username, subject, contact, comment, date FROM comments WHERE page = ? ORDER BY time");
$show->execute(array($_SERVER['REQUEST_URL']));	
$numRows = $show->rowCount();
}



Fatal error: Call to a member function prepare() on a non-object in line 4.


I re-referenced the intro to PDO tutorial here. I did notice that there is this line
$pdo = new PDO($dsn, $login, $password, $opt);



Should I put this above my function or is passing $conn as a parameter to the function enough? (only I should substitute my variables). That above line creates a new instance, but is that the same thing as passing $conn as a parameter? (Now I really show my noobiness here:) :)
Thanks
Was This Post Helpful? 0
  • +
  • -

#7 Sho Ke  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 107
  • View blog
  • Posts: 246
  • Joined: 13-October 11

Re: Fatal error call to member function query() on a non object

Posted 10 August 2013 - 04:24 AM

add var_dump($show) before and after calling $show->execute(). what does each one say? The query is most likely not finding any results, causing $show to be a boolean(false).
Was This Post Helpful? 0
  • +
  • -

#8 JackOfAllTrades  Icon User is online

  • Saucy!
  • member icon

Reputation: 5951
  • View blog
  • Posts: 23,214
  • Joined: 23-August 08

Re: Fatal error call to member function query() on a non object

Posted 10 August 2013 - 04:57 AM

From the PHP manual for PDO::prepare():

Quote

If the database server successfully prepares the statement, PDO::prepare() returns a PDOStatement object. If the database server cannot successfully prepare the statement, PDO::prepare() returns FALSE or emits PDOException (depending on error handling).


From the PHP manual for PDOStatement::execute():

Quote

Returns TRUE on success or FALSE on failure.


You totally ignore these return values (or the exception if so configured). If you want to be an actual developer, you need to learn to check the values returned from functions and act appropriately.
Was This Post Helpful? 2
  • +
  • -

#9 exiles.prx  Icon User is offline

  • D.I.C Head

Reputation: 65
  • View blog
  • Posts: 239
  • Joined: 22-November 10

Re: Fatal error call to member function query() on a non object

Posted 10 August 2013 - 09:22 AM

View PostDormilich, on 09 August 2013 - 11:26 PM, said:

@exiles: nope, shouldn’t matter as long as the connection is passed as parameter.


@Dormilich Your right, I don't know how I overlooked the argument passing of $conn.


@codespook Check the ouput of $conn in your function to make sure the variable is referencing the proper connection.
Was This Post Helpful? 0
  • +
  • -

#10 codespook  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 135
  • Joined: 31-October 12

Re: Fatal error call to member function query() on a non object

Posted 10 August 2013 - 03:21 PM

output from var_dump($conn);

object(PDO)#1 (0) { }


If I understand correctly, it is not even recognizing my variable, so it is returning false:(
Was This Post Helpful? 0
  • +
  • -

#11 exiles.prx  Icon User is offline

  • D.I.C Head

Reputation: 65
  • View blog
  • Posts: 239
  • Joined: 22-November 10

Re: Fatal error call to member function query() on a non object

Posted 10 August 2013 - 06:04 PM

If I remember correctly, a var_dump() on a new instanace of PDO will only show its type, not any of its details. Are you calling var_dump() within your showcomments function? b/c your error message (Fatal error: Call to a member function prepare() on a non-object in line 4. ) tells me otherwise if in fact the prepare statement from your code above is on line 4. Please post the code you are using.

This post has been edited by exiles.prx: 10 August 2013 - 06:06 PM

Was This Post Helpful? 0
  • +
  • -

#12 codespook  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 135
  • Joined: 31-October 12

Re: Fatal error call to member function query() on a non object

Posted 11 August 2013 - 12:52 AM

Wow. I think it was a combination of things, and I can't even tell you what now I've changed things so much. Well, I did learn a lot though from this thread so I thank everyone for all their help! But it now works.....at least for now. There is no error, which is what I wanted as there is no info in the database yet. Next I work on the insert statement:) Here's the working code:)

<?php


function showcomments($conn) {

$show = $conn->prepare("SELECT username, subject, contact, comment, date FROM comments WHERE page = ? ORDER BY time");
	
$show->execute(array($_SERVER['REQUEST_URL'])); 

$numRows = $show->rowCount();
}


?>



Was This Post Helpful? 0
  • +
  • -

#13 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3390
  • View blog
  • Posts: 9,586
  • Joined: 08-June 10

Re: Fatal error call to member function query() on a non object

Posted 11 August 2013 - 03:24 AM

View Postcodespook, on 11 August 2013 - 09:52 AM, said:

Here's the working code:)

except for the small fact that it does nothing *gg*
Was This Post Helpful? 0
  • +
  • -

#14 codespook  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 135
  • Joined: 31-October 12

Re: Fatal error call to member function query() on a non object

Posted 11 August 2013 - 03:42 AM

yet:-)
Was This Post Helpful? 0
  • +
  • -

#15 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3390
  • View blog
  • Posts: 9,586
  • Joined: 08-June 10

Re: Fatal error call to member function query() on a non object

Posted 11 August 2013 - 04:08 AM

I can only comment about the code I see, not what I have yet to see.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1