I have a list of urls stored in a database, and I want to run a bit of code that checks an array of urls vs what is stored in the database. If the value exists, I want that value from the array dropped.
我有一个存储在数据库中的URL列表,我想运行一些代码来检查url数组与数据库中存储的内容。如果该值存在,我希望删除该数组中的值。
So far, I have a database that contains 3 rows:
到目前为止,我有一个包含3行的数据库:
CREATE TABLE links
(
link_id INT(10) NOT NULL AUTO_INCREMENT,
url VARCHAR(255) NOT NULL,
last_visited TIMESTAMP,
PRIMARY KEY (link_id),
UNIQUE KEY (url)
)
And basically I'm just trying to insert the data vs a unique value via an INSERT command and if it fails, i'd like to remove that array value. Is this possible?
基本上我只是尝试通过INSERT命令插入数据与唯一值,如果失败,我想删除该数组值。这可能吗?
My bad code:
我的错误代码:
foreach ($urlArray as $url) {
$sql = "INSERT INTO linkz (url, last_visited) VALUES ('".$url."', NOW())";
if (!mysql_query($sql,$con)){
// remove array here somehow?
}
}
Is there a better way?
有没有更好的办法?
Any help would be appreciated, thanks! Tre
任何帮助将不胜感激,谢谢! TRE
2 个解决方案
#1
3
You can drop a value from an array using unset
. To do this, you need to know the key, so you might consider modifying your foreach
to include the key:
您可以使用unset从数组中删除值。要执行此操作,您需要知道密钥,因此您可以考虑修改foreach以包含密钥:
foreach ($urlArray as $key => $url) {
...
// Remove the item from the array
unset($urlArray[$key]);
}
#2
1
I suppose that's one way to do it. There are several issues:
我想这是一种方法。有几个问题:
- Non-normalized URL notation
- Changing a table to test for existence
非规范化URL表示法
更改表以测试是否存在
The first issue is that there are a large number of ways of expressing any given URL. For example: http://www.example.com/somepage can be written http://www.example.com/%73omepage
第一个问题是有很多表达任何给定URL的方法。例如:http://www.example.com/somepage可以写成http://www.example.com/%73omepage
The other is that philosophically speaking, a pure test for some data in a database should not change the database, whether or not it already exists. A simple SELECT * FROM links WHERE url=whatever
is the cleaner approach. Presumably you have an unstated goal of collecting URLs.
另一个是哲学上讲,对数据库中某些数据的纯测试不应该改变数据库,无论它是否已经存在。一个简单的SELECT * FROM链接WHERE url =什么是更清洁的方法。据推测,您有一个未说明的收集URL的目标。
@mfonda has already answered the literal question.
@mfonda已经回答了字面问题。
#1
3
You can drop a value from an array using unset
. To do this, you need to know the key, so you might consider modifying your foreach
to include the key:
您可以使用unset从数组中删除值。要执行此操作,您需要知道密钥,因此您可以考虑修改foreach以包含密钥:
foreach ($urlArray as $key => $url) {
...
// Remove the item from the array
unset($urlArray[$key]);
}
#2
1
I suppose that's one way to do it. There are several issues:
我想这是一种方法。有几个问题:
- Non-normalized URL notation
- Changing a table to test for existence
非规范化URL表示法
更改表以测试是否存在
The first issue is that there are a large number of ways of expressing any given URL. For example: http://www.example.com/somepage can be written http://www.example.com/%73omepage
第一个问题是有很多表达任何给定URL的方法。例如:http://www.example.com/somepage可以写成http://www.example.com/%73omepage
The other is that philosophically speaking, a pure test for some data in a database should not change the database, whether or not it already exists. A simple SELECT * FROM links WHERE url=whatever
is the cleaner approach. Presumably you have an unstated goal of collecting URLs.
另一个是哲学上讲,对数据库中某些数据的纯测试不应该改变数据库,无论它是否已经存在。一个简单的SELECT * FROM链接WHERE url =什么是更清洁的方法。据推测,您有一个未说明的收集URL的目标。
@mfonda has already answered the literal question.
@mfonda已经回答了字面问题。