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?
 
The_KiD said:
Code:
	$user = "root"
        $password = "password"
        $dbase = "main";
	$dbcnx = @mysql_connect("localhost", $user, $password);
	if (!$dbcnx) {
		die( '<p> Db Connection Error </p>');
	}
?>
I think it should be like I've edited....
 
Code:
	$user = "root"
        $password = "password"
        $dbase = "main";
	$dbcnx = @mysql_connect("localhost", $user, $password) or die(mysql_error());
	
?>

maybe quotes around mysql_error() but i don't think so.
 
FYI, it wasn't the 'localhost' to "localhost", it was the single quotes around your $user and $password variables.

Variables are not parsed in single quoted strings, but they are in double-quoted strings. So:

Code:
$foo = "hello";

echo '$foo world'; // prints `$foo world`
echo "$foo world"; // prints `hello world`

Since you're just passing the variables themselves to mysql_connect, you don't need to quote them at all; this will suffice:

Code:
$host = 'localhost';
$user = 'root';
$password = '';

mysql_connect($host, $user, $password);
 
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.
 
Code:
$rid="1";

$query2 ="SELECT region FROM tbl_regions WHERE RID = $rid";
$result2 = mysql_query($query2);

//If you want a single result you can just do
$rname = mysql_result($result2,0,0);
//Which is the first row and column result

//Or you could do
$row2 = mysql_fetch_assoc($result2);
$rname = $row2["region"];
//Which is the first rows value for the column "region"

echo "<b><center>Region: $rname</center></b><br><br>";
 
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?
 
Something like

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

while($row = mysql_fetch_assoc($result))
{
$region_id = $row["region_id"];
$region = $row["region"];
?>
<option value="<?php echo $region_id; ?>"><?php echo htmlentities($region, ENT_QUOTES); ?></option>
<?php
}//End while
?>
</select>
 
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 :(
 
Look again...you've just typed $rid as part of the HTML :)

Edit: Hangon, it's wrong anyway...presumably you want to use the value selected from the drop-down as the page? No time to read the thread I'm afraid....going out...
 
Ok - you need to use a form - hopefully you can set that up (halfway there anyway) - then to get the values sent by the form you use the following:

Code:
<?
echo $_GET['rid_combo'];
?>

The variable from the form (the <option> tag named rid_combo) is placed into the $_GET[] array, which contains all the variables from a URL in the form foo.php?variable=value, ie foo.php?rid_combo=something :)

One thing to note...if you want to have the form create a URL like that, change the method to 'get' - otherwise, if you don't want the value of the form sent in the URL, leave it as post and change $_GET to $_POST :)
 
You're using POST for your form method not GET, you use GET if you want the form variables to be appended to the end of the address.

POST is a better method, but basically you just want to POST to the allregions.php page and then on there put:

Code:
<?php
if(isset($_POST["rid_combo"]))
{
$RID = $_POST["rid_combo"];
//Pull out the data related to the id

}
else
{
//Show the form
}
?>
 
Last edited:
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

		}
	
?>
 
Back
Top Bottom