...

View Full Version : PHP $_GET function



jchrisphonte
01-31-2012, 10:52 PM
Hello CF,

Looking to populate a details page with dynamic data from my sql table..

The link that is going to be clicked would look something like this:

http://www.hedgevent.com/eventdetails.php/Distressed_Investing_Summit?id=124

what code would I need to add to my eventdetails.php page to tell it to get the id# in the url and use that number to reference data in my calendar_event table using the same id?

>ssp-cdr<
01-31-2012, 10:55 PM
(int) $_GET['id']

You'll want to cast it as an integer to make sure you are getting a number and to protect against MySQL injection hacks.

Perhaps use it in a condition like this -

if ((int) $_GET['id']) mysql_query('SELECT * FROM calendar_event WHERE id = '.(int) $_GET['id'])

mlseim
01-31-2012, 10:55 PM
<?php

$myid=$_GET['id];
echo $myid;

// now you can use $myid anywhere you want,
// in your example, $myid would be equal to 124.
?>



.

jchrisphonte
01-31-2012, 11:02 PM
Great!! thanks mlseim.

How do I tell the same php page to connect to my database(hedgevent) and table(calendar_event) and only use the data there for id #124 to fill the echo fields?? ex fields : 'event', 'location', 'city' etc....

mlseim
01-31-2012, 11:06 PM
wow ...
There's a lot to do for that.

You should view an online tutorial on: PHP MySQL INSERT

Too much code for me to provide here.

jchrisphonte
01-31-2012, 11:12 PM
sorry for being so vague... I guess my question is more about the linking of the id with the table.

assuming I use this code to connect and select data:


<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("my_db", $con);

$result = mysql_query("SELECT * FROM Calendar_event");

while($row = mysql_fetch_array($result))
{
echo $row['FirstName'] . " " . $row['LastName'];
echo "<br />";
}

mysql_close($con);
?>

how would I tell it to check the id i found in the url and only use that rows info?

>ssp-cdr<
01-31-2012, 11:18 PM
look at my suggestion above

jchrisphonte
01-31-2012, 11:23 PM
thanks ssp for the help -- It did look right...

just wasn't sure where to include it in the script I have above...


would it look like this??



<?php
(int) $_GET['id'];

if ((int) $_GET['id']) mysql_query('SELECT * FROM calendar_event WHERE id = '.(int) $_GET['id']);

$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("my_db", $con);

$result = mysql_query("SELECT * FROM Calendar_event");

while($row = mysql_fetch_array($result))
{
echo $row['FirstName'] . " " . $row['LastName'];
echo "<br />";
}

mysql_close($con);
?>

BluePanther
02-01-2012, 12:01 AM
thanks ssp for the help -- It did look right...

just wasn't sure where to include it in the script I have above...


would it look like this??



<?php
(int) $_GET['id'];

if ((int) $_GET['id']) mysql_query('SELECT * FROM calendar_event WHERE id = '.(int) $_GET['id']);

$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("my_db", $con);

$result = mysql_query("SELECT * FROM Calendar_event");

while($row = mysql_fetch_array($result))
{
echo $row['FirstName'] . " " . $row['LastName'];
echo "<br />";
}

mysql_close($con);
?>



No.


// Get ID from your GET value, casting as integer
$id = (int) $_GET['id'];

// Mysql connection stuff here

// Use that to SELECT from the database the fields you require
$result = mysql_query('SELECT some,fields FROM Calendar_event WHERE id='.$id);
if(!$result){
// Do some error reporting here, with a die or exit maybe. Example
die(mysql_error());
}
// Seeing as script will be killed if the mysql failed, ok to assume mysql OK if down here

// Loop through results row by row
while($row = mysql_fetch_assoc($result)){
// Do something with your selected rows. In this case, $row['some'] and $row['fields']
}

All of this could have been found by a google search, and a simple php mysql tutorial like mlseim said



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum