my first php/mysql site

Permabanned
Joined
19 Apr 2006
Posts
2,333
Location
West Yorkshire
I am just starting on learning php and mysql to create a site and have fallen at the first hurdle:

Code:
	$user = "root"
        $password = "password"
        $dbase = "main";
	$dbcnx = @mysql_connect('localhost', '$user', '$password');
	if (!$dbcnx) {
		die( '<p> Db Connection Error </p>');
	}
?>

All I get with this is the "DB connection error" any tell me why this might be or if there is some way of getting more info on why it is?
 
thanks rob ! :)

Am picking it up quite quickly. used to do lots of mIRC scripting and the way this is structured is similar which helps.

However... I am not having problems with this:

Code:
	$rid="1";
	
	$query2 ="SELECT region FROM tbl_regions WHERE RID = $rid";
	$rname = mysql_query($query2);

	mysql_close();

	echo "<b><center>Region: $rname</center></b><br><br>";

What I get outputted is: Region: Resource id #3

why's that? I want it to say the name of the region.
 
Cheers Paul, worked a treat.

Now for my next trick I would really like the region "$rid" to be selectable from a drop down list.

Any pointers?
 
Ok so a little edit later and it all works :)

However now how do I make it so when someone selects the branch in the combo box it reloads the page with the correct details?

Code:
<select name="rid_combo" size="1" id="RID">
<?php
$query = "SELECT RID, region FROM tbl_regions";
$result = mysql_query($query);

while($row = mysql_fetch_assoc($result))
{
$RID = $row["RID"];
$region = $row["region"];
?>
<option value="<?php echo $RID; ?>"><?php echo htmlentities($region, ENT_QUOTES); ?></option>
<?php
}//End while
?>
</select>
 
ok I dont think I am a million miles away, however I just cannot get it going.

Code:
		<form action="allregions.php?regionid=$rid" $rid method="post">
	<select name="rid_combo" size="1" id="RID">  
<?php
	$query = "SELECT RID, region FROM tbl_regions";
	$result = mysql_query($query);

	while($row = mysql_fetch_assoc($result))
		{
			$RID = $row["RID"];
			$region = $row["region"];
			?>
			
			<option value="<?php echo $RID; ?>"><?php echo htmlentities($region, ENT_QUOTES); ?></option>
			
			<?php
		} //End while
?>

	</select>

	<input type="submit" value="Update" />
	</form>

The way I see it working is that when Submit is clicked, the page is reloaded with a RID in the url, which can then be picked up by the main page and used to select which region the user is looking at.

But it doesnt it reloads a page with allregions.php?regionid='$rid' as the url instead of the actual region ID :(
 
nice one gents you have helped me immensely :)

full Code:

Code:
<?php
 	include('connect.php'); //Connect to the database

	if(isset($_GET["rid_combo"]))
		{
			$rid = $_GET["rid_combo"];
			$regionquery ="SELECT * FROM tbl_regions WHERE RID = $rid";
			$regionresult = mysql_query($regionquery);
			$regionrows = mysql_numrows($regionresult);
			$rname = mysql_result($regionresult,0,1);
	
			echo "<b><center>Region: $rname</center></b><br><br>"; //Display Region name

			$query ="SELECT * FROM tbl_addresses WHERE RID = $rid";
			$result=mysql_query($query);
			$num=mysql_numrows($result);

			mysql_close(); // Close connection to the db as this stops problems with too many connections.


			// Create Name Anchors for each of the branch names within the region
			echo "<center>";
			$i=0;
			while ($i < $num) {
				$bname=mysql_result($result,$i,"a_name");
		
				echo "<a href='#$bname'>$bname</a> // ";
				$i++;
			}		
			echo "</center><hr><br><br>";
	
			//Display all the region info with an anchor for each name.
	
			$i=0;
			while ($i < $num) {
				$bname=mysql_result($result,$i,"a_name");
				$baddress=mysql_result($result,$i,"address");
				$bpost_code=mysql_result($result,$i,"post_code");
				$bfront_line=mysql_result($result,$i,"front_line");
				$bback_line=mysql_result($result,$i,"back_line");
				$bmobile=mysql_result($result,$i,"mobile");
				$bfax=mysql_result($result,$i,"fax");
				$bsite_id=mysql_result($result,$i,"site_id");
				$bemail=mysql_result($result,$i,"email");
		
				echo "<b><a name='$bname'>Site: $bname </a></b><br>Address: $baddress , $bpost_code<br>Front Line: $bfront_line<br>Back Line: $bback_line<br>Mobile: $bmobile<br>Fax: $bfax<br>Email: $bemail<br>Site ID: $bsite_id<br><hr><br>";

				$i++;
			}
		}
	else
		{
 			//Beginning of combo box to select region name -->
			?> <form action="allregions.php?regionid=RID" method="get" target="_self">
			<select name="rid_combo" size="1" id="RID">  
			<?php
			$query = "SELECT RID, region FROM tbl_regions";
			$result = mysql_query($query);

			while($row = mysql_fetch_assoc($result))
				{
					$RID = $row["RID"];
					$region = $row["region"];
			
					?>
			
					<option value="<?php echo $RID; ?>" selected><?php echo htmlentities($region, ENT_QUOTES); ?></option>

					<?php

				} //End while
	?>

	</select>

	<input type="submit" value="Update" />
	</form>
		<!-- End of combo box to select region name -->
<?php

		}
	
?>
 
ok have moved on quite a bit since this.

Have now got allt he display functions working, have made it so I can add branches to the database, with a selectable region box. I have even done my first array and sorted the branch names out alphabetically \o/. ( I know that is prolly easy, but man it hurt my head).

The problem I have now is on my display page which shows the branch details, I would like them also sorting in to alphabetical order.

Any ideas how I would do this?

THis is the code I have for displaying the branch details:

Code:
			$i=0;
			while ($i < $num) {
				$bname=mysql_result($result,$i,"a_name");
				$baddress=mysql_result($result,$i,"address");
				$bpost_code=mysql_result($result,$i,"post_code");
				$bfront_line=mysql_result($result,$i,"front_line");
				$bback_line=mysql_result($result,$i,"back_line");
				$bmobile=mysql_result($result,$i,"mobile");
				$bfax=mysql_result($result,$i,"fax");
				$bsite_id=mysql_result($result,$i,"site_id");
				$bemail=mysql_result($result,$i,"email");
		
				echo "<b><a name='$bname'>Site: $bname </a></b><br>Address: $baddress , $bpost_code<br>Front Line: $bfront_line<br>Back Line: $bback_line<br>Mobile: $bmobile<br>Fax: $bfax<br>Email: $bemail<br>Site ID: $bsite_id<br><hr><br>";

				$i++;
			}

but that displays them in the order they were added in to the db which is no good.

How do I sort it into alphabetical order?
 
OMG!!!

YOu have no idea how long it just took me to write a sort array to do that same job lol!!!

Thanks BeanSprout
 
ok I dont have my books with me at home, but am quite addicted to learning this stuff so.....

I have this line:

Code:
echo "<b><a name='$bname'>Site: $bname </a></b><br>Address: $baddress , $bpost_code<br>Front Line: $bfront_line<br>Back Line: $bback_line<br>Mobile: $bmobile<br>Fax: $bfax<br>Email: <a href='mailto:$bemail'>$bemail</a><br>Site ID: $bsite_id<br><hr><br>";

Which basically I want the post code to be a link to the multimap site with the same post code.

Like LS42JE.

However no matter what I try it wont work

Anyone help?
 
Back
Top Bottom