PDA

View Full Version : help on "echo"


dzwestwindsor
06-20-2009, 09:28 PM
so i wanna echo one object of my database, for ex-- i want to display the date of birth of username "bob". so i want to pinpoint that one point and give 2 criteria- username and date of birth. i want to display the date of birth of a certain username. how can i display it like that? i was watching the video tutorial and it had this code

<?php echo $row ['dateofbirth']; ?> that just displays the first date of birth. how can i display the DOB for a certain username? thx for ur help

Corrosive
06-21-2009, 06:28 AM
so i wanna echo one object of my database, for ex-- i want to display the date of birth of username "bob". so i want to pinpoint that one point and give 2 criteria- username and date of birth. i want to display the date of birth of a certain username. how can i display it like that? i was watching the video tutorial and it had this code

<?php echo $row ['dateofbirth']; ?> that just displays the first date of birth. how can i display the DOB for a certain username? thx for ur help

You'll need to tell the database which specific dob you are looking for and then echo that out. I'd suggest something like...

<?php

$sql = ("SELECT * FROM customer WHERE username = '$username'"); //this queries the database based on username

$query = mysql_query($sql); //this puts query into a variable

$result = mysql_fetch_array($query); //this gets the array from the variable

echo $result ['dateofbirth']; //this echoes the dob the query got from the database

?>


You'll need to change the table name etc to suit what you have in your database.

dzwestwindsor
06-21-2009, 11:52 AM
ok im sorta confused about the first line. do i have to define anything else b4 that? like do i have to define $username? and also wat is the first "username" indicating?

dzwestwindsor
06-21-2009, 12:25 PM
ok so i tried this code-- <?php

$sql = ("SELECT * FROM members WHERE username = 'bobjoesmith'"); //this queries the database based on username

$query = mysql_query($sql); //this puts query into a variable

$result = mysql_fetch_array($query); //this gets the array from the variable

echo $result ['email']; //this echoes the dob the query got from the database

?> but then it returned an errrror message- "
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in D:\wamp\www\local root\Test\index.php on line 158"

dzwestwindsor
06-21-2009, 12:27 PM
oh line 58 says this-- $result = mysql_fetch_array($query); //this gets the array from the variable

Corrosive
06-21-2009, 12:37 PM
oh line 58 says this-- $result = mysql_fetch_array($query); //this gets the array from the variable

Have you checked the connection to the database and do you have a members table which has an 'email' column?

dzwestwindsor
06-21-2009, 01:32 PM
if it does, is my code correct?

Corrosive
06-21-2009, 01:43 PM
if it does, is my code correct?

Yes, it looks OK but I usually end up missing something out when coding PHP! The idea is certainly correct even if there is a stray character or something...normally a ';' I find!!

I take it that the variable $username will be powered by the user entering a name in a form?

dzwestwindsor
06-21-2009, 09:22 PM
Ohh Yess!!! i got it!!!!! Omg Thx So Much!:mrgreen::mrgreen::mrgreen::mrgreen:

Corrosive
06-22-2009, 06:08 AM
You're welcome. PHP is annoying as hell as you try to get it right but really rewarding when it all comes together. It's rollercoaster time :wink:

dzwestwindsor
06-22-2009, 10:04 PM
oh one more thing-- can u plz give me the code to define $username plz? this is wat im doing-- i have a log in page, and then if the user types in the right log in info, he gets directed to a different page that says "welcome _______" the blank is where his username should go. so i need to define $username. $username is the field that he entered on the form of the previous page. how do i define it so it works?

Corrosive
06-23-2009, 06:06 AM
oh one more thing-- can u plz give me the code to define $username plz? this is wat im doing-- i have a log in page, and then if the user types in the right log in info, he gets directed to a different page that says "welcome _______" the blank is where his username should go. so i need to define $username. $username is the field that he entered on the form of the previous page. how do i define it so it works?

You need to feed it into a variable using the form.

Form field:

<input name="username" type="text" id="username" size="30" maxlength="30" />

(It goes without saying that you will need a submit button!!)

Then define the variable wherever it feeds into by using PHP:

$username = $_POST['username'];

dzwestwindsor
06-23-2009, 01:42 PM
right, but does that only work for that one page only? i am directing the user to another page once after they log in. so can i still use the form to define the variable if its on another page?

Corrosive
06-23-2009, 03:06 PM
right, but does that only work for that one page only? i am directing the user to another page once after they log in. so can i still use the form to define the variable if its on another page?

Yes, of course. Just make sure your form is 'pointed' at the correct page

Something like...

<form action="login.php" method="post">

(replace red text with whatever your login page is called)

and then define the variable from the posted form in the new page as I showed you.

bee80
06-30-2009, 07:33 PM
Also you can define constants by : define('USERNAME','what u want it defined as');