Here is the error: Fatal error: Call to a member function on a non-object in /home/pathto/index.php on line 13 I am trying to pull info from a wordpress database outside of the wp directory. Here is the code What in the world is the problem here?
I can't see the code above but I would guess it's this: $rs = $db->Execute( $sql ); PHP: I think you need to instantiate the class first. I don't know the actual class name, but something like this would need to be above: $db = new mysqlClass; PHP: Otherwise you could also call mysql manually without using the class. <?php $sql = mysql_query("SELECT * FROM wp_posts ORDER BY ID ASC LIMIT 1 WHERE post_status='publish'"); //for or while loop $rs = mysql_fetch_array($sql); //end loop ?> PHP:
$db is the class (or object) and Execute() is a method (function) in that class. If your script is saying that you are trying to call a function on a non-object, then the $db object hasn't been created. So either 1. The php file containing the class hasn't been called. or 2. You haven't created the $db object Have you ever had this script working? For $db to be an object, somewhere in your script you would need $db = new someClass() before you could start calling methods (aka functions) of that class.
bro - you really sound like you know what you are talking about. However, I don't. No, I am writing this script to pull some info from a wordpress database outside of WP. I recently tried this from php.net <?php // Connecting, selecting database $link = mysql_connect('mysql_host', 'mysql_user', 'mysql_password') or die('Could not connect: ' . mysql_error()); echo 'Connected successfully'; mysql_select_db('my_database') or die('Could not select database'); // Performing SQL query $query = 'SELECT * FROM my_table'; $result = mysql_query($query) or die('Query failed: ' . mysql_error()); // Printing results in HTML echo "<table>\n"; while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { echo "\t<tr>\n"; foreach ($line as $col_value) { echo "\t\t<td>$col_value</td>\n"; } echo "\t</tr>\n"; } echo "</table>\n"; // Free resultset mysql_free_result($result); // Closing connection mysql_close($link); ?> PHP: but it basically started printing my entire table in a single row.
Here is what is working but it seems to be a whole lot of bloat! <?php $dbuser = 'blah_dbase'; $dbpass = 'password'; $dbhost = 'localhost'; $conn = mysql_connect($dbhost, $dbuser, $dbpass) ; $dbname = 'blah_user'; mysql_select_db($dbname)or die ('Error connecting to WP MySql'); $query = "SELECT * FROM wp_posts ORDER BY id DESC LIMIT 1"; $result = mysql_query($query); if ($result) { while ($array= mysql_fetch_assoc($result)) { print "<li><h4>$array[post_title]</h4></li>\n"; } } else { print "<li>No results.</li>"; } ?> PHP:
This will shorten it up a bit: <?php $conn = mysql_connect('localhost', 'blah_dbase', 'password') ; $dbname = 'blah_user'; mysql_select_db($dbname)or die('Error connecting to WP MySql'); if ($result = mysql_query("SELECT * FROM wp_posts ORDER BY id DESC LIMIT 1")){ while ($array= mysql_fetch_assoc($result)) { print "<li><h4>$array[post_title]</h4></li>"; } } else { print "<li>No results.</li>"; } ?> PHP: Otherwise, what you had would work fine, and the amount of code isn't anything to worry about. Also, normally you don't need to manually put in linebreaks (\n).
That actually isn't that much code. If you were actually including a database class you would have a lot more code than that. I was confused for the longest time about objects, classes, etc. since I tought myself PHP and never took a course. I understand it now, one day it all just clicked. All a class is, is a group of functions. Functions are called methods when they are grouped in a class, thats the first confusing part. When you include a class, or group of functions into your script, it is called 'creating an object'. So an object is just an instance of a class. So if you had group of functions grouped into a class in file class.php class MyClass { function1() { $a = 2; return $a;} function2() { $a = 4; return $a;} } To access those functions first you would have to include the php file include("class.php"); and then create an object from the class $whatever = new MyClass(); Then you could access those functions $newvar = $whatever->function1(); $another = $whatever->function2(); $newvar would equal 2 and $another would equal 4. You were trying to access a function/method in an object/class where no class was ever included in your file and no object was ever created. It looks like you were copying code from another part of the script. All you probably needed to do to make that work was include the database class, whereever it was. Something like, include("database.php"); and then create the object with something like $db = new dbClass(); I have a couple basic database classes that I can show you how to use to query your database if your script gets any more complicated than that. I haven't used wordpress yet, but I will be implementing it for a site in the next week or two so I'll be able to help more specifically with wordpress related issues soon.