skip to content

 Web design 

Database-driven systems

Easy-to-maintain web sites


 



PHP Help

This is my PHP help page. I put this together after answering lots of questions on the DaniWeb PHP Forum and finding that the questions became pretty repetitive. This is my attempt at avoiding having to keep repeating myself.  

I have provided info here on what I believe to be the most commonly asked questions. I have used my own info as well as selected internet links to provide the answers. This is a work in progress so I am starting small and it will build over time.

If you have any feedback or suggestions you can send them to me here. Don't send me requests to debug specific problems. Please post that sort of thing on the DaniWeb PHP forum.


Frequently Asked Questions (by Newbies)
Categories and Topics

Notice: This information is protected by Copyright. You may use it for your own benefit but you may not copy or reproduce it without permission.


1. General 2. Some Common Questions 3. Some Common Errors 4. Specialized Stuff
1. General
Q. I'm a PHP Newbie. How do I get started and where do I find good PHP information?

My suggestions are as follows:

  1. PHP Manual
    Download and use a copy of the PHP Manual. You can get it here. This is your single best source if you have a question about the language or how a particular PHP function or language construct works. Many newbies ask questions that have simple answers that can easily be found in the manual.

    The manual isn't as useful if you know what you want to accomplish but you don't know what PHP command/function or combination of them you need to use. In that case, I go to the web and do a search.
     
  2. Internet Sources
    The best all-around source for reference information and tutorials is w3schools. As you will see, it covers many internet-related technical topics, including PHP. If you do a search on something related to PHP they are usually near the top of the list. There are many other sources as well so you will have lots of choices. One important thing is to use a good search term so you hone in on what you are looking for. There is so much information related to PHP that you can easily end up drowning in material that isn't really what you need.
     
  3. Books
    I can't recommend any specific books. You can find one on Amazon or in your local bookstore. I'm sure that you can do a search and find some along with user reviews. I used an HTML book for reference when I first got started working with the web but I don't think I ever had a PHP book. My approach is more to start small and try things than to try to learn it as a subject. None-the-less, a well written book may speed up your learning process and may be a good investment.
     
  4. Sample Code
    One of the benefits of PHP is that there is all kinds of open-source code available, probably more than for any other language. A couple of good sources are Hotscripts and Sourceforge. They both cover much more than just PHP but there is a lot of PHP systems available on both. At the beginning, if you are looking for simple examples, you may find many of the systems are too large and complex and all that you want are some code snippets (one step up from "hello world"). If you do a search for a specific topic, I'm sure that you'll find references and examples.
     
  5. Getting Help
    There are many help forums on the Internet. DaniWeb is one of the most popular but there are many others. If you are a newbie and you don't demonstrate that you have made any effort before asking a question, don't count on a warm welcome. Getting over the learning curve requires effort on your part. I have seen too many posts on DaniWeb from people who haven't made the effort to look at the manual or do an internet search. You shouldn't expect people on the forum to be writing code for you. You also shouldn't expect to dump a bunch of half-working code and expect other people to debug it for you. If you are going to be a competent PHP programmer, you need to do your own research, learning and debugging. If you have given a good try and you're still not getting there, then that's when you should post the problem and look for some help.
     
  6. If you have something to do and you can't wait to learn PHP and everything else
    If it's more than a casual interest and you have a specific (challenging) job that you need to get completed sooner rather than later, then you may want to outsource the work (while you learn at your own pace). In that case, have a look at oDesk. You can find competent people for a pretty reasonable price. Spend your time spec'ing out the requirements / work and managing the project rather than doing the work yourself.


^ TOP





Q. I'm a PHP Newbie - What do I need to learn / know

 

PHP as a scripting language can't do it all on its own. It depends on a number of other systems to get the whole job done. In addition to PHP, you will want to know something about:

  • The Browser and its tools
    This is the local input and output device that PHP needs to control when it is dealing with a live user (which is most of the time). The Browser has its own scripting languages that it understands so in a PHP program you must generate output in a language that it can understand (it doesn't understand PHP, that only runs on the server):
    • HTML (mandatoy)
      This is the most basic "language" that the Browser understands. You can use HTML without ever using PHP or anything else. A good fundamental understanding of HTML is critical to being able to communicate with the user and present information in an appealing manner on the screen. Just like PHP, you can get info on HTML from online sources like W3schools and from books.
    • CSS (optional)
      Initially, HTML had all of the formatting information (colors, fonts etc) built in. You can still do that but the preferred method is now to use CSS. This is kept in a separate file that is referenced from an HTML script and it has much more capability than the old style native HTML formatting. As with PHP and HTML, you can get info online or from books.
    • Javascript (optional)
      This is a scripting language that is specifically for the Browser. If you want to use more advanced presentation techniques (web 2!) then you need to use some Javascript. There is some similarity between Javascript code and PHP code but they are quite distinct and personally, I find Javascript more difficult. The good news is that there are packages like JQuery and MooTools that make it much easier. 

      Ajax is a specific implementation of Javascript that is used to provide real-time behind-the- scenes communication between the local Browser environment and the server. An example of the use of Ajax is a signup form that tells you right away if the user name you selected is available. In a traditional PHP approach, you would have to submit the form first and then it would tell you if there are any errors. Ajax avoids the need to send the whole form to the server and then refresh the browser window in order to provide you with the information. 
       
  • The Backend - MySQL Database
    Serious web applications almost always use a database. You can store data in files on the server but your ability to store and retrieve the data from a flat file is extremely limited compared to what a database provides you. Most web hosts provide MySQL as the standard database. It is the only one that I use. There are others such as PostgreSQL, SQLite, Oracle and Microsoft SQL Server and they are probably no more difficult but chances are that you'll be using MySQL. Just like all the other topics above, there is lots of info available on the Internet. PHP provides a simple interface for MySQL.

This may seem like a pretty intimidating list but it is reality. You won't be an expert overnight. You can build some useful programs with PHP and some HTML and maybe a simple database. If you are aiming to write the next Facebook or Amazon or Ebay, you better be realistic about your own skills and knowledge and how much time you need to learn what you need to know and then to actually build something of significant size and complexity.

  • Linux / Windows
    If you are going to put your programs on a server, then it will probably be running Linux (or maybe Windows Server). If you are using a virtual server and you leave the server management to the Hosting Company, then you don't need to know too much about the server in order to use it successfully. I use a Windows machine for development and I use virtual servers that run under Linux. Most web hosts provide a GUI Control Panel that makes it easy to access all of the functions that you will need. If you are using a Linux server, then you need to understand about the Linux file security levels and a few other things but in general, the operation under Linux and Windows is pretty much the same. In your programs, you don't normally have to worry about which operating system is being used. You do need to be aware that the directory structure / names are a bit different so you don't want to be defining files as c:/xxxx/... in your program if you are going to be running on a Linux server. There are ways to deal with files and directories that will work in either environment.  


^ TOP





Q. What tools (and skills) do I need to develop and test PHP code?

There are various tools available that will help you but at the most basic level, all you need to write some PHP code is a text editor. There are better tools available and I will outline them below. You will also need to create a test environment. There are many packages that simplify the installation of a LAMP environment on your local machine (Linux, Apache, MySQL, PHP). I am running under Windows 7 but there are ways to create such an environment on a Mac or a Linux machine as well.

For a complete list of tools that I use (with links for info and download) click here.

Development Tools

  • Text Editors
    There are many programmer's text editors that will provide you with formatting and tools to make the development work more effective. I use the Context Editor but there are many others.
     
  • Integrated Development Environments (IDE)
    These provide the same capabilities as the best programmer's text editors plus other tools. I have used the Netbeans PHP IDE and it works quite well.
     
  • Frameworks
    I must admit that I have no experience with using a Framework. Some of these can be used in conjunction with an IDE (Symphony works with Netbeans for example) so they aren't exactly an option to the previous two approaches. Using a Framework is sort of like using a macro language. You are no longer writing native PHP as you would with the previous options so there is a learning curve to learn how to use the specific Framework that you have selected (and there are quite a few of them). Those who use them believe that there are significant benefits in structuring the code, speeding up development and simplifying maintenance and so forth. 
     

Testing Environment
You will need a 'LAMP' (Linux,Apache,MySQL,PHP) environment to test your code. The good news is that you don't need to know much of anything about any one of the components in order to get such an environment running on your machine. If you are running under Windows (or if your are on a MAC), then the Linux part is taken care of without any disruption to your Windows Environment.

Some of the more popular LAMP packages are listed here. I have been successfully using EasyPHP for quite a few years. Once the environment is set up, there will be a folder set up that you will use to save your PHP programs and they will be run from there (just like on a real server). The folder will likely be called www or htdocs. Once you start the LAMP package, you can access your folder of programs by typing in 'localhost' (or possibly http://localhost) into your browser).

On a Windows machine the default installation will be in the Program Files folder (or Program Files (x86) on a 64-bit machine) but I have have found it simpler to use if it is installed elsewhere. Windows limits saving of files into folders within Program Files so I put my installation in a separate partition on my hard disk. 

Debugging Tools and Strategies
This is where some people seem to hit the wall. They write a bunch of code (or copy it from somewhere) but when it doesn't work, they don't know what to do. Here are some things that you need or need to do:

  • You need to know if you have syntax errors in your code
    When you execute a PHP program, it will tell you if there are any errors and it will stop until you fix them. You don't need to execute your program to do this. You can run the PHP interpreter to do this function. I have this setup to run in my editor environment using a function key. IDEs such as Netbeans have this built in. If you run the interpreter, it will only give you the first error that it finds. You may have to fix problems one at a time and try again until you get through them all.

    Figuring out where the error is becomes easier as you gain some experience working with PHP. If you miss a closing bracket or quote, it probably won't detect this on the line where you missed it. It could be many lines later or even the last line in the program where it realizes that there is a problem. Thus, you can't count on the line that it reports as being where the problem is. You need to work backward paying particular attention to anything that you changed. If you are making changes to a large program, it can be beneficial to make changes a bit at a time and then do a check for errors to make any problems easier to find.
     
  • Figuring out why it didn't work
    Some people who post their problems on DaniWeb don't seem to have a clue how to do this. This is a basic programming skill that I take for granted but it doesn't seem to be obvious to many people. If you went through formal programmer training and/or you have many years of programming experience, debugging becomes second nature. If you are self-taught and/or relatively new to programming this might be a stumbling block for you. The things that you need to know are:
    • What has changed since the last time it worked
      If you are working on the program and it was working and then it fails (it doesn't give you the desired results but it has no syntax errors), you need to look carefully at whatever you changed. You may change something that you didn't intend to change or the logic of the change may have been incorrect. If you have a tool that shows you the current code versus a previous version (highlighting differences) that can sometimes be helpful. 
       
    • Is the logic working and is the data what you expected?
      If you aren't getting the result that you expected at line 400 in the code, then you need to work backward from that point and trace the logic and the data to determine where it went wrong. So how do you do that?
      • Desk-Checking
        This is the most basic approach. It can be very tedious and it isn't effective in all situations. You basically work through the logic of the program to validate that the logic is correct and that you didn't miss something when you wrote the code (e.g. incrementing an index). If the data isn't what you expected, then you probably won't find the solution this way.
      • Dump data / trace the logic (echo statements)
        The most basic way is to insert some echo statements at key points in the code. This can tell you a couple of things: values of key data fields; and, the flow of the logic. By selectively dumping data at selected points in the code, you can see the path that it is taking and what some of the key data variable values are.
      • Use a debugging package
        There are a number of packages that provide you with more sophisticated debugging capabilities. One of them is PHP-Debugger. The IDE's usually provide one as well. I can't recommend one because I built my own and that has satisfied my needs for quite a few years. My debug facility does the same thing as in the previous point with some additional 'bells and whistles'. I leave the debug statements in my production programs but turn the facility off once everything is working. If I have a problem, the program is already "instrumented" and I can turn it back on and immediately get the details that I thought were important when I did my original testing. 
         

Debugging can be hard, frustrating work. The only way to get better at it is to do it. If you write good, well-structured code then you will probably have fewer problems and it may be easier to find the problem. Even if you do, everything won't work the first time and you will still need to track down the problem. This could take more time than it took you to write the code. PHP allows you to pack / nest a number of statements together to create fairly complex statements. If your objective it make the code as compact as possible and push the interpreter to its limit, this may be a fine thing to do. If it doesn't work, it may be a lot harder to debug than a series of simple statements. I prefer the simple approach but it is up to you.

Once you have exhausted your options, if it still isn't working and you can't figure out why, then you may want to go to a help forum like DaniWeb. This may not be very beneficial unless you have isolated the piece of code that isn't doing what you want. If you dump a whole long program into your post and expect someone else to do your debugging for you, then you may be disappointed.



^ TOP





Q. My PHP Module won't execute

This could happen on your local machine or on the server. Here are some things to check out:

  • If it is running on a server, does the server support PHP? Check with your web host if you aren't sure.
    If it is running in your PC environment, you must have a proper LAMP (Linux, Apache, MySQL, PHP) environment installed in order to run PHP. You must start the environment so that all of the components are active before trying to run a PHP program.
     
  • Was the module placed into the correct directory? There is a specific root directory (or sub-folders within that directory) where html and php modules must be placed (this is normally called www or htdocs).
     
  • Does your PHP module have a .php suffix? If it doesn't then your server won't recognize it as a php module and it won't get interpreted.
     
  • Does your module use 'short-open tags'. PHP code can start with <?PHP or just <? (the short open tag version). The first is standard and acceptable on any server that supports PHP. The second is an option and it isn't supported on all servers. It is an option in the php.ini module and if you have a lot of modules that are already coded this way, you can activate it for your local environment by changing php.ini (short_open_tag=on). You may be able to do the same on the server (if the default is 'off') by using a local php.ini file that will over-ride the default.
     
  • Does your module have a code error? If you have an error in your code that the PHP Interpreter identifies before starting execution, then it won't run. Depending how the error_reporting level is set, you may get some error messages but you may just get a blank screen. You can check for any errors before trying to execute the module in an IDE like Netbeans or from your Editor if it is set up for this. If you get a blank screen, you can change / add the error_reporting statement in your module to make it:
      error_reporting(E_ALL);
    to show everything (including warnings that don't have any impact).


^ TOP





Q. I want to create a new Content Management System / Forum / Facebook etc - Please Help!!!!!

You see these quite regularly on the DaniWeb PHP Forum and they invariably come from newbies. They do not have an appreciation for the scale of these systems or the amount of time that it takes to develop one. This answer applies to trying to developing any large application from scratch. If you are one of those people, then you need to consider the following things:

  • Why do you want to do it?

    • To build something that is better / has different features
      The best of the existing open-source systems have been developed over an extended period of time by teams of pretty smart and skilled people. The features of the systems are usually pretty heavily influenced by their users (so they have a lot of what people really want and need). While there is a possibility that you could build something better, you will need a team that is at least as large and as skilled as the ones who are working on the existing systems if you are going to build something better.
       
    • To make money
      This is pretty unlikely. There is a lot established competition and most of it is providing open-source systems that can be downloaded and used for free. Producing a product that is even as good as those free systems is a significant challenge. Developing something that is better that people / companies are willing to pay for is more than a challenge.
       
    • In order to learn
      If you aren't already a skilled and experienced developer, then this isn't really the place to start. If you were just starting out as an amateur carpenter, you wouldn't be trying to build a 10 story building. 
       
    • As a challenge
      This may be the only one that makes sense at all. If you have a passion for doing it with no specific expectations of ever completing it or competing with the existing systems or making money on it, then give it a shot. 
       
  • How much work is it going to take?
    Measuring the size of a system and estimating the work involved is often done on the basis of the number of lines of code in the system. Large systems like a CMS or a Forum can have 200,000 - 300,000 lines of code. A lot of work has been done to estimate the amount of error-free, documented code that a developer can produce. It is probably a lot lower than you might think. The average for a project of this size is about 2500 lines of code / year. The upper limit is probably 15,000 lines of code / year but that is probably for a very quick and capable developer with very clear specs and no need to coordinate the work with anyone else. Remember that there will be more to do than just churning out code. Requirements and features need to be defined, the system needs to be architected, the project has to be planned and managed, documentation needs to be created and so forth. If you do the math, then you will see that it would take a single developer many years of full-time work to develop one of these systems. While some of the lesser, smaller systems might have been created by a single individual, the big ones have all been done by teams and most of them have been working at it for many years as well.
     
  • What skills (and tools) would I need?
    If you want to develop a system like this, then you are looking at the high end of development. If you want to start this sort of project before you are a skilled and experienced developer, then expect that learning will be a part of the project and that it will take time and potentially lead to some re-work of the project as you learn the things that you didn't know at the start.

    You will certainly need to develop a very good knowledge of PHP, MySQL, HTML and CSS. You will probably need to know at least a bit of Javascript and/or how to use libraries like JQuery. You will need to understand how to use and develop classes and functions. You may want to use a Framework for your development. In that case, you will need to assess them, pick one and then become very familiar with it. You need the ability and perseverance to research questions and find the answers on your own. You will have many questions and this will be far beyond an occasional question on Daniweb. Generally, the answers are already out there on the internet but you have to be prepared to take the time to search for them.

    With a large project, you have a greater need to manage the project and keep track of all the plans and the components. You will need some sort of project tracking tool and you will need to use it consistently. I have covered the tools that I use elsewhere on this site and you can see that page here. Professional development of a large system takes a certain amount of organization and discipline, just as if you were working for Microsoft or Google. The difference is that you need to create it for yourself rather than have a large organization provide some of the processes and expectations for you.
     
  • What are the alternatives?
    This is the good part. With so much open-source code available, there are some things that you can do:
    • Use Existing: If what you want is something to use on your own web site (or for a client), find the open-source system that comes closest to what you need and then use it. There are enough good open-source systems available that you will probably find something that comes close and you can probably have it up and running in a day or two. If you can't find something for free, consider the time and effort it will take to build something and consider buying a system if you can find one to fit your needs. 
       
    • Learning: If you want to learn, then starting with a large complex system isn't really the place to start. Once you get past the basics however; it can be instructive to download some large open-source systems and try to understand how they work. You also have the option to get involved in one of these systems as a tester, documenter or developer.
       
    • Contribute: If there is a new feature that you'd like to have in an existing system, you could choose to develop it as part of the core system and give it to the project to potentially implement as a standard feature in a future release. It could also be a new application / plug-in that you would maintain as an add-on to the base system.
       
    • Fork/Experiment: If you want your own unique version of a CMS or a Forum, open-source allows you to take a copy of an existing system and create your own "fork" (ie a separate version). You can take advantage of all the work that has gone into the existing version and then put your own stamp on it. This is generally less desirable (and potentially a lot more work) than contributing to the existing system so you won't want to go down this path without the appropriate resources and a compelling reason to do it. As a learning exercise however; you can certainly do this to experiment with making changes to one of these systems.
       
    • Start Small: Forget about these big systems and start with something much smaller. You can get some satisfaction by completing something in a much shorter period of time without all of the complexity needed for a big system. If your aim is to make it available as open-source or as a commercial product, you can gain some experience with that much faster and learn from it.  
       


^ TOP




2. Some Common Questions
Q. Forms: How do I accept data from the user and process it in my PHP program?

This question comes up regularly. Once you understand how it works, the rest is just details:

  1. You define a form (in HTML). This can be done in a pure html module or you can generate it from a PHP program. A form starts with a <form> statement and ends with </form>. On the <form> statement you can define two parameters:
    • method=post (could also be method=get but we'll ignore that for now).
    • action=module_name.php (this is the name of your php module that will process the form fields. If you don't define it, then the default is to use the same program (that contained the form) to process the form. You can use a relative or absolute address for the module name.
       
  2. You then need to define the input fields in the form. These generally start with <input type= but there are one or two other variations. You can define text fields, drop-down selection lists, radio buttons, check boxes and so forth. You can also define hidden fields that allow you to pass info as part of the form data that the program provides rather than the user.
     
  3. You create the php code / module that will process the form. The input 'fields' in the form will be contained in the $_POST array with the element names the same as the names you defined for the input 'fields'.

    You can see more details if you follow the links below.
     

Click here for an overview of the different types of fields that you can use in a form.

Click here for an example of a form and the module that processes the data. 



^ TOP





Q. Input: How do I read input from a spreadsheet

There are a few options:

  1. If it is a one-time activity to load a database table, you can use PHPMyAdmin to read a CSV or XLS file and load it into the table. 
     
  2. If you want to read it into a program, then you can read it from a CSV file. Here is an example of uploading a CSV file and then loading it into a MySQL Table.
     
  3. You can read directly from an XLS file. The php-excel-reader can be used to read from individual tabs. 


^ TOP





Q. Output: How do I save output to a spreadsheet

You can use Header statements to specify different types of output formats. You can save a file formatted with html as a spreadsheet file and Excel will take care of the conversion / translation. My little desktop_write utility routine packages the code to produce spreadsheet output and other kinds of output files.

 

 



^ TOP





Q. Output: How do I save output to a Word file or other file types

 

You can use Header statements to specify different types of output formats. You can save a file formatted with html as a .doc file and Word will take care of the conversion / translation. My  desktop_write utility routine packages the code to produce Word output as well as:

    • MS Works spreadsheet (xlr)
    • Notepad (txt)
    • Powerpoint (ppt)
    • Rich Text (rtf)
    • Comma separated variables (csv)
    • MS Excel (xls)

 

 



^ TOP





Q. Output: How do I save output to a PDF file

The easiest way to create PDF output from a PHP program is to use HTML2PDF. You create your output as normal html. Rather that echo the html to the screen directly, you need to create a variable containing the report output. You can then echo it to the screen to make sure that it is formatting correctly. Youcan then change it to call html2pdf to convert and optionally to display it. 

More information on this to come.



^ TOP





Q. SMS: How to I send and receive text messages from a PHP program?

Free Service
An SMS (text) message can be sent from a PHP program just like an email. The key is to know the Service Provider for the phone and to then know the email address format that you need to use for that Service Provider. As an example, to send a text message to someone who has cell number 123-456-7890 on AT&T, you would send the email to:

  1234567890@page.att.net 

For a pretty complete list of Service Providers and the format they require, click here.

In order to send messages this way, there must be some sort of registration / sign-up process where the potential recipients give you their number and their Service Provider which you then record (in a database). You cannot receive text messages from cell phones directly through the Service Providers.



Commercial Gateway Service
If you want to be able to send messages without knowing who the Service Provider is and if you want to receive incoming text messages from cell phones, then you need to sign up with a Gateway service that will deal with that for you. There are a few free Gateways but they generally have paid advertising to pay the cost of the service. Thus, by using such a service you subject your users to advertising on their cell phones and run the risk that their cell number might get onto a spam list.

Paid services such as Ozeki can provide the full service with less risk of abuse.



^ TOP





Q. PayPal: how do I add PayPal IPN to my web site?

PayPal Instant Payment Notification allows you to send your customers to PayPal for Payment; and, PayPal then provides you with immediate feedback on the result of the transaction. PayPal provides pretty good documentation on the interface but rather than write it from scratch, you can use the paypal.class by MIcah Carrick. It works well and saves you the time and trouble of building your own.



^ TOP





Q. Mail: I need to send emails (with attachments) but I'm having trouble making it work.

Sending a simple email from PHP isn't too complicated but once you start adding some of the optional features, especially attachments, then it gets more difficult. I use a class called libmail that satisfies my needs and you may find it useful as well. My version for download is available here. This version has a few changes from the original:

  • It no longer stops if it finds an invalid email address;
  • It allows multiple email addresses in the TO string separated by commas
  • It allows you to pass an attachment (rather than getting it from a file.

There are other email classes available on the internet (e.g. phpmailer) so feel free to do a search and look at the other options as well. 



^ TOP




3. Some Common Errors
Q. PHP Parse Error: syntax error, unexpected $end in ... on line nn

This is a very common error and it usually means that you have left out a closing quote, brace or bracket. What makes this difficult to find and fix is that the PHP Interpreter only discovers the problem some number of lines later when it discovers something that should logically only happen after the missing quote brace or bracket. If you have missed a closing brace, it will be easier to find if your code is indented where you can see closing braces matching up with opening braces. If your IDE / Editor highlights the matching opening / closing brace or bracket when you put the cursor on one in your code, that will also make it easier to find.

To debug this, you need to:

  1. Look at any code you have recently added or changed (since it was last error-free) and see if you can spot a problem. If you are regularly saving the history of changes* you should be able to compare your current version to a previous version and this may make it more obvious.
  2. Use whatever tools that your IDE / Editor provides (such as matching up braces, brackets and quotes) to help you find what you missed.
  3. Work backward from the line where the error was reported to see if you can spot a problem.
     
  4. If you still can't find it, then pretty much the last resort (before going to a Forum and begging someone else to find it for you!) is to start (temporarily) removing or commenting-out large sections of your code to isolate the problem. You probably want to make a working copy of the module and play with that until you find it. You can do this like a binary search by removing the second half of the module and if that eliminates the error then we know it is somewhere in the second half. Then we put the module back together and remove the last quarter and try it again. Doing this a few times should allow you to narrow it down until you have a short section of code to inspect to find the problem.
    NOTE: You can create new problems if you don't do this carefully. If you remove the last half of a While loop or an If statement, then you are going to introduce a new problem that will just make it more confusing. If your module is a series of nested loops, then you will need to change your strategy and work from the middle out rather than removing blocks of code from end or the other.

* There are a number of systems that you can use to save snapshots of your modules at specific points in time. Netbeans has an interface to Subversion, Mercurial and CVS and it also provides its own local history. I also use CSRCS which is very simple to install and use.

 

 



^ TOP





Q. MySQL: Supplied argument not a valid resource

This usually means that you didn't connect successfully to MySQL (e.g. didn't provide the correct ID and/or Password) so you haven't been provided with access (a 'resource' id). If you do some error checking after the MySQL connect you will likely find that it returned FALSE indicating that the Connect didn't work.

Here is a link for more information: "..not a valid resource"

 



^ TOP




4. Specialized Stuff
Q. How to do Screen Scraping

Screen scraping is the extraction of data from a web page. You might be able to go directly to the page in question and pull all the data that you need however; it can be much more complicated. In many cases, you may need to follow links to find the data that you need and you may need to handle a sign-in as well. For pages that use Javascript and call-backs, it can be really difficult if you aren't using the right tool. There are a number of options depending on the complexity of the requirement:

  1. DYI (Do It Yourself)
    You can read a web page like a remote data file) in PHP and do your own analysis to find what you are looking for. This might be ok for a simple extract but if you are going to do somethying serious, you probably want to try another approach.
     
  2. Utilities
    There are programs that you can download that will do some screen-scraping for you. The better ones can be pretty expensive but there are some free edition. Screen Scraper provides a free version that you could try.
     
  3. PHP - Class
    I used a php class called class_http.php from Troy Wolf do do some data extraction that required some fairly serious navigation to get to the data and then a series of steps to get the data that I needed. Using the PHP CURL tools is another similar approach.
     
  4. Autoit
    If you don't have a specific need to use PHP and run the extract from a server, then you can use a Windows program instead. Autoit is a scripting tool that has a syntax that is quite similar to PHP. It was built as an automation tool for Windows systems. It has a lot of user-built extensions (called UDFs) that provide a terrific range of capabilities. One of these is the IE UDF that accesses the browser functions that are built in (with a COM interface) to the Windows Operating System. Among other things, this allows you to directly access elements of a web page by name or by ID and to get the value of those elements (or set them).

    Developers usually have a pretty poor opinion of the Internet Explorer browser but that shouldn't be confused with this tool. This provides access to the basic facilities within Windows (which are also used by Internet Explorer) and they work just fine. They allow you to replicate what someone using a browser would do. It is the only tool that I could find that can successfully access ASP pages. Your program is positioned at the same point as a person sitting at a keyboard. Re-directs and callbacks can cause problems with other scraping tools but in this case, the IE facilities deal with it and give you the end result.

    I believe that Autoit is the ultimate screen scraping tool. It can handle virtually any requirement. Autoit is free and it is pretty easy to learn and use, especially if you already know PHP. It provides the ability to create a compiled version of your program so that is a big bonus, especially if you need to deploy this on other machines. You can even build a script that can be used to transfer data to a PHP program so your data can still get to a server if that is required.

     


^ TOP




CopyRight Innovations Design 2011
Feel free to use this information to help you to be successful using PHP.
You may not republish this information, in whole or in part, without permission. Feel free to link to this page.
 
   

Powered by Website Baker,
Design by Innovations Design based on a template by gavjof