Jump to content

Simple mysql and php


Godoploid_Design

Recommended Posts

I have a site where I built two tables that house user information and their story information. The site is kind of like a blog in that people can post their own own stories about their relationships. Basically the story table has all the stories information like id, title, body etc and the posting persons id. The second table has the users info such as id first name, last name, etc. I have a story.php which is dynamic in that it grabs information based on the url so if there is story 2 the link would be story.php?sid=2. So what happens is the information is pulled based on that sid and in turn the posting users id is pulled via a while loop where i create variables for every column in the table. What I want is to be able to run basically two while loops on inside the other in order to get information based on the initial while loop column uid (users id). I don't know if this is clear but this is what I've build:

 

function story() {

// connection information
include 'includes/connection.php';

$sid = "23";

// mysql connection
mysql_connect("$host", "$user", "$pass") or die(mysql_error());
mysql_select_db("$dbname") or die(mysql_error()); 

// grabs all story information
$squery = ("SELECT * FROM $stable WHERE sid = '$sid'");
$sresult = mysql_query($squery) or die(mysql_error());
$fullstory = '';

while($srow = mysql_fetch_array($sresult)){		

// grab variables for the story
$ssid = $srow['sid']; // stories id
$title = $srow['title']; 
$body = $srow['body'];
$keywords = $srow['keywords'];
$suid = $srow['uid']; // the posting users id
$sdate = $srow['date'];
$accusedfname = $srow['accusedfname'];
$accusedlname = $srow['accusedlname'];
$location = $srow['location'];
$accusedfacebook = $srow['accusedfacebook'];
$accusedmyspace = $srow['accusedmyspace'];
$accusedimage = $srow['accusedimage'];

	// mysql connection
	mysql_connect("$host", "$user", "$pass") or die(mysql_error());
	mysql_select_db("$dbname") or die(mysql_error());	

	// grabs all information on the posting user
	$uquery = ("SELECT * FROM $utable WHERE uid = '$suid'");
	$uresult = mysql_query($uquery) or die(mysql_error());

	// grabs the posting users information
	while($urow = mysql_fetch_array($uresult)){		
		$uid = $urow['uid'];
		$fname = $urow['fname'];
		$lname = $urow['lname'];
		$image = $urow['image'];

		$fullstory .= '
		<div id="story-wrap">
			<div class="story-image">
			<p>
			The Accused: '.$accusedfname.' '.$accusedlname.'<br/>
			<a target="_blank" href="'.$accusedimage.'"><img src="'.$accusedimage.'" alt="'.$accusedfname.' '.$accusedlname.'" title="'.$accusedfname.' '.$accusedlname.'" /></a>
			</p>
			<p>
			The Accuser: <a href="user-profile.php?uid='.$uid.'">'.$fname.' '.$lname.'</a><br/>
			<a target="_blank" href="'.$image.'"><img src="'.$image.'" alt="'.$fname.' '.$lname.'" title="'.$fname.' '.$lname.'" /></a>		
			</p>
			</div>

			<div class="story-copy">
			<h3><a href="user-profile.php?uid='.$uid.'">'.$fname.' '.$lname.'</a> wrote: '.$title.'</h3>
			<p>'.$body.'</p>
			<p>Posted on: '.$sdate.'<br/>
			The accused"s <a href="'.$accusedfacebook.'">Facebook</a><br/>
			The accused"s <a href="'.$accusedmyspace.'">Myspace</a><br/>
			The accused"s <a href="'.$location.'">'.$location.'</a>
			</div>		
		</div>
		';
	}
}
return $fullstory;
}

 

When I run this I get some errors, I tried tweaking the code ending the loops once the variables had been built then just echoing the information out after but that just wouldn't echo anything out and what I got was a blank screen. If anyone can help it would be greatly appreciated! Thanks

Link to comment
Share on other sites

While you are trying to give us some more information.

 

what I got was a blank screen.

TOP OF SCRIPT

ini_set ('display_errors', 1);
error_reporting (E_ALL);

 

EDIT, don't nest while loops of MySQL data.  Pull everything you want with a JOIN query.  Otherwise, you are liable to run into problems.

Link to comment
Share on other sites

I'm sorry, I don't understand what you mean at all.. could you maybe try and explain expected output, as your explanation doesn't make any sense, sorry.

 

Ok so basically I have two tables the story which houses all story information plus the posting users id "uid" and then I have a table with user info. What I want is to run one query to show the story then grab the posting users information via the story tables posting user id column. That way whatever image or name they have on file will always update everytime the page is corrected.

 

The only reason I didn't try a join is because I need to first get the uid (users id) that posted the story in order to select the proper user

 

If this still doesn't make sense let me know!

Link to comment
Share on other sites

It is hard to write a query, when you don't know the database tables, or how you know what story you are posting.

 

$sql = "SELECT a.*,b.* FROM story AS a JOIN user AS b ON a.userid = b.userid WHERE storyid = '$id'";

 

You shouldn't need to know what the userid is.  The story table knows what the userid is, since it should be stored in a column.  Just join that column to the userid column in the user table, MySQL will do the rest.

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.