SitePoint Sponsor

User Tag List

Results 1 to 25 of 25
  1. #1
    SitePoint Member
    Join Date
    Oct 2013
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Exclamation Unknown errors on "Build Your Own Database Driven Web Site Using PHP & MySQL"

    I downloaded the code archive for the entire book, and so far all of it has worked. I am on chapter 7, and when I go to chapter7/admin/authors/index.php , I get the error "Error fetching authors from database!" I changed the database username, password, etc at /inludes/db.inc.php by the way. Do you know what's wrong or what else I have to do?
    Thanks

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,813
    Mentioned
    141 Post(s)
    Tagged
    0 Thread(s)
    Can you post the code that you have in authors/index.php
    Be sure to congratulate xMog on earning April's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  3. #3
    SitePoint Member
    Join Date
    Oct 2013
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There's a lot of code in it, but this is the part that is involved in the error.

    // Display author list
    include $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';
    $result = mysqli_query($link, 'SELECT id, name FROM author');
    if (!$result)
    {
    $error = 'Error fetching authors from database!';
    include 'error.html.php';
    exit();
    }

    while ($row = mysqli_fetch_array($result))
    {
    $authors[] = array('id' => $row['id'], 'name' => $row['name']);
    }

    I believe the bolded line is the part that is wrong.
    I am running this on my website which uses MySQL Databases and phpMyAdmin. I have to go to phpMyAdmin to post the sql from chapter 7 into here. In the book, they use localhost and build in sql, which they can run in terminal.

  4. #4
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,813
    Mentioned
    141 Post(s)
    Tagged
    0 Thread(s)
    Okay,
    1) does your database contain a table named 'author'? If not, which table most closely resembles 'author'?

    2) Can you post your db.inc.php however, please empty out your username and password.
    Be sure to congratulate xMog on earning April's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  5. #5
    SitePoint Member
    Join Date
    Oct 2013
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is the db.inc.php contents

    PHP Code:
    <?php
    $link 
    mysqli_connect('localhost''websitedomain_joke''password');
    if (!
    $link)
    {
        
    $error 'Unable to connect to the database server.';
        include 
    'error.html.php';
        exit();
    }

    if (!
    mysqli_set_charset($link'utf8'))
    {
        
    $output 'Unable to set database connection encoding.';
        include 
    'output.html.php';
        exit();
    }

    if (!
    mysqli_select_db($link'websitedomain_joke'))
    {
        
    $error 'Unable to locate the joke database.';
        include 
    'error.html.php';
        exit();
    }
    ?>
    This image is the tables that are in phpMyAdmin (The tables that were made from the SQL I pasted in there).
    http://imgur.com/D9Df72y
    Last edited by SpacePhoenix; Oct 8, 2013 at 00:17. Reason: placed php tags around php code

  6. #6
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    4,904
    Mentioned
    93 Post(s)
    Tagged
    0 Thread(s)
    Can you please post a screenshot of the authors table? Possibly one of the field names is spelt wrong in the table
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  7. #7
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,813
    Mentioned
    141 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Aeip View Post
    This is the db.inc.php contents

    PHP Code:
    <?php
    $link 
    mysqli_connect('localhost''websitedomain_joke''password');
    if (!
    $link)
    {
        
    $error 'Unable to connect to the database server.';
        include 
    'error.html.php';
        exit();
    }

    if (!
    mysqli_set_charset($link'utf8'))
    {
        
    $output 'Unable to set database connection encoding.';
        include 
    'output.html.php';
        exit();
    }

    if (!
    mysqli_select_db($link'websitedomain_joke'))
    {
        
    $error 'Unable to locate the joke database.';
        include 
    'error.html.php';
        exit();
    }
    ?>
    This image is the tables that are in phpMyAdmin (The tables that were made from the SQL I pasted in there).
    http://imgur.com/D9Df72y
    Okay, two questions come to mind, you are using "stribbly_joke" in your mysqli_select_db call correct (and you simply replaced it before posting here)?
    And you aren't seeing the contents of 'error.html.php' or 'output.html.php'?
    Be sure to congratulate xMog on earning April's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  8. #8
    SitePoint Addict
    Join Date
    Aug 2006
    Location
    Nantwich, Cheshire
    Posts
    278
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    My copy of that book (albeit old, 2nd edition, and using mysql instead of mysqli) has the table name "Authors", not "Author", which would cause trouble with your query.
    http://www.firenza.net - my homage to a car from the 1970s

  9. #9
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,813
    Mentioned
    141 Post(s)
    Tagged
    0 Thread(s)
    Interesting, the latest code archive for this book uses PDO instead of mysqli and it seems to work out of the box.
    Be sure to congratulate xMog on earning April's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  10. #10
    SitePoint Addict
    Join Date
    Aug 2006
    Location
    Nantwich, Cheshire
    Posts
    278
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by droopsnoot View Post
    My copy of that book (albeit old, 2nd edition, and using mysql instead of mysqli) has the table name "Authors", not "Author", which would cause trouble with your query.
    Ignore that - OPs screen shot clearly shows table is called 'author'.
    http://www.firenza.net - my homage to a car from the 1970s

  11. #11
    SitePoint Member
    Join Date
    Oct 2013
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's the authors table
    http://imgur.com/qyCVXtJ
    Here is the SQL code that made these tables (The code I pasted and ran in phpMyAdmin):
    Code:
    CREATE TABLE joke (
    	id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    	joketext TEXT,
    	jokedate DATE NOT NULL,
    	authorid INT
    ) DEFAULT CHARACTER SET utf8;
    
    CREATE TABLE author (
    	id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    	name VARCHAR(255),
    	email VARCHAR(255)
    ) DEFAULT CHARACTER SET utf8;
    
    CREATE TABLE category (
    	id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    	name VARCHAR(255)
    ) DEFAULT CHARACTER SET utf8;
    
    CREATE TABLE jokecategory (
    	jokeid INT NOT NULL,
    	categoryid INT NOT NULL,
    	PRIMARY KEY (jokeid, categoryid)
    ) DEFAULT CHARACTER SET utf8;
    
    # Sample data
    # We specify the IDs so they are known when we add related entries
    
    INSERT INTO author (id, name, email) VALUES
    (1, 'Kevin Yank', 'kevin@sitepoint.com'),
    (2, 'Joan Smith', 'joan@example.com');
    
    INSERT INTO joke (id, joketext, jokedate, authorid) VALUES
    (1, 'Why did the chicken cross the road? To get to the other side!', '2009-04-01', 1),
    (2, 'Knock-knock! Who\'s there? Boo! "Boo" who? Don\'t cry; it\'s only a joke!', '2009-04-01', 1),
    (3, 'A man walks into a bar. "Ouch."', '2009-04-01', 2),
    (4, 'How many lawyers does it take to screw in a lightbulb? I can\'t say for fear of being sued.', '2009-04-01', 2);
    
    INSERT INTO category (id, name) VALUES
    (1, 'Knock-knock'),
    (2, 'Cross the road'),
    (3, 'Lawyers'),
    (4, 'Walk the bar');
    
    INSERT INTO jokecategory (jokeid, categoryid) VALUES
    (1, 2),
    (2, 1),
    (3, 4),
    (4, 3);

  12. #12
    SitePoint Member
    Join Date
    Oct 2013
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am using the 4th edition. I'll try the 5th edition code.

  13. #13
    SitePoint Member
    Join Date
    Oct 2013
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I used the 5th edition code and I get the same error (essentially). I did find out however, that the bolded line of code I had above is what is wrong. With the 5th edition, it gives me an error:

    Fatal error: Call to a member function query() on a non-object in /home/stribbly/public_html/PHPMYSQL5-master/chapter7/admin/jokes/index.php on line 372

    I think it may be something to do with getting information from MySQL. Since I am using phpMyAdmin on a shared web server, I think it may have to do something with that, since I am not using the same programs the book uses.

  14. #14
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,813
    Mentioned
    141 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Aeip View Post
    I used the 5th edition code and I get the same error (essentially). I did find out however, that the bolded line of code I had above is what is wrong. With the 5th edition, it gives me an error:

    Fatal error: Call to a member function query() on a non-object in /home/stribbly/public_html/PHPMYSQL5-master/chapter7/admin/jokes/index.php on line 372

    I think it may be something to do with getting information from MySQL. Since I am using phpMyAdmin on a shared web server, I think it may have to do something with that, since I am not using the same programs the book uses.
    What version of PHP are you using (you need to be on 5.1 or greater)? Did you update your db.inc.php to be that from Ed 5 too? As it should look entirely different than the one you posted previously (since it is using PDO).

    Can you blank out your username and password, and zip up the chapter7 folder and attach it? I'd love to see why this isn't working for you, but I'm just at a loss without seeing the whole thing.
    Be sure to congratulate xMog on earning April's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  15. #15
    SitePoint Member
    Join Date
    Oct 2013
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Versions of what I'm using: http://imgur.com/yPR6VIE
    The code I'm using is here: https://www.dropbox.com/sh/sh8bpkzgz...ysql4-code.zip

  16. #16
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    4,904
    Mentioned
    93 Post(s)
    Tagged
    0 Thread(s)
    try the SELECT query directly against the data to check the query itself and do var_dump() on $link to check that you've got a connection to the database
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  17. #17
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,813
    Mentioned
    141 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Aeip View Post
    Versions of what I'm using: http://imgur.com/yPR6VIE
    The code I'm using is here: https://www.dropbox.com/sh/sh8bpkzgz...ysql4-code.zip
    That showed your version of phpMyAdmin, but not PHP.

    Edit:

    Side Note: I'm out of town today, so my interaction will be limited, but I'll be back tomorrow
    Be sure to congratulate xMog on earning April's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  18. #18
    SitePoint Member
    Join Date
    Oct 2013
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How do I do that?

  19. #19
    SitePoint Member
    Join Date
    Oct 2013
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    5.3.27 says my host provider support.

  20. #20
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,813
    Mentioned
    141 Post(s)
    Tagged
    0 Thread(s)
    Weird, I don't get any errors... I updated the username and password and database name to point to my MySQL server and it runs just fine...
    Be sure to congratulate xMog on earning April's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  21. #21
    SitePoint Member
    Join Date
    Oct 2013
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are you running it locally or on a shared server?

  22. #22
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,813
    Mentioned
    141 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Aeip View Post
    Are you running it locally or on a shared server?
    Locally. But that shouldn't matter. So long as the username, password, and database name are correct, it should work.

    Can you do the following:
    Create a PHP file with the following in it:
    PHP Code:
    <?php phpinfo(); ?>
    Save it, upload it, and run it. Then search for the following string on that page: "mysqli"

    If you don't find that listed anywhere, then that's your issue, your server doesn't support mysqli. Next do a search for "pdo_mysql", let me know if that is listed.

    Once you have determined if either of those are listed, delete the file from the shared server.
    Be sure to congratulate xMog on earning April's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  23. #23
    SitePoint Member
    Join Date
    Oct 2013
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Both returned results:
    mysqli: http://imgur.com/s1fHj8J and http://imgur.com/q2aOZIV
    pdo_mysql: http://imgur.com/zRWRuDI
    Did you have to paste the sql code somewhere and run it? It was not in my db, so I had to go to phpMyAdmin and paste it in there/run it.

  24. #24
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,813
    Mentioned
    141 Post(s)
    Tagged
    0 Thread(s)
    And you are still getting an error regarding the use of mysqli_query right?

    Can you verify that line 17 in db.inc.php has the correct database name in it?
    Be sure to congratulate xMog on earning April's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  25. #25
    SitePoint Member
    Join Date
    Oct 2013
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes. I used the same one as in line 2 of db.inc.php


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •