SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Jan 2007
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP connecting with SQL problem(s)

    Hi,

    I have been reading the book from sitepoint "Database driven website using PHP and MySQL" and have a few problems. I have managed to get text to appear on a php document from an sql table, though I am having trouble when trying to get informaiton to display when using two tables.

    Basically at the moment i have one table, "jokes", consisting of the columns "ID" "JokeText" "JokeDate" and "AID". The other table is called "authors" which has the columns "ID" "Name" and "EMail".

    The main goal is to display a list of the jokes with the authors name and email. Though the code I copyied from the book doesnt seem to be working because i just get a blank page.

    Here is the code i have done:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    </head>
    
    <body>
    <?
    
    $jokelist = mysql_query("SELECT JokeText, AID FROM jokes");
    	
    	//get the author details for the joke
    	while ($joke = mysql_fetch_array($jokelist)) {
    		$joketext = $joke["JokeText"];
    		$aid = $joke["AID"];
    		
    		//get the author details for the joke
    		$authordetails = mysql_query("SELECT Name, EMail from authors WHERE ID=$aid");
    			$author = mysql_fetch_array($authordetails);
    			$name = $author["Name"];
    			$email = $author["EMail"];
    					
    		//Display the joke with author information
    		
    		echo( "<p>$joketext<br></br>" .
    			"(by <a href='mailto:$email'>$name</a>)</p>" );
    			
    			}
    
    ?>
    
    </body>
    </html>
    Any help would be much appreciated.

    Thanks

  2. #2
    SitePoint Wizard simplecode's Avatar
    Join Date
    Jun 2006
    Posts
    1,129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $no mysql_num_rows($authordetails);
    echo 
    $no

  3. #3
    SitePoint Member
    Join Date
    Jan 2007
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So what will this do? Or where should I put it?

    Sorry, i'm a complete newbie to PHP/MySQL

    Thanks for your help

  4. #4
    SitePoint Addict Procode's Avatar
    Join Date
    Dec 2006
    Location
    New York
    Posts
    371
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Give this a shot.

    PHP Code:
    <?php

    $jokelist 
    mysql_query("SELECT JokeText, AID FROM jokes");
        
        
    //get the author details for the joke
        
    while ($joke mysql_fetch_assoc($jokelist)) {
            
    $joketext $joke["JokeText"];
            
    $aid $joke["AID"];
            
            
    //get the author details for the joke
            
    $authordetails mysql_query("SELECT Name, EMail from authors WHERE ID=$aid");
                
    $author mysql_fetch_assoc($authordetails);
                
    $name $author["Name"];
                
    $email $author["EMail"];
                        
            
    //Display the joke with author information
            
            
    echo( "<p>$joketext<br></br>" .
                
    "(by <a href='mailto:$email'>$name</a>)</p>" );
                
                }

    ?>

  5. #5
    SitePoint Member
    Join Date
    Jan 2007
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Unfortunatly i'm still getting a blank screen

  6. #6
    SitePoint Member
    Join Date
    Jan 2007
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Lol, guys I got it working. It was a real n00by error :/ sigh. Looking back at my old scripts i neglected to add the connect to database information.

    Oh well, lesson learned.

    Thanks anyway guys :P

  7. #7
    SitePoint Wizard simplecode's Avatar
    Join Date
    Jun 2006
    Posts
    1,129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thats why am telling to u to see no. of rows u r getting if it is 0 then it means no result are coming so no o/p on screen http://us3.php.net/mysql_num_rows

    anyways its good to hear that ur problem is solved


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
  •