hrm. PHP question.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
neveryoumind
Forum Newbie
Posts: 4
Joined: Fri Feb 14, 2003 7:17 am
Location: Fayetteville, AR

hrm. PHP question.

Post 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?
neveryoumind
Forum Newbie
Posts: 4
Joined: Fri Feb 14, 2003 7:17 am
Location: Fayetteville, AR

well.

Post 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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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
neveryoumind
Forum Newbie
Posts: 4
Joined: Fri Feb 14, 2003 7:17 am
Location: Fayetteville, AR

nothing yet.

Post 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.
neveryoumind
Forum Newbie
Posts: 4
Joined: Fri Feb 14, 2003 7:17 am
Location: Fayetteville, AR

blah

Post 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.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
Post Reply