deansaddigh Posted January 6, 2010 Share Posted January 6, 2010 am i wrong in thinking that $id = $emailresult, would have the id in of a user that already exists. HEres the code. its basically just a check to see if a user exists and if he doesnt add him and if he does dont and print out the id of the user that exists. /*sql query to find if email address exists for client*/ $emailquery = "SELECT id FROM clients WHERE email = '$email'"; $emailresult = mysql_query($emailquery); /* if there are rows returned for the contact id the below code will return true*/ if(!mysql_num_rows($emailresult)) { /* if no row is returned insert to db*/ $addClientSQL = "INSERT INTO clients(title, name, surname, email, telephone, address1, address2, town, county, postcode, country, emailMarketing, mailMarketing) VALUES ('$title','$name','$surname','$email','$telephone','$address1','$address2','$town','$county','$postcode','$country','$emailMarketing','$mailMarketing')"; $db->execute($addClientSQL); echo mysql_error(); $client_id = mysql_insert_id(); header('location: index.php'); } else { /*pass the id through url to get that users records*/ $id = $emailresult; echo $id; Quote Link to comment https://forums.phpfreaks.com/topic/187386-whats-being-passed-to-my-variable/ Share on other sites More sharing options...
trq Posted January 6, 2010 Share Posted January 6, 2010 am i wrong in thinking that $id = $emailresult, would have the id in of a user that already exists. Yes. In your example $emailresult is a result resource. You would need to pass this resource to mysql_fetch_assoc (or similar) to actually get any data out of the resource. Quote Link to comment https://forums.phpfreaks.com/topic/187386-whats-being-passed-to-my-variable/#findComment-989505 Share on other sites More sharing options...
Adam Posted January 6, 2010 Share Posted January 6, 2010 To add to that, you can use mysql_fetch_field to return just 1 field: $id = mysql_fetch_field($emailresult, 0); Quote Link to comment https://forums.phpfreaks.com/topic/187386-whats-being-passed-to-my-variable/#findComment-989506 Share on other sites More sharing options...
deansaddigh Posted January 6, 2010 Author Share Posted January 6, 2010 Thanks for your help guys, im getting this error now Catchable fatal error: Object of class stdClass could not be converted to string in D:\Inetpub\wwwroot\dhcottages.co.uk\testsite\admin\clients\saveClients.php on line 174 my code is now /*sql query to find if email address exists for client*/ $emailquery = "SELECT id FROM clients WHERE email = '$email'"; $emailresult = mysql_query($emailquery); /* if there are rows returned for the contact id the below code will return true*/ if(!mysql_num_rows($emailresult)) { /* if no row is returned insert to db*/ $addClientSQL = "INSERT INTO clients(title, name, surname, email, telephone, address1, address2, town, county, postcode, country, emailMarketing, mailMarketing) VALUES ('$title','$name','$surname','$email','$telephone','$address1','$address2','$town','$county','$postcode','$country','$emailMarketing','$mailMarketing')"; $db->execute($addClientSQL); echo mysql_error(); $client_id = mysql_insert_id(); header('location: index.php'); } elseif(mysql_num_rows($emailresult)) { /*pass the id through url to get that users records*/ $id = mysql_fetch_field($emailresult, 0); echo $id; echo "Sorry a user with that email address exists in our database <br />"; ive used mysql_fetch_field thing. Quote Link to comment https://forums.phpfreaks.com/topic/187386-whats-being-passed-to-my-variable/#findComment-989512 Share on other sites More sharing options...
trq Posted January 6, 2010 Share Posted January 6, 2010 And line 174 is? Also, you seem to be using two different database access methods. One, an object ($db) and the other, the standard mysql extension. You should use one and stick to it unless you need multiple connections to multiple database types or something. Quote Link to comment https://forums.phpfreaks.com/topic/187386-whats-being-passed-to-my-variable/#findComment-989516 Share on other sites More sharing options...
deansaddigh Posted January 6, 2010 Author Share Posted January 6, 2010 Hi, sorry i know i use 2 diff db types, one was written before me and was ridiculous so im just using standered connection, plus my boss has told me that this is what he wants. line 172 is $id = mysql_fetch_field($emailresult, 0 ); and the complete code is /*sql query to find if email address exists for client*/ $emailquery = "SELECT id FROM clients WHERE email = '$email'"; $emailresult = mysql_query($emailquery); /* if there are rows returned for the contact id the below code will return true*/ if(!mysql_num_rows($emailresult)) { /* if no row is returned insert to db*/ $addClientSQL = "INSERT INTO clients(title, name, surname, email, telephone, address1, address2, town, county, postcode, country, emailMarketing, mailMarketing) VALUES ('$title','$name','$surname','$email','$telephone','$address1','$address2','$town','$county','$postcode','$country','$emailMarketing','$mailMarketing')"; $db->execute($addClientSQL); echo mysql_error(); $client_id = mysql_insert_id(); header('location: index.php'); } elseif(mysql_num_rows($emailresult)) { /*pass the id through url to get that users records*/ $id = mysql_fetch_field($emailresult, 0 ); echo $id; echo "Sorry a user with that email address exists in our database <br />"; Quote Link to comment https://forums.phpfreaks.com/topic/187386-whats-being-passed-to-my-variable/#findComment-989519 Share on other sites More sharing options...
trq Posted January 6, 2010 Share Posted January 6, 2010 You should be using mysql_result not mysql_fetch_field. Quote Link to comment https://forums.phpfreaks.com/topic/187386-whats-being-passed-to-my-variable/#findComment-989521 Share on other sites More sharing options...
deansaddigh Posted January 6, 2010 Author Share Posted January 6, 2010 Thank you it works now using result, cheers. One last question i thought that because i was /*sql query to find if email address exists for client*/ $emailquery = "SELECT id FROM clients WHERE email = '$email'"; $emailresult = mysql_query($emailquery); it would only return the id but does it bring back the whole row still? and thats why its an array? Quote Link to comment https://forums.phpfreaks.com/topic/187386-whats-being-passed-to-my-variable/#findComment-989525 Share on other sites More sharing options...
trq Posted January 6, 2010 Share Posted January 6, 2010 $emailresult isn't an array. Its a result resource. Quote Link to comment https://forums.phpfreaks.com/topic/187386-whats-being-passed-to-my-variable/#findComment-989527 Share on other sites More sharing options...
Adam Posted January 6, 2010 Share Posted January 6, 2010 You should be using mysql_result not mysql_fetch_field. Baah, my bad! Sorry it's early Quote Link to comment https://forums.phpfreaks.com/topic/187386-whats-being-passed-to-my-variable/#findComment-989528 Share on other sites More sharing options...
deansaddigh Posted January 6, 2010 Author Share Posted January 6, 2010 Thanks for all your help guys Quote Link to comment https://forums.phpfreaks.com/topic/187386-whats-being-passed-to-my-variable/#findComment-989530 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.