Source files for PHP Solutions

Several people have written asking what has happened to the friends of ED website because they can’t find the source files for PHP Solutions, 2nd Edition. The site was merged with that of its parent company, Apress, a long time ago, and is no longer online.

The source files for PHP Solutions, 2nd Edition, are available from the book’s page on the merged site. I’ve put a notice to this effect on Amazon. It’s also prominently mentioned on the book’s updates and corrections page on this site. However, the information obviously hasn’t got through to everyone who needs it, so I’m posting it here with a subject line that will hopefully be picked up by search engines.

This entry was posted in Books, MySQL, PHP. Bookmark the permalink.

44 Responses to Source files for PHP Solutions

  1. Coder47 says:

    I’m enjoying learning from your books, and am interested in the possibility of using certain of your code examples, with certain modifications, in one or more websites. I see that the text of your books affirms their copyright. Do you provide for possible use of that code by your readers? Is the code published in your books and website under any open source licenses? Please let me know so I can look elsewhere if needed. (I would prefer a back-channel email response.)

  2. David Powers says:

    You’re free to use the code in your websites without restriction. The copyright refers to republishing. For example, you can’t use the code in an article, and claim it as your own. Nor can you republish or redistribute the text without permission. Also, the images in the source files are provided only for the purpose of the exercises.

    But the whole idea of the examples is to teach you how to use the code – and to encourage you to use it.

  3. Coder55 says:

    Hi is there any way this code to work with Cyrillic:

    <input name="name" id="name" type="text" class="formbox"

    I tried this way, but did not get:

    <input name="name" id="name" type="text" class="formbox"

    Thanks in advance!

  4. Coder55 says:

    Sorry but this site does not allow me to write code here. Is there a way to ask you a question and write the PHP code here? Thank you!

  5. David Powers says:

    The way to write code is to use HTML entities for angle brackets. Alternatively, just leave out the angle brackets.

  6. Coder55 says:

    Hi, is there any way this code to work with Cyrillic:
    <input name=”name” id=”name” type=”text” class=”formbox”
    <?php if ($missing || $errors) {
    echo ‘value=”‘ . htmlentities($name, ENT_COMPAT, ‘UTF-8’) . ‘”‘;
    } ?>>

    I tried this way, but did not get:
    <input name=”name” id=”name” type=”text” class=”formbox”
    <?php if ($missing || $errors) {
    echo ‘value=”‘ . htmlentities($name, ENT_COMPAT, ‘cp1251’) . ‘”‘;
    } ?>>

    Thanks in advance!

  7. David Powers says:

    There’s nothing wrong with your PHP code: cp1251 is supported by PHP. I suspect that the problem lies with the encoding being used when the form is submitted. cp1251 is a Windows-specific encoding, so it probably won’t work if someone uses a Mac or Linux machine to submit the form. You would also need to check the headers being sent by the server. If it’s using a different encoding, the characters will be garbled.

    UTF-8 supports just about every writing system in the world, and is intended to replace language-specific codes like cp1251. It certainly supports Cyrillic. So, in the end, I think you’ll probably find it easier to try to get everything working in UTF-8.

  8. Coder55 says:

    Yes you were right the problem was missing opening quote in the header:

    when i correct it, works like charm:

    Thank you and thanks for a great book: PHP Solutions
    Dynamic Web Design Made Easy

    Best regards

    P.S. The code was taken from
    folder ch5 – contact_04.php

  9. Coder55 says:

    Yes you were right the problem was missing opening quote in the header:
    <meta charset=utf-8″>

    when i correct it, works like charm:
    <meta charset=”utf-8″>

    Thank you and thanks for a great book: PHP Solutions
    Dynamic Web Design Made Easy

    Best regards

    P.S. The code was taken from
    folder ch5 – contact_04

  10. Jacob says:

    I am having a problem with the code on page 308 in PhP Solutions, 2nd Edition:

    The problem is when defining $result

    query($sql) or die(mysqli_error()); num_rows;

    from this I get the resulting error message:
    Fatal error: Call to a member function query() on a non-object in C:\wamp\www\PhPsolutions\mysql\mysql.php on line 8

    I looked in the errata on both this site and Apress but I cannot seem to find a solution. I simply tried ignoring it, hoping the code wasn’t too important, but $result is used throughout chapter 11. Do you know where I can find a solution to this? Please help.

  11. Jacob says:

    Sorry my code did not paste correctly, here it is:

    query($sql) or die(mysqli_error()); num_rows;

  12. Jacob says:

    3rd time the charm i hope:

    //connect to MySQL
    //prepare the SQL query
    $sql=’SELECT * FROM images’;
    //submit the query and capture the result
    $result = $conn->query($sql) or die(mysqli_error());
    //find out how many records were received

  13. Jacob says:

    sorry about all the posts, not trying to be a spammer 🙂
    I found a soulution. It was in fact answered on the Apress site in the Errata section.

    On page 306-308:
    This is one of those cases where the error isn’t actually pointing to the right place. The issue appears to be in the file at roughly line 14 (will vary depending on how you format your code).

    Book code: if($connectionType == ‘mysqli’) {
    return new mysqli($host, $user, $pwd, $db) or die(‘Cannot open database’);

    Correct code: if($connectionType == ‘mysqli’)
    $conn = new mysqli($host, $user, $pwd, $db) or die(‘Cannot open database’);
    return $conn;

  14. David Powers says:

    Glad you found the solution on the Apress site, Jacob. It’s also on the errata page on this site (it’s the correction for page 307). Not sure how you missed that.

  15. Giovanni says:

    Dear Mr. Powers,
    thank you for your great book! I am geeting deep into OOP.
    I just have one problem:
    on your: Php OOP (first edition/page 180) I cannot use $url = ‘’; becouse the page does no longer exist.
    would you mind tell me another webpage I could use on test_connector.php?
    At the moment I get a blank screen or an error in the __to String function while running it.
    Thank you very much for your answer

  16. David Powers says:

    You can test it with any valid URL. If you’re looking for a news feed, try one of the BBC feeds, such as

  17. Giovanni says:

    Thank you so much for your prompt answer!
    Now I am sure I just have to find an error on my script!
    Should you visit Itlay I will take you for a tour of Venice!!
    Thank you angain, have a nice day!

  18. mike says:

    I’m stuck on page 255 of php sols second edition. Dealing with simple login. I’m not getting any errors from the server nor do I see any when I go and view the output source code but I can not get to either the menu page or the secret page. I went through the first part of this chapter twice and built the code over again searching for mistakes but I am at a loss, not to be rude but I was hopping someone else was having the same problem. There are a few different pages involved in this should I paste all the code or do you have an idea of what it might be?
    Thank in advance, the books has been great otherwise.

  19. David Powers says:

    Hi Mike, Are cookies enabled in your browser? PHP sessions require cookies to be enabled.

  20. Kev says:

    I had a question on Chapter 6 (Uploading Files) I always wanted to ask: When you have the section to ask:

    In the section of the chapter when you used the str_replace() to replace spaces with underscores with the intention of removing the clearly invalid character for file names on a server, I was curious why other invalid characters weren’t addressed or even using preg_replace for the same task, which is (technically) faster.

    preg_replace(‘/[^a-z0-9_]/i’, ”, $filename) or a similar implementation would have worked. Considering you use preg_replace and other regular expression functions elegantly in other sections of the book, I always wondered why you didn’t do it in that section.

  21. David Powers says:

    Hi Kev,

    When you’re uploading a file from the local hard disk, the operating system will have prevented the use of invalid characters in the filename. So, the only characters that you need to deal with are spaces. For a simple replacement, str_replace() is more efficient than preg_replace().

  22. Mr. Powers,

    I’m reading and experimenting with your PHP Solutions latest version – so far so good…

    However, I have a very particular – but common – interface problem I want to find – and looking through the book/index I don’t think you address it.

    You – and a lot of others – show how to create custom forms to add, update and delete database records – but typically – these are forms involving individual records – or rows of records in a table.

    Is there a guide anywhere, in your book or otherwise – that shows how to create forms that allow you to edit, update and delete inline? On a single page? There are a lot of control panel applications – like phpMyAdmin – that provide similar functionality.
    With related tables…

    Using the common Author/Book example – show a page with

    Author Info, Name, Contact etc.

    Book 1 Info, fields etc.
    Book 2 Info Fields, etc.
    Book 3… etc.

    And easily be able to edit Author info, Book info, add and delete – all from what appears to be a single page?


  23. David Powers says:

    Hi Charles, No. My book doesn’t deal with that type of scenario. phpMyAdmin uses custom forms similar to those described in “PHP Solutions”, but the underlying code is more complex because it’s dealing with multiple database entries. From your description, it sounds as though you want the ability to edit directly without resubmitting the form. To do so, you need to use Ajax, which brings JavaScript into the equation. You might find “jQuery in Action” helps you understand how to combine Ajax with an online form.

  24. Thanks! I’ll get that book also … you are absolutely right.. I need to provide “Ajax” functionality to create a ‘control panel’ like application with links to my databases.
    I see Dreamweaver support Spry controls – but they don’t connect directly to MySql DB’s – instead seem to use local files or XML database’s.

    Doesn’t Dreamweaver directly support Ajax interaction to a database? Or have an extension to make it simple?

    I’m prototyping a site… but eventually will hire a designer. I find it hard to believe that DW itself doesn’t allow you to create a basic Ajax driven form…?


  25. Kev says:

    Thanks again for answering my original question. I also had an additional question that also has been in my mind:

    On Page 444, when you used the getFilenames() public function, I am confused why we can safely ignore the possibility that more than one image has been uploaded and a need may be necessary to concatenate things using the MySQL insert syntax for multiple entries which is later done for the categories.

    While understandably we would need to explicitly add ‘[]’ to the name of the input file for this case to be applicable, wouldn’t we mistakenly show sever errors to the user of the page unnecessarily if they were to edit that part of the form using the browser (assuming someone was lazy and didn’t use ‘@’ to control errors and redirect to a proper errors.php page) ?

  26. David Powers says:

    I simply made the assumption that only one image would be uploaded. As you say, you would need to change the name attribute to accept multiple files. You would also need to add the multiple attribute to the input field. The script is quite complex as it is, so I decided to go for the simpler option of just one image.

    However, I disagree with your categorization of not using @ to control errors as being lazy. Using @ is the lazy option. You should try to catch errors by checking what’s uploaded to your server, and build in the necessary conditional logic to deal with the situation.

    When writing a book, it’s impossible to cover every eventuality. The book and code would become overwhelmingly long for most readers. What I hope is that my books encourage readers to start thinking for themselves rather than relying on canned solutions. The fact that you’re asking these questions suggests that you are thinking about different scenarios, which is great.

  27. jacob says:

    Hi me again,
    on page 454 in Php Solutions, 2nd edition i am having problems in phpMyadmin setting the composite primary key for the article2cat table. when i try to set the Internal relations part for artical_id, i get this error message. I have tried doing the same methods on both my local version of phpMyadmin and the version that is online, both came up with the same error.


    SQL query:

    ALTERTABLE`article2cat`ADDFOREIGNKEY (`article_id`)REFERENCES`39758_phpsols`.`blog` (


    MySQL said:
    #1452 – Cannot add or update a child row: a foreign key constraint fails (`39758_phpsols`.<result 2 when explaining filename ‘#sql-137a_a3c8e41’>, CONSTRAINT `#sql-137a_a3c8e41_ibfk_2` FOREIGN KEY (`article_id`) REFERENCES `blog` (`article_id`))

  28. jacob says:

    I found a solution. When creating the table I think I forgot to set both colums as the primary key, I remade the table and it is working fine now.

  29. Mikw says:

    Well, I see I’m the only one that seems to be having this problem. I was hoping it was just a minor typo in the book. But I keep getting this error message trying to work the code on page: 466

    Fatal error: Call to a member function bind_param() on a non-object in C:\xampp\htdocs\phpsols\includes\ on line 41

    I’ve used the code I wrote as well as the code provided in the sample files and continue to get the same error.
    Is it maybe the versions I’m using?
    Software version: 5.5.25a – MySQL Community Server (GPL)
    Apache/2.4.2 (Win32) OpenSSL/1.0.1c PHP/5.4.4

  30. Mikw says:

    OK I figured it out. For some reason the database table (users) didn’t save after I created it. It’s always the simple things that are overlooked =~)

  31. David Powers says:

    Glad you sorted it out. These little things are sent to try us. 😉

  32. Kev says:

    Thanks you for the last response; I had intended to mean having the @ along with catching errors or do you think that’s excessive or over complex bothering with both at the same time for a particular script?

    Overall, your book does provide a clear understanding of how to begin thinking critically on how to solve problems involving the language , just making sure I am not going off the road with how I’m thinking about solving issues before they come problematic habits…

  33. David Powers says:

    All that @ does is suppress the display of error messages. In a production environment, display_errors should be set to Off, so there should be no need to use @. In a development environment, you need to see all error messages to check what’s happening with your code.

    If your hosting company doesn’t set display_errors to Off, you might want to use @ to suppress error messages. A better option would be to add php_flag display_errors off to an .htaccess file (assuming it’s an Apache server).

  34. Nasser says:

    Dear David,
    I got your book “PHP Solutions” from Amazon. I have been trying to download the source codes from the book site but each time I got the message that the contained files are corrupt. Is there any mirror link or a way to get by this?

    P.S: I tried different resources to learn PHP, except for your book, they all fall into one of two categories: too simplified to build a confident experience or thorough in a way that makes you feel you don’t have enough background… your book walks the reader in a very smart way that preserve simplicity and confidence! Thank You.

  35. David Powers says:

    Where are you trying to download them from? The correct location is given in my blog post. Here it is again:

    I downloaded the files from that location a couple of days ago, and they were not corrupted. If you’re still having a problem, try a different browser.

  36. Nasser says:

    Thanks a lot,
    Yes I was using Explorer in three different comptrers, I just downloaded them with Firefox.


  37. David Powers says:

    Glad you managed to get them. I’ve heard several complaints about Internet Explorer mangling zip files on download. Not sure what can be done about that.

  38. Rob Berridge says:

    I am using your book PHP Solutions 2nd Edition. I am trying to connect to MySQL pg 306-308 . I keep getting the same error, which I thought I fixed by putting in the correction on pg 307 (Thanks) But to no avail. I have even resoted to copying your code and trying again, same error. The error is:
    query($sql) or die(mysqli_error()); // find out how many records were retrieved $numRows = $result->num_rows; ?>

    A total of records were found.
    End Quote”

    I am using PHP Version 5.3.1 Downloaded source files with Firefox. Running Windows 7. Everything else I have tried from your book runs as stated.

  39. David Powers says:

    Hi Rob,

    I have just tested mysqli_01.php from the ch11 folder, and it works just fine for me. The only difference is that I’m running PHP 5.3.8. Looking at the error message you reproduce here, it looks as though the query and result are not inside the PHP block above the DOCTYPE. The page is displaying the raw PHP instead of processing it.

  40. Rob Berridge says:

    Thanks for your reply…. I found the problem :-/ I fell back into my html testing ways , Ctrl + O then click the file :-/ instead of typing in localhost/mysql etc etc.
    Works better that way 🙂
    Thanks again for the reply.

  41. Alex B says:

    Mr. Powers, I have been using your book, PHP Solutions, to learn basic PHP. I am currently working through the managing content chapter, chapter 13 I believe. I also think I have an older print, because I am working through the MySQL versions, not the Improved or PDO. Anyway, the problem is, I have used your example verbatim for the update page, but each time I submit I get the row empty error and the records in the database are not changing? Is this because I am using the old MySQL?



  42. David Powers says:

    Hi Alex,

    It sounds as though you’re using the original edition of PHP Solutions, which has a gold cover and was published in 2006. The second edition, which has a black cover and was published in 2010, doesn’t use the original MySQL extension. Have you checked the errata for the first edition?

    I doubt if the problem is related to using the original MySQL extension. The code should still work. It sounds as though you’re not passing the primary key correctly.

    Quite honestly, though, you shouldn’t be using the original MySQL extension. I included it in the first edition because many hosting companies had not upgraded to PHP 5 when the book was published. If your hosting company is still using PHP 4, it’s time to move. Support for PHP 4 ended on the opening day of the Beijing Olympics four years ago. Use PHP 5 and MySQLi instead.

  43. Felix says:

    Hi there David!

    I’ve been following your tutorials and articles for several months now and I find it really interesting and nourishing. Thanks and thousand salutes for people like you. I foresee where the knowledge that I gained from you will take me to an advance. More power and more books please!

  44. tashi says:

    hi David,

    i really find your books as delicious as my favourite dish.please some more books. thumbs up for you.