taking 2 values with one query?
Moderator: General Moderators
taking 2 values with one query?
at present i have 2 queries in a script
//extract email address
$query = "select email from user where email='$mailpass'";
$result = mysql_query($query, $db_conn) or die('query failed');
//extract password
$query2 = "select passwd from user where email='$mailpass'";
$result2 = mysql_query($query2, $db_conn) or die('query failed');
then i've taken one into a variable
$pass = mysql_result($result2, 'passwd');
is it possible to combine the 2 queries and draw out what i want to make the variables?
//extract email address
$query = "select email from user where email='$mailpass'";
$result = mysql_query($query, $db_conn) or die('query failed');
//extract password
$query2 = "select passwd from user where email='$mailpass'";
$result2 = mysql_query($query2, $db_conn) or die('query failed');
then i've taken one into a variable
$pass = mysql_result($result2, 'passwd');
is it possible to combine the 2 queries and draw out what i want to make the variables?
Code: Select all
$query = "select email,passwd from user where email='$mailpass'";e.g.
then you'll have $result['email'] and $result['passwd'] (in case myqsl_... didn't fail)
Code: Select all
$result = mysql_fetch_assoc($result);I don't quite get what you mean, but here are my guesses.
Q1) 'I need to put values into two tables in one statement'
A1) You can't, as far as I know
Q2) 'I need to get values from 2 tables in one statement'
A2) Try
Then once you've got your data into an associative(?) array, you can $array['SomeInfo'] or $array['SomeOtherInfo']
If the feild names conflict, you might want to chech how it's stored in the array using print_r()
Q1) 'I need to put values into two tables in one statement'
A1) You can't, as far as I know
Q2) 'I need to get values from 2 tables in one statement'
A2) Try
Code: Select all
SELECT `Table1`.`SomeInfo`, `Table2`.`SomeOtherInfo` WHERE whatever = something;If the feild names conflict, you might want to chech how it's stored in the array using print_r()
)
thanks i'd better explain better as well, hehe
i have a user table and an access table when someone registers i need it to enter the user info and then enter values into access as well. As they both have auto id this will mean the user info matches with the access lvls.
i have a user table and an access table when someone registers i need it to enter the user info and then enter values into access as well. As they both have auto id this will mean the user info matches with the access lvls.
From what you say, it seems like you're storing the access levels for a user in a different table from the other info about the user. Why not just use one table? You don't hae to worry about matching values then
If you still want to go down the 2 tables route, have a look at mysql_insert_id()
If you still want to go down the 2 tables route, have a look at mysql_insert_id()
re volka's response
regarding volka's response about 2 queries do you mean like this?
<form method="post" action="index.php?log=forgot">
<input type="text" name="mailpass" style="style="font-size:10px;border:solid 1px;">
<input type=image src="buttons\send.gif" value="send" style="font-size:10px";>
</form>
<?php
if (!$_POST['mailpass'])
{
echo "<div class=\"log\">please enter your email address.</div>";
}
elseif (isset($_POST['mailpass']))
{
$mailpass = $_POST['mailpass'];
$db_conn = mysql_connect("localhost", "cadmin", "cpass") or die('cannot connect to db-server');
mysql_select_db("crimson", $db_conn) or die('cannot select database');
$query = "select email,passwd from user where email='$mailpass'";
$result = mysql_query($query, $db_conn) or die('query failed');
$result = mysql_fetch_assoc($result);
if (mysql_num_rows($result) >0 )
{
$email = $result['email'];
$from = "from: crimson@irealms.co.uk \r\n";
$mesg = "your password is $result['email']\r\n";
echo "<fieldset><legend>Mail sent to</legend> $email</fieldset>";
mail($email, $from, $mesg) or die('mail not sent');
}
else
echo 'record not found';
}
else
echo 'invalid request';
?>
<form method="post" action="index.php?log=forgot">
<input type="text" name="mailpass" style="style="font-size:10px;border:solid 1px;">
<input type=image src="buttons\send.gif" value="send" style="font-size:10px";>
</form>
<?php
if (!$_POST['mailpass'])
{
echo "<div class=\"log\">please enter your email address.</div>";
}
elseif (isset($_POST['mailpass']))
{
$mailpass = $_POST['mailpass'];
$db_conn = mysql_connect("localhost", "cadmin", "cpass") or die('cannot connect to db-server');
mysql_select_db("crimson", $db_conn) or die('cannot select database');
$query = "select email,passwd from user where email='$mailpass'";
$result = mysql_query($query, $db_conn) or die('query failed');
$result = mysql_fetch_assoc($result);
if (mysql_num_rows($result) >0 )
{
$email = $result['email'];
$from = "from: crimson@irealms.co.uk \r\n";
$mesg = "your password is $result['email']\r\n";
echo "<fieldset><legend>Mail sent to</legend> $email</fieldset>";
mail($email, $from, $mesg) or die('mail not sent');
}
else
echo 'record not found';
}
else
echo 'invalid request';
?>
That code will e-mail them their e-mail address. You're on the right track though.
Also...
should be
or something similar, so that you can get further rows from the result set. Not really applicable in this case, coz you only deal with one record, but a note to take for the futute 
Also...
Code: Select all
$query = "select email,passwd from user where email='$mailpass'";
$result = mysql_query($query, $db_conn) or die('query failed');
$result = mysql_fetch_assoc($result);Code: Select all
$query = "select email,passwd from user where email='$mailpass'";
$result = mysql_query($query, $db_conn) or die('query failed');
$row = mysql_fetch_assoc($result);ok
so if i change that to row does row store the values and i use $row['email'] to get the email?
as in
$row = mysql_fetch_assoc($result);
then
$mesg = "your password is $row['passwd']\r\n";
i'm getting the following error in the mesg line
Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in /home/httpd/vhosts/irealms.co.uk/httpdocs/crimson/forgot.php on line 31
as in
$row = mysql_fetch_assoc($result);
then
$mesg = "your password is $row['passwd']\r\n";
i'm getting the following error in the mesg line
Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in /home/httpd/vhosts/irealms.co.uk/httpdocs/crimson/forgot.php on line 31
Last edited by irealms on Thu May 15, 2003 8:00 am, edited 1 time in total.
ok still getting that error
Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in /home/httpd/vhosts/irealms.co.uk/httpdocs/crimson/forgot.php on line 31
the line in question is
$mesg = "your password is $row['passwd'] \r\n";
the line in question is
$mesg = "your password is $row['passwd'] \r\n";