Page 1 of 1

hrm. PHP question.

Posted: Fri Feb 14, 2003 7:17 am
by neveryoumind
i'm a bit confused. i've done a slight work with PHP in the past. here is what i am trying to do.

i want to have an HTML page setup...entirely designed, all text written, except for in a few spots i want variable names to be..ie

Code: Select all

Colin Fagras has $what.
just a simple example. i don't want to pass anything to the URL through a ?, so i was hoping to store all entries in a database. my main question is....how do i make a page that pulls variable names from a database?

well.

Posted: Fri Feb 14, 2003 7:40 am
by neveryoumind
well, now i got some of it working. i realized i didnt save it as a *.php file. so can i save it as a *.php file, and pass one argument (the record number) with a ?, then use the page to access all of the elements of that record:

Code: Select all

http://www.myurl.com/script.php?name=ColinFagras
something like that for the URL? will a URL in PHP take arguments like that, and then can i use these to access my database? thanks.

Posted: Fri Feb 14, 2003 8:03 am
by volka
yes, passing parameters this way is called GET-method.
in newer versions of php (see also: http://forums.devnetwork.net/viewtopic.php?t=511) all parameters passed via GET are stored in the array $_GET. So in your example $_GET['name'] contains the string ColinFagras.
To use it in a query e.g. for mysql you can do somthing like

Code: Select all

<html><body>
Welcome, <?php
$dbHost = ...
$dbUser = ...
$dbPass = ...
$dbDbName = ...
$dbConn = mysql_connect($dbHost, $dbUser, $dbPass) or die(mysql_error());
mysql_select_db($dbDbName, $dbConn) or die('database error');
$id = mysql_escape_string($_GET[name]);
$query = "SELECT forename,name FROM tablename WHERE id='$id'";
$result = mysql_query($query, $dbConn);
if ($row = mysql_fetch_row($result))
	echo $row[0], ' ', $row[1];
else
	echo 'unknown user';
?></body></html>
php-mysql function reference: http://www.php.net/manual/en/ref.mysql.php

nothing yet.

Posted: Fri Feb 14, 2003 5:44 pm
by neveryoumind
hrm. this is what i have so far and its not working. any suggestions or a better way?

Code: Select all

<?php

$story = mysql_escape_string($_GET&#1111;story]); 

$linkID = @mysql_connect("localhost", "central", "imac411") or die(mysql_error());
mysql_select_db("news", $linkID) or die('database error'); 

$firstName = mysql_query("SELECT firstName FROM 'story' WHERE $story", $linkID);
$lastName = mysql_query("SELECT lastName FROM 'story' WHERE $story", $linkID);
$mainStreet = mysql_query("SELECT mainStreet FROM 'story' WHERE $story",$linkID);
$mainCity = mysql_query("SELECT mainCity FROM 'story' WHERE $story", $linkID);
$theGender = mysql_query("SELECT theGender FROM 'story' WHERE $story", $linkID);

mysql_close($linkID);
?>
thanks.

blah

Posted: Sat Feb 15, 2003 7:45 am
by neveryoumind
i can't get this, can anyone help? i modified my code some and ended up where sometimes it says "Resource id #2" ...

this is what i really want...to have a url like:

http://www.mywebsite.com/something/anot ... ory=981032

....i know it makes a $story variable that I can use, just by being passed as an argument. now i want to look up the $story id in a MySQL database. the database will be simple containg story id, first name, last name, city, and gender. i want to make variables out of each of these elements to be used in the text of my HTML page.

i know how to actually use them in the text, i just can't figure out how to use the passed argument and turn the values of the fields in that entry into variables. thanks.

Posted: Mon Feb 17, 2003 2:36 am
by twigletmac
Try something along the lines of:

Code: Select all

<?php 

if (!empty($_GET['story'])) {
	$story = mysql_escape_string($_GET['story']);

	@$linkID = mysql_connect('localhost', 'central', 'imac411') or die(mysql_error()); 
	@mysql_select_db('news', $linkID) or die('database error'); 

	$sql = "SELECT firstName, lastName, mainStreet, mainCity, theGender FROM story WHERE story = '$story'";
	$result = mysql_query($sql) or die(mysql_error().'<p>'.$sql.'</p>');

	if (mysql_num_rows($result) > 0) {
		$row = mysql_fetch_assoc($result);
		$firstName = $row['firstName'];
		$lastName = $row['lastName'];
		$mainStreet = $row['mainStreet'];
		$mainCity = $row['mainCity'];
		$theGender = $row['theGender'];
	} else {
		echo '<p>That story is not in the database.</p>';
	}
} else {
	echo '<p>No story selected.</p>';
}
?>
For more info:
http://www.php.net/manual/en/ref.mysql.php

Mac