Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/214723-array-confusion/
Share on other sites

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'

Link to comment
https://forums.phpfreaks.com/topic/214723-array-confusion/#findComment-1117171
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/214723-array-confusion/#findComment-1117176
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/214723-array-confusion/#findComment-1117180
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/214723-array-confusion/#findComment-1117182
Share on other sites

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
}

Link to comment
https://forums.phpfreaks.com/topic/214723-array-confusion/#findComment-1117199
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.