Before I start let me define what I mean by fail. I mean your website won’t make money. Most amateur webmasters get into the business with the aim of making money online. Some of the methods include selling a product, advertising or membership. 90% of online money is made with these methods. We can gain valuable insight into making more money by examining the reasons webmasters fail.
Firstly, whats your money making method? Lets deal with advertising first.
If you are running adverts on your site, say from Google Adsense, Bidvertiser, ValueClick or direct ad sales you are dependent on visitors clicking on those ads or impressions. Either way you are totally reliant on visitors. Which raises the question ‘Why do web users visit your site?’ The key to this is content. I’ve said it before and I’ll say it again - Content is King. If you are providing unique and fresh content users will come. You won’t have to go looking for them - they’ll find you. Word of mouth is the most valuable marketing you can undertake. If you’re content is freely available elsewhere you are immediately at a disadvantage. You are scrambling with thousands of other webmasters for those web users. Good quality, well presented content will always bring users with the minimal of effort on your part. Content that is available elsewhere requires more marketing effort, more Search engine optimization and better presentation.
Lets take the case of an arcade website as an example. Games you host on your arcade website are in all likihood available on thousands of other websites. So how do you gain a competitive advantage over your competitors so that users visit your site? You must offer something unique to your users. Either a cool design, better features, a community, forum, up to date news or the newest games. If you fail to do something that no-one else is doing you are already facing an uphill struggle. That in effect means you will have to invest more effort in your marketing and search engine optimization.
Selling a product? Again unless you’re product is unique you are creating extra unneccessary work for yourself. You will end up slashing prices as you attempt to compete with every other webmaster selling that same product. If you charge a higher price, then you are again returning to marketing effort to gain a competitive advantage and have web users visit your site first.
Selling membership? Again, you must offer something unique. That may be the feel and community spirit of the site, access to unique data or a group of people.
There are very very few websites that prosper that are not unique. Think of any successful site you know. What do they do that you don’t?
Can you purchase a script and still have a unique website or product? Absolutely. You’re competitive advantage may come in the form of constantly updating the site with new articles, games, pictures or videos. Perhaps you have a source for fresh pictures that no-one else has. Maybe you create your own videos. Maybe you have modified the template layout to create a unique feel to the site. There is a wonderful effect when you reach critical mass - that is the users you have bring more users. It snowballs. But to reach that stage I cannot emphasis enough how important it is to offer in your website content that is unique, fresh and well presented. Without it you are simply racing with every other webmaster to reach a user first. Prepare to spend hours optimizing your site, building links, swapping links, placing adverts and running competitions.
Why create so much work for yourself?
Tags: Traffic Building
In creating an application using Berkeley DB JE (Java Edition) this error started cropping up in the logs:
java.lang.IllegalArgumentException: je.env.isReadOnly is set to false in the config parameter which is incompatible with the value of true in the underlying environment
at com.sleepycat.je.EnvironmentMutableConfig.checkImmutablePropsForEquality(EnvironmentMutableConfig.java:218)
at com.sleepycat.je.DbInternal.checkImmutablePropsForEquality(DbInternal.java:192)
at com.sleepycat.je.dbi.EnvironmentImpl.checkImmutablePropsForEquality(EnvironmentImpl.java:1534)
at com.sleepycat.je.Environment.(Environment.java:128)
My application had a number of threads running. Berkeley DB is ideally suited to a single process, rather than multiple processes running simultanously. What this means in effect is that you can have multiple read process but one, and one only write process. No matter what synchronization I tried, I kept getting the error, most probably because one process still had a handle on the DB. I tried setting env.transactional to true, then to false and still no joy. THough I haven’t exactly nailed down the root cause of the error (Which at this stage you’re mutturing something under your breath about me…) I’ve come close enough to know that when I’m opening multiple environments (JE DB will check whether an environment is already open) the environment configs are clashing leading to the above error.
TO put it simple, JE DB will check whether you already have an environment open when you call another environment on the same location. IF you have it will check whether the configs are the same - If they are not then the above error is thrown. The odds are that if you remove all process except one the error will disappear which should confirm that you’re processes are indeed clashing when accessing/writing to the database.
The only way I could get around it was to have each process write to their own database. When all processes were completed, or at a later stage, I merge all these databases into the main database. Its not a process that is hard to code, as all databases have the same structure as the main database. The only thing I had to check for was duplicate records and how to handle them - Whether to merge the data or over write the older record altogether.
Of course if you are seeking instant access to the fresh data you can do two things.
1. Solve the above problem(and if you do be sure to let me know so I can post it here for others!! - You’ll also get you’re name in lights here on this very page :D)
2. Create and automatic update process. Say by counting the number of records that need to be updated and updating every 100 records.
Sorry I can’t be of more help here. This one really got me flummoxed and all coffee’d out!
Tags: Berkeley DB JE
With Microsofts withdrawal of its bid for Yahoo, it leaves Google the undisputed master of search. Even better for Google, Yahoo is so opposed to a Microsoft buy-out its now expirementing with running Google ads on its network - over and above its own ads. Reports are suggesting Yahoo sees a click-through rate of nearly 33% better with Google ads rather than its own.
Microsoft’s own bid for Yahoo was essentially admitting that that its own search engine was failing. It’s share of search queries has been falling steadily in recent years and currently stands at 10%. Assuming that Chief Executive of Microsoft, Steve Ballmer, was genuine in withdrawing the bid and that its not some tactic to increase pressure on the Yahoo board to accept a bid at a later date, then where does Microsoft go from here? After all, if Yahoo gets a new board of directors, Ballmer may be enticed to bid again for the company.
The dominance of Google at this time is unrivalled. Even Microsoft who has defended its monopoly in operating systems in the face of ever increasing fines, is crying foul - an irony that may seem rich to some.
But there are very real concerns for webmaster and end-users if Google goes unchallenged in the Search Engine market. Without competition in the market it means Google can increase its cost for keywords - essentially charging what it likes. And that is bad for us, particularly if you don’t have very deep pockets. No monopoly I can think of has ever been good for consumers. And Google will be no different despite all the glossy marketing.
Microsoft ended up bidding an astronomical $45 Billion for Yahoo. Its difficult to see how Yahoo can increase its networth over and above that at the moment. Following the withdrawal of the bid, its share price has plummeted. Some investors are now launching lawsuits alledging the board of Yahoo failed to act in the best interests of shareholders. But whatever the fallout from the offer, could Microsoft spend $45 Billion developing its own search engine technology?
Microsoft has a woeful history of online development. It struck it big in operating systems, Hotmail was a revolution when it first came out but since then it has been out-marketed, out-developed and out-revenued by internet competitors, chief amongst them Google. Why? Microsoft is wedded to the idea of charging for its software. Google delivers its software for free and instead makes money from advertising. Yahoo does the same. Even Microsoft’s traditional business of Office Applications is now under threat from free versions. There is Google docs (soon to be available offline) and Open Office can now read and create fully compatable Microsoft documents. Over the next few years this revenue stream will start to get smaller for Microsoft as the alternatives gain acceptance. Which would explain why Microsoft is now deleloping its Albany project to sell its office documents, anti virus and live search as a package deal. It also explains why its desperate to gain a genuine foothold in the the advertising market vis a vis Search Engine Technology.
But its up against an internet giant in Google that has its very birth in search engine technology. Thats why Yahoo was such an attractive proposition for Microsoft. Overnight its share of search queries would have gone from 10% to 33% and it would have gained significant internet presence.
Now both Microsoft and Yahoo face difficult choices. For Yahoo, it can pair up with Google and revert to its core of content creation rather than search engine developement. However to do so runs the risk of it following in AOL’s path - a long slow painful journey to an internet death. For Microsoft its only real option is to develop a better search engine than Google, or make another bid for Yahoo at a later date. I don’t believe Microsoft has the development capacity to produce a better search engine. If they had they would have done so. So I think we’ll see a renewed bid for Yahoo maybe in a few months.
The only person laughing all the way to the bank at the moment is Google.
Tags: Search Engine Development · Internet
Over the last three weeks I’ve been immersed in PHP, updating sites and in particular Thugvillage.com. Its been well worth it. Already I’m seeing the benefits of the new sections including a Digg style area remodelling the index pages for visitors and members and introducting a whole new content section that will be eventually turned into a magazine section. I’m enjoying the site now and memberships are growing daily. I love writing so with the magazine section I get to write to my hearts content about anything that crosses my mind.
Its been a tough 3 weeks and I’ve been consious that while I was coding in PHP I was ignoring my Java Search Engine. I went at it again tonight and I’m still trying to catch up to where I was. The number of java files I have is now running over 100 and most are running perfectly. I have my distributed file system running, I can inject urls into the database and successfully crawl and parse them. The actual pages are all wrapped in objects so that I write once and can read often. The great thing is most of the data about a URL is contained withing one object, so there is no re-writing the object until the URL is crawled again. Out of the parsed Links I am sucessfully updating the main and distributed databases, and I must now account for bad URLS found by the slaves in their crawling.
To do this I don’t immediately delete a URL if it fails to load or is missing. A tally is kept of the number of attempts, and when this tally reaches three (that is three seperate crawl attempts - not all on the one day) then the URL is deleted from all databases.
After this I can implement my URL scoring, I have the majority of the code written, only to update it so that it works in a ‘many slaves-master’ relationship.
Coming back to the original point about the absense of coding in JAVA for 3 weeks I found it difficult to get back into the swing of things tonight, particularly given that the amount of code is large and is all relatively new. Its not something I’ve been tinkering with for years. I begun learning JAVA just under one year ago and the learning curve has been steep to say the least.
I’m aiming to be able to crawl and fully assimilate 300,000 URLS in one day. If I can accomplish that on the very basic machines I have, then I’l be quietly confident about adding additional capacity to the system. Previously when I had only one machine doing the fetching and processing (there was no master-slave set-up) I could assimilate over 20,000 URL’s in the database after a days work.
Fortunately I’ve bought more powerful machines lately, and when I’m ready to run a full scale test with my distributed system, I’l have 3 dedicated slaves and 1 master server.
My biggest problem at the moment is integrating data into the main database on the server. Or rather the records of the data. The master only holds records, rather than the actual URL data and links etc. So with threads running everywhere I’m close to completing a checking operation that only one write process takes place at any one time.
That will be my main focus for tonight and tomorrow. Once that is completed it will be an hours job to complete the Bad URL’s code.
Tags: JAVA · Search Engine Development
People normally go looking for CURL when they get an error message like below:
Call to undefined function: curl_init().
99.99999% of the time this means the PHP has no idea what curl_init() is or what it does. So stop staring at your code for the moment, wondering what you’ve done wrong and read on.
First off, these instructions are for linux, specifically centos/fedora. You can probably apply them to just about any system, but you’ll need to tweak the installation of CURL / PHP.
Okay, ssh into your server and check first that you actually have CURL installed. simple type
curl
at the command promt. if you get
curl: try: curl –help
then you have curl installed and its simply a matter of getting PHP to connect with it. Which means you can skip the next step.
FIRST STEP
For those that get error messages, you need to install cURL. Go to http://curl.hazz.se and download the most recent version for your operating system. If the date is anywhere around the 06th May 2008 when you read this you can try (make a directory and navigate there first if you want to keep things in order!):
wget http://curl.haxx.se/download/curl-7.18.1.tar.gz
Navigate to where you downloaded it to and untar the file:
tar -xzf curl-7.18.1.tar.gz
You will now have a directory curl-7.18.1. cd into that and then lets compile
cd curl-7.18.1
./configure
make
make install
if you now check
/usr/local
you should see curl there. Or type
curl
at the command prompt as above and the help message should show up.
NOTE If you serve secure sites there are additional steps to take. Sorry, but I’ve never installed CURL to work with secure sites so I’m not going to pretend I know about it.
SECOND STEP
Now that we got CURL installed we need to tell PHP all about it. If you already have PHP up and running the bad news you need to re-install it. The good news is that its not difficult!
First off look up your phpinfo file. If you don’t know how to do this save the following in a file and name it phpinfo.php
NOTE: here and below I’ve placed ‘||’ before the php tags so it this page will display properly. You must remove these!
||<||? phpinfo(); ||?||>
Upload the file to your webroot, say for example:
http://www.hirednetwork.com/phpinfo.php
The second/third piece of information on that page is the configure command. You’ll need this so keep this page open for the moment.
Okay, now lets download the latest version of PHP. go to http://php.net and get the latest version. Again, create a directory to download it to if you wish. Now navigate to that directory. Again we need to unpack the contents (I’m assuming you downloaded the tar.gz file, and NOT the tar.bz2 file):
tar xvfz yourPHPfile.tar.gz
again, you’ll find a new folder created in your current location so cd into it -
cd php-X.X.X
Now we must run a ./configure command. From the phpinfo.php page we looked up earlier, copy all of the configure command onto your new one. For instance the phpinfo page will show something like ‘–with-blah-blah-blah’ ‘ –with-myFile=/usr/lib’ and so on. You need to copy them all to your new ./configure command. ALL OF THEM! And then at the end we need to add support or bindings for our new CURL option. So add on:
'--with-curl=/usr/local'
Then we:
make
and finally
make install
At this stage you need to restart you’re webserver. If you are running apache in the default location you can try the following:
/etc/rc.d/init.d/httpd restart
To check if its correctly installed reload your phpinfo.php page, and search for ‘curl’. There should be a new section displaying something along the lines of:
Curl : enabled
You can also try the following to test if curl_init() is working. Again upload a new webpage to your server named test.php or anything you want with the following:
(Remove the ||)
||<||?php
$ch = curl_init("http://www.hirednetwork.com/");
$fp = fopen("hirednetwork.txt",
"w");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch); curl_close($ch); fclose($fp);
||?||>
NOTE: YOU WILL NEED TO CREATE THE ‘hirednetwork.txt’ file or an error message will be thrown!!
If you found curl in your phpinfo.php page, then in all likihood, it is installed correctly. If you have any errors double check you’re code. As a last resort, you can always post a comment here and I’ll do my best
Tags: PHP
Why can’t coders insert proper comments into their code?
I’m so sick of crawling through scripts and spending half an hour going through each page just trying to figure out what the hell is going on. If I had my name on a script I’d be embarrassed to release it if I didn’t have it properly documented. And comments don’t even take that long to insert. What is ideal is to have at the top of each page
/*
code comments!
Process the input from submit.html
$variable1 = //this is variable one
function getIt($variable x) - returns the location of variable x
*/
Thats it! It takes two minutes to write when you’re coding the document and saves everyone else a whole bunch of frustrating time. It also saves you being cursed by millions as they parse through your code.
Serious coders know that any work they do for large organisations have to be properly documented and commented. No organisation is going to pay another coder to come in just to decipher what the previous guy was doing!
So really, if you think you’re anywhere near a good coder, have a look at what you write. If its not commented and documented by you, then you’re just another script kiddie.
Tags: Scripts
I’ve spent the whole day re-organising and editing the phpFox system, it was confusing at the start but is much easier now. I’m using phpFOX 1.5 and its only this that the following applies to. I have no idea how phpFOx 1.6 is laid out, I haven’t even downloaded the script yet!
As I’ve said before its a neat system - provided you can get to grips with the layout and where everything goes.
Particularly confusing is the notion of splitting up site sections, their modules and then again the blocks of that module. It can become a game of ‘chasing the source’ where you are opening one file only to find another file is being included and so you go chase this one down.
So let me give a brief overview of what I’ve learnt, and hopefully it’ll help someone else avoid having to spend a few hours wondering why object orientated code was ever introduced to PHP!
First off all our ‘master’ PHP files reside in
SITE/PUBLIC/
. We can think of this as our master section. I’m going to use the example of the gallery here to explain things but most of the sections are laid out the same way. So when a user clicks on Gallery in the menu bar, everything gets started in this section.
If we check the gallery directory we find a few files. Main.php is the main file naturally while _pre is a file that is preloaded. You don’t have to worry too much about this unless you want to manually block users etc. from accessing that section,rather than carrying out the task in the admin panel.
You’ll know that there is a module in the index page that gets the latest images from the gallery. Now if we were looking in SITE/PUBLIC/GALLERY/ for where all this happens we’d be old and grey before we realised it wasn’t there. Such modules can be thought of as the ‘building blocks’ of a section. So when we need to do something special with the gallery we put all these blocks in INCLUDES/MODULES/GALLERY/CLASSES. So in here you’ll find the actual PHP code for showing the latest images. All files there take the format of phpFox_ComponentSOMENAME.class.php. For instance the recent images from the gallery on the index page have the file name phpFox_ComponentRecentImages.class.php
Just like the overall sections, these php files have their templates stored in a seperate section,mainly so that SMARTY, the templating engine used in phpFOX can join the two together - the php file and the HTML file.
The templates for the gallery section are stored in design/templates/default/_modules/Gallery. The template or HTML file for the above mentioned phpFox_ComponentRecentImages.class.php is lying there are simply called RecentImages.html.
So now we have our processing file and our templating file, which we can edit as we wish.
You’ll notice that checking any phpFox_ComponentSOMEFILE.class.php that two things happen. First it assigns the result of any database queries to the SMARTY templating engine with SMARTY’s assigning code. For example:
$oTpl->assign(array(
'aItems' => $aItems, ... ... ... ... ));
It then calls the template file that we found previously with
$oTpl->fetch('RecentImages.html');
However the only different thing here is that as we are essentially in a class (or very basically a function) we need to return the template fetching to the main php file that called this (like Index.php) so we add a return call. So the template fetch now looks like:
return $oTpl->fetch('RecentImages.html');
Tags: Scripts · PHP
Recently I’ve done some coding on phpBB3.
I was somewhat familiar with phpBB 2 series, so I was excited about this piece of work. I spent the best part of two days tinkering with it and overall it was a very easy piece of code to work with.
As always with phpBB, there is much documentation about, both official and unofficial. Whatever you’re looking to do, somebody else has done it, or attempted to do it. This makes phpBB3 easy to get started with quickly. As always with phpBB, the installation couldn’t have been easier. it was completely web based and painless, on a par with vBulletin.
Forum setup and management is much easier than in the past, with a new admin layout that even I could understand! Setting permissions on the various forums has also been made far easier. Installing mods is of course a process of editing files, but thanks to the new templating system in phpBB3 (You can edit templates withing the admin panel), the installation of mods is accomplished quickly in most cases. Of course where phpBB3 falls down in my opinion is the relative lack of mods compared to vBulletin.
VBulletin has moved into the arena of social networking and in the future this may possibly be the way forums will go. As we move out of the text based internet, everything will be tied up in more audio and visual media with strong networking forums. I would love to see phpBB take a step in this direction, and if anyone ever cares to code a social network add-on for phpBB, i’m sure it would sell extremely well. I’d do it but I just don’t have the time at the moment
There are of course new styles coming out of the woodwork for phpBB3, the majority of them free and easily downloadable. Its easy to modify, both the CSS and templates, so you should be able to get a unique look relatively quickly.
There are of course a number of areas where phpBB3 does fall down, and taken as a package as a whole, these are minor issues. For instance, on the index page it should come naturally to be able to see the last topic posted on in the subforums. Instead we get the default ‘last post by xxxUser’. This is one modifcation I’m sure I’ll be doing shortly.
In addition as always SPAM protection is something you have to implement immediately with phpBB and phpBB3 is no exception. Why a simple admin set question can’t be implemented for registration is beyond me. Luckily one of the first modifications out fixed this, and after installing it the owner hasn’t reported any problems with SPAM bots so far.
The only other gripe I have with phpBB3 is the absense of a plug-in system. Being a free forum software, it is widely used and bridged with other software, inlcuding word-press, joomla,drupal to name but a few. A plugin system would make bridging between phpBB3 and other software a much easier and pleasant task.
Taken as a whole package, and a free package at that, I’d say phpBB3 would give vBulletin a run for its money. If you’re looking for a forum only package that does what its meant to do I highly recommend it. Its come a long way from the early days. vBulletin is of course more expandable, but for forum only purposes it wins my vote.
Tags: Scripts
If you’re not running your own DNS server, it means your records are being handled somewhere. After all, not everyone knows your IP address, and I’m afraid the art of magic is sadly missing in resolving your domain name to your IP. Hence its all done via DNS records and for email through MX records.
These instructions relate to GoDaddy, but can be used for any good webhost where you have access to the DNS records.
DNS stands for Domain Name Server, and it tells the world where your website is hosted at. I won’t get too much into the whole background, as I want to keep this as non-technical as possible. Suffice to say you can think of DNS records as a traffic map to your website.
So lets get started.
The A-Name record is your primary record. It is essentially ‘yoursite.com’. Note that this is not the same as ‘www.yourdomain.com’ or ‘mail.yourDomain.com’.
‘www.yourdomain.com’ and all the other prefixes that you can put before ‘yourdomain.com’ (eg mail,smtp,ftp,email,pop…) are what as known as CNAME records. They are not the root domain in the way that the A-Name record is.
So lets use this site as an example. We’ll presume the following is true:
Domain: Hirednetwork.com
IP address of server: 123.4.5.6
Email Server: 987.6.5.4
Registered At: Godaddy
Domain Name Servers: We don’t know!
We have all the information here to get your records set up. You should at least know the above information before proceeding any further with the following instructions.
First of all, with Godaddy and most other hosts, transfer the Domain Name Servers to them. This will either be an option on a menu like ‘transfer DNS to us’ or in the case of Godaddy do the following:
Domains->My Domains -> YourSite.com ->Nameservers->Default NameServers->Default Hosting Nameservers
The Nameserver fields should be automatically filled out.
Note that any changes you make to NameServers and DNS records CAN take up to 24 hours before they get ‘read’ across the world. So if you can’t access your website immediately fret not. Most Internet providers and webhosts are very good and update master records nearly immediately so in most cases you shouldn’t be waiting anything near 24 hours. Most of my records update instantly.
If your webhost does not have an option to tranfer the DNS back to them, just drop them an email asking what nameservers to use. Any reasonably large host will let you use their nameservers for hosting your DNS records.
After a few minutes check back and follow the above directions until you arrive at ‘yourSite.com’. You should now see an option for ‘Total DNS control and MX records’. Do not be afraid! Click the dam link!
First things first. Lets change the root record (the A record). Some webhosts have an ‘@’ in the Host field for this, and we can think of it as just a symbol indicating the root record.
In the IP field for the A-Name record enter your Server IP where your website is hosted.
In our example above we would enter ‘123.4.5.6′.
Next we must take care of the ‘www.yourDomain.com’ name. As mentioned this is a CNAME record. Fill out the appropiate field with as follows:
Host - > www
Points To - > @
This means the www.yourDomain.com is pointing to ‘yourDomain.com’. For some strange reason I’ve come across hosts that won’t allow the @ symbol here, and I’ve resorted to either entering the IP address if thats allowed and failing that the ‘yourDomain.com’.
Now that we have our server address set up, you should (after waiting for the changes to take effect!) be able to access you website once again. Do clear your browser cache, and I always check by using a web proxy to access my site. I’m paranoid!
Before we get to MX records we will set up one more CNAME record to handle our mail calls. I’m presuming of course that you have an email server humming away somewhere and you know the IP address. It could be hosted on the same machine as your webserver. In our example above we have a different IP for our email server. Our new CNAME record we’ll call ‘mail.yourDomain.com’. If you’re email server IP is the same as your web server IP you can insert the @ symbol or simply ‘yourdomain.com’. If you have a different IP for email (as we do)enter it here.
So in our example above, this CNAME record would read:
MX record:
Host: mail.gamingmight.com
CNAME: CNAME
IP/HOST: 987.6.5.4
Now we get to the MX records. This specify where email should be routed - through what server etc. Having followed the step above and set up our CNAME record ‘mail.yourDomain.com’ all we need to is complete an MX record. The field Priority in the MX record is just that. Priority is just a means of clasifying the importance of various MX records - the lower the number the more priority it gets when being read. A tradition is to enter a ‘10′ in this field as the lowest, though more and more people now enter ‘0′.
So our MX record we’ll complete as:
Host: YourDomain.com
RECORD MX
DEST mail.YourDomain.com
Run a test on your email at MX ToolBox. You should be able to send and receive mail now (Presuming you have your outlook express or email client configure correctly!).
Tags: Servers
This exception is usually thrown when trying to connect to a Database. There are three things that possibly cause it:
1. No connection to the DB - unlikely as an error of ‘unable to connect to Database…’ will most be thrown first by any well laid out code.
2. Your looking for something that doesn’t exist - also unlikely as an sql error will be thrown along the lines of You have an error in your sql statement. Please check the syntax to use near…
3. The most likely cause of the call to a member function Execute() on a non object in… is an object that hasn’t been properly incorporated into your code. The PHP script recognises the execute call, but doesn’t know what object you’re referring to.
Lets take an example to make this a bit more clear.
Using an ADOdb connection (any connection method can throw this error - just read on and it should be clear where your error is!) we establish our connection in say config.php:
//DB variables
$server="localhost";
$user="Your User";
$pwd="YourPassword";
$db="YourDataBase";
//files needed for adodb - these willo be different in your script
include('adodb/adodb.inc.php');
include("adodb/adodb-exceptions.inc.php");
$DB = NewADOConnection('mysql');
$DB->PConnect($server, $user, $pwd, $db);
Now in your main PHP files you import this and use $DB to connect to your Database. You do this with the line:
$result = $DB->Execute("select * from Users");
And it works perfectly.
Where the error can be thrown is when you try using the call to $DB object within a function. Say you have a function to get the users address in a file called functions.php:
//functions.php file
function getAddress($id)
{
$result = $DB->Execute("select Address from Users where id=$id");
return $result;
}
You will now get the famous - Call to a member function Execute() on a non object in …. The problem lies in that variables within functions are local variables. So the $DB call in the getAddress functioin is a local variable/object. Unless you declare what $DB is, within the function then you will get an error message. To make the $DB variable the same as the object you use outside the functioni you will have to declare it Global - WITHIN THE FUNCTION!
So to get your function call to operate correctly your code will now look more like this:
//functions.php file
function getAddress($id)
{
global $DB;
$result = $DB->Execute("select Address from Users where id=$id");
return $result;
}
And why does this error not occur within the main code of index.php? Because you inlcuded the config file directly into index.php which makes the variables/objects in config.php available to the code in index.php. The functions had no such import until you declared the $DB global.
Tags: PHP