Forum Moderators: coopster

Message Too Old, No Replies

I can get output from mysql...

...but i can't get php to work correctly

         

mjar81

3:06 pm on Feb 11, 2004 (gmt 0)

10+ Year Member



can someone please help me out? i've been at this for days and i can't seem to get the correct code to do what i want to do.

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

mjar81

3:12 pm on Feb 11, 2004 (gmt 0)

10+ Year Member



heres' my code... all i get as output in php is that it says "Resource id #2"


<?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!

seomike2003

3:21 pm on Feb 11, 2004 (gmt 0)



Hey mjar81 try this :)

<?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]

justageek

3:22 pm on Feb 11, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You've got it. You just have to finish it now.

$resultset = mysql_fetch_array($sql);
echo $resultset['password'];

Remove the echo $sql and add those 2 lines and you should see the password.

JAG

mjar81

4:10 pm on Feb 11, 2004 (gmt 0)

10+ Year Member



GREAT! it worked! thanks so much for your help!

coolo

6:19 am on Feb 16, 2004 (gmt 0)

10+ Year Member


Hello, I was having the same problem, (I was using the print function instead of echo (don't think this makes any difference). I'm a complete newbie to php and mysql but I'm trying to understand how this works.

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?

coopster

1:27 pm on Feb 16, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Sure! And Welcome to WebmasterWorld, coolo!

Basics of extracting data from MySQL using PHP [webmasterworld.com] has a pretty good explanation.

coolo

6:37 pm on Feb 16, 2004 (gmt 0)

10+ Year Member


cool, thank you for the link and the warm welcome coopster. That is good reading. So, i'm starting to think that it is necessary to use a mysql_fetch_array() or similar function if you want to display info pulled from a table within MySQL?

coopster

7:22 pm on Feb 16, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Yep. Just like jatar_k outlined in the link you read. Build the query statement, execute the query statement (mysql_query), and process the result set using one of the mysql_fetch functions (mysql_fetch_array, mysql_fetch_assoc, etc.)