Forum Moderators: coopster

Message Too Old, No Replies

Resource id #3

         

iubaker84

1:41 am on Oct 11, 2004 (gmt 0)

10+ Year Member



resultID should be returning their dancer number. It instead is returning "Resource Id #3"...

What am I doing wrong?

if($submit)
{
$db = mysql_connect("*****", "*****","*****");
mysql_select_db("*******",$db);
$sql = "INSERT INTO dancer (firstname, lastname, email, phone, stAddress, city, state, zip, organization, effectedbyRiley, addtoSFemails)
VALUES ('$firstname', '$lastname', '$email', '$phone', '$stAddress', '$city', '$state', '$zip', '$organization', '$effectedbyRiley', '$addtoSFemails')";
$result = mysql_query($sql);
$idSelect = "SELECT userId from dancer where firstname='$firstname'";
$resultID = mysql_query($idSelect);
echo "Your application to be a dancer has been submitted. Please submit the $15 registration fee to the Student Foundation. ";
echo $resultID;
echo " is your Dancer Number";
}

Thanks for your help in advance!

Nutter

2:20 am on Oct 11, 2004 (gmt 0)

10+ Year Member



Look @ mysql_fetch_array and mysql_fetch_assoc. The will take your variable ($resultID) and put it into whatever variable array you need. I like fetch_assoc better b/c you can then access it with $variable['fieldname'] rather than just $variable[fieldnumber].

Question for you. It looks like you're adding a record and then getting the id for that record by searching for the first name. What happens if you add a second 'Bob' to the database? It should pull up the first one and give you that id first. If all you're trying to do is get the unique id from the previous INSERT, look at mysql_insert_id. I'm pretty sure it would do what you're trying to do without the second query.

HTH
- Ryan

iubaker84

2:34 am on Oct 11, 2004 (gmt 0)

10+ Year Member



This actually is my first PHP application that I am building, and first experience with My_SQL. I've had limited experience with SQL, but like I said, it's limited.

What would the code be like for this?

mysql_fetch_array($resultID); <- like that?

Nutter

2:43 am on Oct 11, 2004 (gmt 0)

10+ Year Member



$whatever = mysql_fetch_assoc($resultID);
echo "Your ID is ".$whatever['fieldname'];

It's taking the result and storing it in an associative array. You then pull the array member out. This example will pull out the first record. If there's more than one record, you'll have to loop. If it doesn't pull a record, it will return FALSE.

Have a look @ [us2.php.net...]

- Ryan

dreamcatcher

8:41 am on Oct 11, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



As you are only pulling data from one row, then mysql_fetch_row or mysql_fetch_object will be better:

$resultID = mysql_query($idSelect);
$dancerID = mysql_fetch_object($resultID);

echo $dancerID->userId;
echo " is your Dancer Number";

And like Nutter mentions, unless your firstname field is unique, you could pull the wrong entry and there`s no way you can have firstname unique, so you may want to think about pulling the data based on something unique, then use the limit clause:

$idSelect = "SELECT userId from dancer where something='$something' LIMIT 1";

or at the very least check against both firstname AND lastname:

$idSelect = "SELECT userId from dancer where firstname='$firstname' AND lastname = '$lastname' LIMIT 1";

Nutter

1:00 pm on Oct 11, 2004 (gmt 0)

10+ Year Member



Dreamcather - can you explain why fetch_row or fetch_object is better? I typically use fetch_row or fetch_assoc, although I don't know why I use one in place of the other. But, why is one better than the other?

Thanks,
- Ryan

dreamcatcher

1:29 pm on Oct 11, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Nutter,

fetch_row is apparently slower then fetch_object, but to be honest I can never see any difference. I always use fetch_object over fetch_row because I prefer an associative array over an enumerated one. If you have lots of rows the numbers can get confusing. Plus its easier for debugging if you can see your row names. Works better for me anyway.

fetch_assoc is really an equivalent to fetch_array, except that fetch_array fetches the set as an associative and numeric array. If you need to use the numeric indices with fetch_assoc you need to add MYSQL_ASSOC as a parameter. You probably know this already.

I think its all down to personal preference really. :)

Nutter

4:47 pm on Oct 11, 2004 (gmt 0)

10+ Year Member



Thanks Dreamcatcher. Just wanted to make sure I wasn't using the slow method out of habit :-)

iubaker84

9:25 pm on Oct 12, 2004 (gmt 0)

10+ Year Member



Thanks sooo much, nutter and dreamcatcher! This is so cool. ^_^ I just did an if else that does a 'postback' when it has been submitted.

My next step is to send an e-mail to both me to let me know a new person has applied and also to the user as a confirmation e-mail, preferably an HTML e-mail so I can make it look cool. Any suggestions?

dreamcatcher

11:08 pm on Oct 12, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



iubaker84,

Glad we can help, this is what this forum is for. Sending HTML e-mails is down to your headers. By default headers are plain text if nothing is specified. To send HTML e-mails you need to change your header syntax.

This is a snippet of code jatar_k posted a while ago:


$from .= "From: Name<$someemail>\n";
$from .= "X-Sender: Name<$someemail>\n";
$from .= "X-Mailer: PHP\n";
$from .= "X-Priority: 3\n";
$from .= "Return-Path: Name<$someemail>\n";
$from .= "Reply-To: Name<$siteemail>\n";

mail($to,$subject,$message,$from);

You just need to add HTML as normal in your message body with these headers. Give it a try and get back to us.

:)