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
Can you post the code that you have in authors/index.php
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.
Okay,
-
does your database contain a table named ‘author’? If not, which table most closely resembles ‘author’?
-
Can you post your db.inc.php however, please empty out your username and password.
This is the db.inc.php contents
<?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).
Can you please post a screenshot of the authors table? Possibly one of the field names is spelt wrong in the table
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’?
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.
Interesting, the latest code archive for this book uses PDO instead of mysqli and it seems to work out of the box.
Ignore that - OPs screen shot clearly shows table is called ‘author’.
Here’s the authors table
Here is the SQL code that made these tables (The code I pasted and ran in phpMyAdmin):
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);
I am using the 4th edition. I’ll try the 5th edition code.
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.
Versions of what I’m using: http://imgur.com/yPR6VIE
The code I’m using is here: https://www.dropbox.com/sh/sh8bpkzgzwvi3ya/rRb2dEka4U/phpmysql4-code.zip
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
That showed your version of phpMyAdmin, but not PHP.
Side Note: I’m out of town today, so my interaction will be limited, but I’ll be back tomorrow
How do I do that?
5.3.27 says my host provider support.
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…