Forum Moderators: coopster
I want to take a single variable result from mysql and assign it to a variable
i input into the mysql command line prompt:
SELECT password FROM prayer_users WHERE username='mjar81'; i understand the syntax, that i want to select the password only, from whatever row contains the username mjar81.
it returns me this:
+----------+
¦ password ¦
+----------+
¦ whatever ¦
+----------+
1 row in set (0.05 sec) now... i can't seem to get the php to give me what i want. all i want is to assign this a variable name so that i can work with it... can someone please show me how to do that?
i can do all the connecting to the database and such, but i have trouble starting with the "SELECT" line.
thanks a whole bunch in advance!
mjar81
<?php
$user = "mjar81";mysql_connect('localhost', 'mjar81', '*******')
or die("Cant connect: " .mysql_error());
mysql_select_db("prayer");
$sql = mysql_query("SELECT password FROM prayer_users WHERE username='.$user.'");
echo $sql;
?>
thanks for looking!
<?php
$user = "mjar81";
mysql_connect('localhost', 'mjar81', '*******')
or die("Cant connect: " .mysql_error());
mysql_select_db("prayer");
//make the record set
$rs = mysql_query("SELECT password FROM prayer_users WHERE username='$user'");
//while returning the row as an array define the variable
//You can use * in place of password and bring back the entire row and fields.
while($rw=mysql_fetch_array($rs)){
//declare $pw variable as being the same as password
$pw=$rw['password'];
}
//print the password to the screen
echo $pw;
?>
[edited by: seomike2003 at 3:25 pm (utc) on Feb. 11, 2004]
From my understanding,
$sql = mysql_query("SELECT password FROM prayer_users WHERE username='.$user.'"[smilestopper]);
should basically leave $sql with a value of passwordX.
So why doesn't the line "echo $sql;" return passwordX.
I can see how the lines suggested by justageek work, but I don't understand why they are needed. Can anyone offer a quick explination?
Basics of extracting data from MySQL using PHP [webmasterworld.com] has a pretty good explanation.