Re: Compare and inserting with php
| From: | Ashley Sheridan | Date: | Tue, 30 Jun 2009 15:17:40 +0000 |
| Subject: | Re: Compare and inserting with php | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-294745@lists.php.net to get a copy of this message | ||
On Tuesday 30 June 2009 15:42:01 Miller, Terion wrote:
> Okay, what I am trying to do is this:
>
> I have a db full of inspections, daily I need to run a script that will
> grab the posting of inspections, check them against the db to make sure
> they aren't duplicates and then insert all the new entries..... Not sure if
> my logic of going about it this way is right, or if I'm off track and need
> to be doing something different please advise...so far this is how I am
> writing it.....and it seems redundant ... Shouldn't there be a way to say
> (in plain speak) if there are no records with the name== insert this one,
> if there is one with the name then check intype if none ..insert... Etc
>
>
> if (!empty($Go)) {
>
> $query = "SELECT * FROM restaurants WHERE name LIKE '$ucName'";
>
> $result = mysql_query ($query);
>
> $row = mysql_fetch_object ($result);
>
>
> If (mysql_num_rows($result) == 0) {
>
> $sql = "INSERT INTO
restaurants (name, address, inDate,
> inType, notes,
> critical, cviolations, noncritical) VALUES (";
>
> $sql .= " '$ucName', '$ucAddress', '$inDate',
> '$inType', '$notes',
> '$critical', '$cleanViolations', '$noncritical')";
>
> $result = mysql_query($sql) or die(mysql_error());
>
>
> } else {
>
> $query = "SELECT * FROM restaurants WHERE inType LIKE '$inType' AND name
> LIKE '$ucName' ";
>
> $result = mysql_query ($query);
>
> $row = mysql_fetch_object ($result);
>
> If (mysql_num_rows($result) == 0) {
>
> $sql = "INSERT INTO restaurants (name, address, inDate,
> inType, notes,
> critical, cviolations, noncritical) VALUES (";
>
> $sql .= " '$ucName', '$ucAddress', '$inDate',
> '$inType',
> '$notes', '$critical', '$cleanViolations',
> '$noncritical')";
>
> $result = mysql_query($sql) or die(mysql_error());
>
>
> } else {
>
> $query = "SELECT * FROM restaurants WHERE inDate LIKE '$inDate' AND name
> LIKE '$ucName' ";
>
> $result = mysql_query ($query);
>
> $row = mysql_fetch_object ($result);
>
>
> If (mysql_num_rows($result) == 0) {
>
> $sql = "INSERT INTO restaurants (name, address, inDate,
> inType, notes,
> critical, cviolations, noncritical) VALUES (";
>
> $sql .= " '$ucName', '$ucAddress', '$inDate',
> '$inType',
> '$notes', '$critical', '$cleanViolations',
> '$noncritical')";
>
>
> $result = mysql_query($sql) or die(mysql_error());
>
>
> }
> }
It's a bad way of doing it, but could you not create a unique index on the
particular key you want to be unique, and then just try the select anyway
with the mysql_query() call preceeded with an @ to suppress warnings?
Thanks,
Ash
http://www.ashleysheridan.co.uk