Page 1 of 1

PHP Question...

PostPosted: Wed Nov 25, 2009 4:13 pm
by David de Argentina
Hi all,

I don't know much about PHP.

I'm trying to do a process that connect to a database, add 1 to a record, and get one field value of one record as link.

I did this code

Code: Select all
<?
$user_ID = $_REQUEST['user_ID'];

$link = mysql_connect('localhost', 'My_UserID', 'My_Pass') or die('Could not connect: ' . mysql_error());

mysql_select_db('My_Database') or die('Could not select database');

$query = "UPDATE My_Table SET Acum=Acum+1 WHERE Id = $user_ID";

$result = mysql_query($query) or die('Query failed: ' . mysql_error());

$query = "SELECT Redir FROM My_Table where Id = $user_ID";

$result = mysql_query($query) or die('Query failed: ' . mysql_error());

while ($line = mysql_fetch_array($result, MYSQL_ASSOC))
 { foreach ($line as $col_value)
 {     $Enlace = "$col_value"; }
}

mysql_free_result($result);
mysql_close($link);
header("location:$Enlace");
?>


Where:
user_ID is the ID or the record
Redir is the field who has the link where the webbrowser must be redirected.
$Enlace is the variable to store the redir value

In fact, this function works, but I think do a loop to catch an only one value... is not ellegant...

Does any know a better way to do this ?

Thanks in advance,
David de Argentina

PostPosted: Thu Nov 26, 2009 12:21 pm
by smartmedia
Hi..

You can limit the results of your SELECT if you are sure that this will return only 1 user. So, then you don't need the while, just pass the result to your variable..

PostPosted: Fri Nov 27, 2009 8:51 am
by bunker
Hi, there is the code.
Code: Select all
<?
$user_ID = $_REQUEST['user_ID'];

$link = mysql_connect('localhost', 'My_UserID', 'My_Pass') or die('Could not connect: ' . mysql_error());

mysql_select_db('My_Database') or die('Could not select database');

$query = "UPDATE My_Table SET Acum=Acum+1 WHERE Id = $user_ID";

$result = mysql_query($query) or die('Query failed: ' . mysql_error());

$query = "SELECT Redir FROM My_Table where Id = $user_ID";

$result = mysql_query($query) or die('Query failed: ' . mysql_error());

$Enlace = mysql_result($result,0,0);

mysql_free_result($result);
mysql_close($link);
header("location:$Enlace");
?>


Greetings

PostPosted: Mon Dec 14, 2009 8:34 pm
by Ferdari
i have a program made with NeoBook, it works with php and mysql see it working at www.eulerion.com , any question about a function ask.

Saludos! :lol:

PostPosted: Thu Dec 17, 2009 1:44 pm
by David de Argentina
Thanks SmartMedia & Bunker

I discovered the function mysql_fetch_row().

It is exactly I need.

Greetings from Buenos Aires,
David de Argentina