Reputation: 339
I'm developing a script, which gets titles and ids from youtube using Curl and saves them to a database with the following structure: id | title | id_youtube | date.
The problem is I need a code so that if the title already exists in db that shows the contents from db, otherwise curl to process and store it in the DB.
id_youtube($q); // this function get the id_youtube
title_youtube($q); // this function get the youtube title
$id = id_youtube($q);
$q = title_youtube($q);
This is my code to insert values to DB
mysql_connect('localhost', 'db_user', 'pw_name') or die($connect_error);
mysql_select_db('db_name') or die($connect_error);
mysql_query("INSERT IGNORE INTO posts (id, title, id_youtube,date) VALUES (null,'$q', '$id',now())");
right now, just saves to DB, and if exist, again insert in new row (dupicate row), I need ssomething like:
if $q already exist {
//query to show content from DB
}else{
// Code tu get titles and ids WHIT Curl
//mysql_query("INSERT IGNORE INTO posts (id, title, id_youtube,date) VALUES (null,'$q', '$id',now())");
}
any help ?
Upvotes: 0
Views: 4820
Reputation: 421
You can probably do a query that try to find a row with the id_youtube you are analyzing. If the query return at least one row (actually only one row), you will get the info using a second query, else you will do the insert query.
Something like this pseudo code:
$query = "SELECT * FROM posts WHERE id_youtube = '$id' LIMIT 1";
$result = mysql_query($query);
if(mysql_num_rows($result) == 1)
$query = "SELECT yourColumns FROM posts WHERE id_youtube = '$id'";
display_the_info();
else
mysql_query("INSERT IGNORE INTO posts (id, title, id_youtube,date) VALUES (null,'$q', '$id',now())");
You should also use myslqi functions, not mysql one.
Upvotes: 1