Assigning Variables (Newbie)
Moderator: General Moderators
Assigning Variables (Newbie)
I'm new to PHP and I'm using MySQL along with PHP. I am running a select statement and I want to assign one of the values in my select statement to a variable so that I can pass that variable through a url. For example:
IN my table administrator I have a column called admin_id. I want to assign the value of the column to a variable ($admin_id) and pass it through a URL. The code I have is below.
<?php
include("dbinfo.inc.php");
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$admin_username = $HTTP_POST_VARS['admin_username'];
$admin_password = $HTTP_POST_VARS['admin_password'];
$qstr = "SELECT * from administrator where username ='$admin_username' and password ='$admin_password'";
$result = mysql_query($qstr);
// check login info is correct
if (mysql_num_rows($result)) header( "Location: http://www.softedgedesign.com/admin_mai ... =$admin_id" );
else header( "Location: http://www.softedgedesign.com/login_failed.htm" );
mysql_close();
?>
Once I have accomplished passing the variable I want to be able to retrieve that variable and utilize it in additional sql statements. Does any of this make sense?
Thanks for the input.
IN my table administrator I have a column called admin_id. I want to assign the value of the column to a variable ($admin_id) and pass it through a URL. The code I have is below.
<?php
include("dbinfo.inc.php");
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$admin_username = $HTTP_POST_VARS['admin_username'];
$admin_password = $HTTP_POST_VARS['admin_password'];
$qstr = "SELECT * from administrator where username ='$admin_username' and password ='$admin_password'";
$result = mysql_query($qstr);
// check login info is correct
if (mysql_num_rows($result)) header( "Location: http://www.softedgedesign.com/admin_mai ... =$admin_id" );
else header( "Location: http://www.softedgedesign.com/login_failed.htm" );
mysql_close();
?>
Once I have accomplished passing the variable I want to be able to retrieve that variable and utilize it in additional sql statements. Does any of this make sense?
Thanks for the input.
I understand that my value would be in $_GET['admin_id'] on the page it is passed to...correct?
For example:
select * from administrator where usernamer= '$username' and password = '$password';
My result would have a row with a value of 1407 in column admin_id. I need to assign 1407 to a variable so that I can pass that variable through a url.
my code would look something like http://www.page1.html/admin.php?admin_id='$admin_id' but the url displayed would actually be:
- http://www.page1.html/admin.php?admin_id=1407
For example:
select * from administrator where usernamer= '$username' and password = '$password';
My result would have a row with a value of 1407 in column admin_id. I need to assign 1407 to a variable so that I can pass that variable through a url.
my code would look something like http://www.page1.html/admin.php?admin_id='$admin_id' but the url displayed would actually be:
- http://www.page1.html/admin.php?admin_id=1407
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
It would be something along the lines of this...
Code: Select all
<?php
$is_single_user = false; // Change to true for a single user and supply the id
$sql = "SELECT user_id, user_name FROM users";
if ($is_single_user) {
$sql .= " WHERE user_id = $user_supplied_id"; // notice the opening space, you need to supply the id
}
if (!$result = mysql_query($sql)) {
die("Could not get the user information: " . mysql_error());
}
while ($row = mysql_fetch_array($result)) {
echo '<a href="admin_page.php?user=' . $row['user_id'] . '">User info for ' . $row['user_name'] . '</a><br />';
}
?>I apologize for not being very clear. I really need to know how to do both. I know how to run SQL statements and that sort of thing but when you run a select statement you expect results, which I have no problem getting. My problem is I want to assign the results to individual variables and pass the individual variable through a URL. I'll try one more example.
$qstr = "SELECT admin_id from administrator where username ='$admin_username' and password ='$admin_password'";
NOW...how do I assign admin_id to a variable?
$variable=admin_id (What's the proper syntax?)
When I have accomplished this, I would like to pass $variable through a URL.
header( "Location: http://www.softedgedesign.com/admin_mai ... =$variable" );
$qstr = "SELECT admin_id from administrator where username ='$admin_username' and password ='$admin_password'";
NOW...how do I assign admin_id to a variable?
$variable=admin_id (What's the proper syntax?)
When I have accomplished this, I would like to pass $variable through a URL.
header( "Location: http://www.softedgedesign.com/admin_mai ... =$variable" );
And the choir sings....Hallelujah..Hallelujah..Hallelujah
I figured it out.
MY PHP CODE:
--------------------------------------------------------------------
<?php
include("dbinfo.inc.php");
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$admin_username = $HTTP_POST_VARS['admin_username'];
$admin_password = $HTTP_POST_VARS['admin_password'];
$qstr = "SELECT * from administrator where username ='$admin_username' and password ='$admin_password'";
$result = mysql_query($qstr);
$row = mysql_fetch_assoc($result);
$id=$row["admin_id"];
mysql_close();
if (mysql_num_rows($result)) header( "Location: http://www.softedgedesign.com/admin_main.php?id=$id" );
else header( "Location: http://www.softedgedesign.com/login_failed.htm" );
?>
----------------------------------------------------------------------------
Thank you all for your assistance.
I figured it out.
MY PHP CODE:
--------------------------------------------------------------------
<?php
include("dbinfo.inc.php");
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$admin_username = $HTTP_POST_VARS['admin_username'];
$admin_password = $HTTP_POST_VARS['admin_password'];
$qstr = "SELECT * from administrator where username ='$admin_username' and password ='$admin_password'";
$result = mysql_query($qstr);
$row = mysql_fetch_assoc($result);
$id=$row["admin_id"];
mysql_close();
if (mysql_num_rows($result)) header( "Location: http://www.softedgedesign.com/admin_main.php?id=$id" );
else header( "Location: http://www.softedgedesign.com/login_failed.htm" );
?>
----------------------------------------------------------------------------
Thank you all for your assistance.
- aerodromoi
- Forum Contributor
- Posts: 230
- Joined: Sun May 07, 2006 5:21 am
How about this:rgoins wrote: $variable=admin_id (What's the proper syntax?)
Code: Select all
$query = "SELECT admin_id FROM administrator WHERE username='$admin_username'";
$sql = mysql_query($query) or die(mysql_error());
while ($ds = mysql_fetch_object($sql)){
$admin_id = $ds -> admin_id;
}
echo $admin_id;- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
Congrats. If you could, next time, wrap your posted PHP code in eitherrgoins wrote:And the choir sings....Hallelujah..Hallelujah..Hallelujah
I figured it out.
MY PHP CODE:
------------------------------------------------------------------------------------------------------------------------------------------------Code: Select all
<?php include("dbinfo.inc.php"); mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $admin_username = $HTTP_POST_VARS['admin_username']; $admin_password = $HTTP_POST_VARS['admin_password']; $qstr = "SELECT * from administrator where username ='$admin_username' and password ='$admin_password'"; $result = mysql_query($qstr); $row = mysql_fetch_assoc($result); $id=$row["admin_id"]; mysql_close(); if (mysql_num_rows($result)) header( "Location: http://www.softedgedesign.com/admin_main.php?id=$id" ); else header( "Location: http://www.softedgedesign.com/login_failed.htm" ); ?>
Thank you all for your assistance.
Code: Select all
BBCode tags or the [syntax=""] tag. It makes it easier for everyone to read.