SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member
    Join Date
    Dec 2009
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Build your own database driven website 4th edition

    Hi forum, got to say I love your books but having a bit of a problem a problem in chapter 4 getting the jokes to display.

    This is my error message,

    Notice: Undefined variable: jokes in C:\wamp\www\code-archive\chapter4\listjokes\jokes.html.php on line 11

    Warning: Invalid argument supplied for foreach() in C:\wamp\www\code-archive\chapter4\listjokes\jokes.html.php on line 11

    I think it means the database variable isn't getting anything from the database? I'm not sure why this is as the database definitely exists, I've tried copying the code from the example code and it still doesn't work so I'm sure it has something to do with my database.

    This is my code
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    		"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    	<head>
    		<title>List of Jokes</title>
    		<meta http-equiv="content-type"
    				content="text/html; charset=utf-8"/>
    	</head>
    	<body>
    		<p>Here are all the jokes in the database:</p>
    		<?php foreach ($jokes as $joke): ?>
    			<blockquote>
    				<p><?php echo htmlspecialchars($joke, ENT_QUOTES, 'UTF-8'); ?></p>
    			</blockquote>
    		<?php endforeach; ?>
    	</body>
    </html>
    If you need me to post any more code let me know.

    Thanks in advanced,
    Josh

  2. #2
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,072
    Mentioned
    103 Post(s)
    Tagged
    0 Thread(s)
    Your're getting the error because no matches were found, so $jokes ends up being a normal variable which foreach() can't work with. Just before the line were you read the jokes returned by the query into $jokes add this line.

    PHP Code:
    $jokes=array(); 
    That will set $jokes up as an array, then if there are no jokes returned the foreach() will see an empty array.
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  3. #3
    SitePoint Member
    Join Date
    Dec 2009
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, that fixed it so it the errors don't come up, but how do I get it to display the jokes from the database? Where would I be going wrong with that?
    Thanks,

  4. #4
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,072
    Mentioned
    103 Post(s)
    Tagged
    0 Thread(s)
    Do you have any jokes stored in 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

  5. #5
    SitePoint Member
    Join Date
    Dec 2009
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, I tried to show a printscreen but it doesn't let you unless you have 5 posts, so here is a link with some spaces to allow it to work

    ht tp:/ /img32.imageshack. us/img32/6470/databaseay. jpg


    I think the name of my database and table is correct?

  6. #6
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,072
    Mentioned
    103 Post(s)
    Tagged
    0 Thread(s)
    What query are you using to get the jokes from the database?

    Is PHP connecting to MySQL ok?
    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
    SitePoint Member
    Join Date
    Dec 2009
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not sure about the query that I'm using but this is all the code I'm using. PHP does connect to MySQL ok but I've just tried to redo index.php to see the rows affected and it now says "Error performing update: Table 'ijdb.jokes' doesn't exist" So I guess that is the problem? How is it trying to find ijdb.jokes and not just ijdb? Here is all of my code

    index.php
    Code:
    <?php
    $link = mysqli_connect('localhost', 'root', 'example');
    if (!$link)
    {
    	$output = 'Unable to connect to the database server.';
    	include 'output.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, 'ijdb'))
    {
    	$output = 'Unable to locate the joke database.';
    	include 'output.html.php';
    	exit();
    }
    
    $sql = 'UPDATE jokes SET jokedate="2010-04-0"
    	WHERE joketext LIKE "%chicken%"';
    if (!mysqli_query($link, $sql))
    {
    	$output = 'Error performing update: ' . mysqli_error($link);
    	include 'output.html.php';
    	exit();
    }
    
    $output = 'Updated ' . mysqli_affected_rows($link) . ' rows.';
    include 'output.html.php';
    ?>
    jokes.html.php
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    		"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    	<head>
    		<title>List of Jokes</title>
    		<meta http-equiv="content-type"
    				content="text/html; charset=utf-8"/>
    	</head>
    	<body>
    		<p>Here are all the jokes in the database:</p>
    		<?php
    			$jokes=array(); 
    			foreach ($jokes as $joke): ?>
    			<blockquote>
    				<p><?php echo htmlspecialchars($joke, ENT_QUOTES, 'UTF-8'); ?></p>
    			</blockquote>
    		<?php endforeach; ?>
    	</body>
    </html>
    output.html.php
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    		"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    	<head>
    		<title>PHP Output</title>
    		<meta http-equiv="content-type"
    				content="text/html; charset=utf-8"/>
    	</head>
    	<body>
    		<p>
    			<?php echo $output; ?>
    		</p>
    	</body>
    </html>
    Thanks

  8. #8
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,072
    Mentioned
    103 Post(s)
    Tagged
    0 Thread(s)
    Code SQL:
    $sql = 'UPDATE jokes SET jokedate="2010-04-0"
        WHERE joketext LIKE "%chicken%"';
    Check the name of the table in the database to see if the name of the table is 'joke' and not 'jokes'
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator


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
  •