Basically, I am working on a joke site that allows users to submit jokes. When displaying the jokes, I want to use a function called joke_author, which will automatically find the authors username based on the authorid of the joke.
in both my users table and jokes table, the user id is authorid. so every new user is assigned an authorid.
anyways, here's my code for the function (in separate functions.inc.php).

function joke_author($jokeid) {
	$authorid = mysql_query("SELECT authorid FROM jokes WHERE id=$jokeid");
	$author = mysql_query("SELECT username FROM users WHERE authorid=$authorid");
	echo "<a href=\"profile.php?id=" . $authorid . "\">" . $author . "</a>";
	}

and here's the PHP executed for displaying a joke. I know it's rough, trying to make everything into a function...

<?php
				$jokes = mysql_query("SELECT * FROM jokes");
				while($row = mysql_fetch_array($jokes)) {
					echo "<div class=\"joke\"><a href=\"#\" class=\"rating\">";
					$jokeid = $row['id'];
					echo vote_count($jokeid);
					print $row ['joke'];
					echo "<div class=\"meta\">";
						echo joke_author($jokeid);
						echo " | Today at 7:56PM | ";
					echo "<a href=\"joke.php?jid=" . $jokeid . "\">";
					echo comment_count($jokeid);
					echo "</a></div></div>";
					}
			?>

I just want to know why it doesn't display and how to fix it. If you could, explain some of te steps. I keep going to a live support and they just say "run this query..." and IDK wtf to do next :(

Member Avatar for diafol

You don't need the function, just modify your jokes SQL:

$jokes = mysql_query("SELECT jokes.*, users.username FROM jokes INNER JOIN users ON users.authorid = jokes.authorid");

Your code also shows a space:

print $row ['joke'];

Make it

print $row['joke'];

Great Idea, and as for the space, it does not make a difference; it displays the joke either way. I am doing the functions for the site to make t more flexible for expansion. I will get back when I got things working.

it works, late reply. answered

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.