Jump to content

Displaying row if listing exists on different table


Sevian

Recommended Posts

Hey, I am still learning PHP and all it's functions. So some of the coding may not be clean. But here is the script I made that I am having problems with:

<?php
// Adjust MySQL connection settings...
$username="newestfu_Dan";
$password = "camaro"; 
$hostname = "localhost"; 
$database = "newestfu_wowtrader";      

// Connect to MySQL...
$conn = mysql_connect($hostname, $username, $password)      or die("Connecting to MySQL failed");  
mysql_select_db($database, $conn)     or die("Selecting MySQL database failed"); 

// Run our query, see if session username exists in session field...
$sql="select username,email from user where username='{$_SESSION['user']}' limit 1";
$result=mysql_query($sql,$conn);

// Parse our results into $data (as an associative array)...
$data=mysql_fetch_assoc($result);

// If one row was found in the result set, username exists...
if (mysql_num_rows($result)==1) {
print "Welcome, {$data['username']}, your current email on file is: {$data['email']}";
echo "<br />";
$query="select seller,id,title from listings";
$queryresult=mysql_query($query,$conn);
$info=mysql_fetch_assoc($queryresult);
if ( $info['seller'] == $data['username'] ) {
echo "{$info['title']}";
}
else {
echo "You have no current auctions";
}  
// Otherwise...
}
else {
print "Sorry, the username {$_SESSION['username']} was not found in our database...";
}
?>
</body>
</html>

 

There are two queries going on. The first selects from the table "user" and gets the username depending on the user session and email. If one exists, it will go onto the next query to select from the table "listings" and gets seller, title, and id. It will then check if the username is equal to the seller. If it is, it will display the title. If not it displays otherwise. The problem is if the user has more than one title listed. But only 1 shows up. I'm guessing the line that has to change is

echo "{$info['title']}";

But I'm not to sure on what to change it to.

Thanks all for your help!

Link to comment
Share on other sites

Actually the problem is here:

$info=mysql_fetch_assoc($queryresult);

 

Replace everything from $query to //otherwise with the following.

 

$query='select seller,id,title from listings where seller = "'.$data['username'].'"';
$queryresult=mysql_query($query,$conn);
while($info = mysql_fetch_assoc($queryresult)){
echo "{$info['title']}";
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.