Question

I would like to be able to check using PHP a sql database for a url to see if it exists before inserting a new row in a table.

so if for example the data base has the following url already in it: http://example.com

how do I query the data base to check if that URL already exists, or a url with that domain within it.

For example, if the domain http://example.com/directory/index.php is submitted i'd like that to return a match as the top level domain already exists.

Was it helpful?

Solution 3

Something like this??? You can change your query depending on how all the URLs in the database are stored.

$url_parts=parse_url($url);
$domain=strtolower(str_replace('www.', '', $url_parts['host']));
$query=mysqli_query($dbconn, "SELECT column_name FROM table WHERE 
INSTR(LOWER(url_column_name), '$domain')");

OTHER TIPS

Just do a SELECT?

$url = 'http://example.com';

//Query using COUNT
$sql = "SELECT COUNT(*) AS num FROM `urls` WHERE `url` = :url";

$stmt = $pdo->prepare($sql);
$stmt->bindParam(':url', $url);
$stmt->execute();

$row  = $stmt->fetch();

if($row['num'] > 0){
    //URL already exists.
}

try this...

SELECT DISTINCT SUBSTRING_INDEX(REPLACE(REPLACE(URLfieldName, "http://", ""), "www.", ""), '/', 1) from tableName
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top