Assigning Variables (Newbie)

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
rgoins
Forum Newbie
Posts: 8
Joined: Thu May 11, 2006 4:09 pm

Assigning Variables (Newbie)

Post by rgoins »

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.
jcherry
Forum Newbie
Posts: 5
Joined: Thu May 11, 2006 4:04 pm

Post by jcherry »

The value of your variable would be available in $_GET["admin_id"] or $HTTP_GET_VARS["admin_id"]
rgoins
Forum Newbie
Posts: 8
Joined: Thu May 11, 2006 4:09 pm

Post by rgoins »

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
jcherry
Forum Newbie
Posts: 5
Joined: Thu May 11, 2006 4:04 pm

Post by jcherry »

Could you clarify your question?

Do you need to know how to pull the information from the database or how to get the information into the URL?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

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 />';
}
?>
rgoins
Forum Newbie
Posts: 8
Joined: Thu May 11, 2006 4:09 pm

Post by rgoins »

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" );
rgoins
Forum Newbie
Posts: 8
Joined: Thu May 11, 2006 4:09 pm

Post by rgoins »

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.
User avatar
aerodromoi
Forum Contributor
Posts: 230
Joined: Sun May 07, 2006 5:21 am

Post by aerodromoi »

rgoins wrote: $variable=admin_id (What's the proper syntax?)
How about this:

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;
aerodromoi
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

rgoins 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.
Congrats. If you could, next time, wrap your posted PHP code in either

Code: Select all

BBCode tags or the [syntax=""] tag. It makes it easier for everyone to read.
Post Reply