merylvingien Posted September 29, 2010 Share Posted September 29, 2010 I think i have muddled myself up here somewhere along the line I am trying to check to see if various things already exists in a database. So for example i have a collumn with names, second collumn is email address, third collumn phone number etc. So i make the usual connection to the database: $sql=("SELECT * FROM table") $result = mysql_query($sql); $row = mysql_fetch_array($result); Now to assign the various collumns is where i have come unstuck. if (in_array("?", $row)) {do something} or is it if (in_array("?", $row['name'])) {do something} ? I have it working by hard coding to a seperate php file, but its bulky and would prefer to use the database. Quote Link to comment Share on other sites More sharing options...
kickstart Posted September 29, 2010 Share Posted September 29, 2010 Hi Assuming you want to check that (say) $row contains a column called 'name':- if (array_key_exists("name", $row)) {do something} All the best Keith Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 29, 2010 Share Posted September 29, 2010 Well, the proper syntax would be if (in_array("?", $row)) {do something} But, that would only check the first record. You would have to loop through all the records in the result set to see if the record exists. However, you are going about this the wrong way. Instead you should craft the query to find the records with the value directly. Are you searching one field for a value SELECT * FROM table WHERE filed1='$searchValue' Or all fields SELECT * FROM table WHERE field1 = '$searchValue' OR field2 = '$searchValue' OR field3 = '$searchValue' OR field4 = '$searchValue' Quote Link to comment Share on other sites More sharing options...
merylvingien Posted September 29, 2010 Author Share Posted September 29, 2010 Thanks for the replies maybe i should explain more clearly. I have a table which has 4 collumns, name, email, weblink, phone When a new user signs up, i want to check this table to make sure they havent signed up in the past. So i need to run through each collumn to check that the name they entered isnt allready listed, email isnt listed, weblink isnt listed etc. So, does this mean that i need to make a query for each seperate collumn? Or can i not just select all, then run through the various checks? As i said, i have got myself confused with this one. Quote Link to comment Share on other sites More sharing options...
kickstart Posted September 29, 2010 Share Posted September 29, 2010 Hi Think I would just do this in SQL. Something like this (assuming you have the entered fields stores as $name, $email, $weblink and $phone) SELECT * FROM SomeTable WHERE name = '$name' OR email = '$email' OR weblink = '$weblink' OR phone = '$phone' Checking each row in php would entail getting every row from the database and checking them each in turn. All the best Keith Quote Link to comment Share on other sites More sharing options...
litebearer Posted September 29, 2010 Share Posted September 29, 2010 Modifiying mjd's code stricly for clarity... $query ="SELECT *FROM tableWHERE namefield = '$theirname' OR emailfield = '$theiremail' OR phonefield = '$theirphone' OR websitefield = '$theirwebsite'"; that will get all records where at least one of the values is already in the table replace OR with AND if you want exact match Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 29, 2010 Share Posted September 29, 2010 Modifiying mjd's code stricly for clarity... $query ="SELECT *FROM tableWHERE namefield = '$theirname' OR emailfield = '$theiremail' OR phonefield = '$theirphone' OR websitefield = '$theirwebsite'"; that will get all records where at least one of the values is already in the table replace OR with AND if you want exact match And, just to clarify, you would run that query and simply check if the number of results (i.e. mysql_num_rows()) is 0 (no matches) or >1 (there is a match). Here is some sample code to illustrate: $query ="SELECT `name` FROM `table` WHERE `name` = '$name' OR `email` = '$email' OR `phone` = '$phone' OR `website` = '$website'"; $result = mysql_query($query); if(mysql_num_rows($result)>0) { //ERROR: There are existing records with that data } else { //OK: There are no existing records with that data } Quote Link to comment Share on other sites More sharing options...
merylvingien Posted September 29, 2010 Author Share Posted September 29, 2010 Thanks for the replies, i will have a play and see what i can achieve. Quote Link to comment 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.