NathanSenn Posted September 9, 2013 Share Posted September 9, 2013 Not sure what im doing wrong here if i take this line out everything works again mysql_query($linksav,"INSERT INTO bx_sites_main (URL) VALUES ('$link['link']')"); This is a little more of the script <?php $linksav = mysql_connect($db['host'], $db['user'], $db['passwd']); if (!$linksav) { die('Could not connect: ' . mysql_error()); } $duperaw = mysql_query("SELECT * FROM bx_sites_main", $link['link']); if (mysql_num_rows($duberaw) > 0) { echo "already exists DB"; } else { mysql_query($linksav,"INSERT INTO bx_sites_main (URL) VALUES ('$link['link']')"); echo "1 record added"; } mysql_close($linksav); ?> Quote Link to comment Share on other sites More sharing options...
gizmola Posted September 9, 2013 Share Posted September 9, 2013 You are introducing a parsing issue with multiple sets of single quotes inside an interpolated string. When you have an array element with a key name and you want to interpolate that inside a string put a block (curly brackets) around the entire variable. mysql_query($linksav,"INSERT INTO bx_sites_main (URL) VALUES ('{$link['link']}')"); Quote Link to comment Share on other sites More sharing options...
DavidAM Posted September 9, 2013 Share Posted September 9, 2013 1) Turn on error reporting (at least in development) so PHP can tell you about problems error_reporting(E_ALL); ini_set('display.errors', 1); 2) Test the results of your queries. I suspect the first one is failing miserably - the second parameter (if provided) should be the connection resource, not a string variable. You are missing the WHERE clause and should (probably) be concatenating: "." instead of "," if ($duperaw === false) { // The query failed, do something reasonable) In your second call to mysql_query you put the link resource first (it should be second if you provide it, but it is not required if you have only one open connection). 3) You cannot embed an associative array element in a double-quoted string unless you use curly-braces $sql = "INSERT INTO bx_sites_main (URL) VALUES ('{$link['link']}')" if (mysql_query($sql)) echo "Data Inserted" Quote Link to comment Share on other sites More sharing options...
NathanSenn Posted September 9, 2013 Author Share Posted September 9, 2013 Its not making my page quit working now but its not adding anything to the DB This is my Table in my database id url title entryUri description status photo date ownerid allowView allowComments allowRate tags categories views rate rateCount commentsCount featured Edit Copy Delete 1 YouSearch.mobi YouSearch YouSearch <p>The worlds newest search engine!</p> approved 0 1377226469 1 3 3 3 search engine Technology 37 5 1 0 0 And this is the updated script <?php $linksav = mysql_connect($db['host'], $db['user'], $db['passwd']); if (!$linksav) { die('Could not connect: ' . mysql_error()); } $duperaw = mysql_query("SELECT * FROM bx_sites_main WHERE (url) = ('{$link['link']}')"); if (mysql_num_rows($duberaw) > 0) { echo "already exists DB"; } else { $sql = "INSERT INTO bx_sites_main (url) VALUES ('{$link['link']}')"; if (mysql_query($sql)) echo "Data Inserted"; } mysql_close($linksav); ?> Quote Link to comment Share on other sites More sharing options...
Barand Posted September 9, 2013 Share Posted September 9, 2013 Check mysql_error() after the insert Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 9, 2013 Share Posted September 9, 2013 I'm pretty sure the error is occurring on the SELECT query which is malformed $duperaw = mysql_query("SELECT * FROM bx_sites_main", $link['link']); Also, I don't see anywhere where you select the DB to use. Try this if (!mysql_connect($db['host'], $db['user'], $db['passwd'])) { die('Could not connect: ' . mysql_error()); } if(!mysql_select_db('NAME_OF_DATABASE') // <==== Enter the DB name here { die('Could select DB: ' . mysql_error()); } $sql = "SELECT url FROM bx_sites_main WHERE url = '{$link['link']}'" $result = mysql_query($sql); if(!$result) { die('Error running Select Query: "{$query}"<br>Error: ' . mysql_error()); } elseif (mysql_num_rows($result) > 0) { echo "Value already exists DB"; } else { $sql = "INSERT INTO bx_sites_main (url) VALUES ('{$link['link']}')"; $result = mysql_query($sql); if(!$result) { die('Error running INSERT Query: "{$query}"<br>Error: ' . mysql_error()); } else { if (mysql_query($sql)) echo "Data Inserted"; } } mysql_close($linksav); Quote Link to comment Share on other sites More sharing options...
NathanSenn Posted September 14, 2013 Author Share Posted September 14, 2013 This is my new code still not adding anything to the DB <?php $linksav = mysql_connect($db['host'], $db['user'], $db['passwd']); $mysqldb = mysql_select_db('yousearc_YouSearch'); if (!$linksav) { die('Could not connect: ' . mysql_error()); } if (!$mysqldb) { die('Could not connect: ' . mysql_error()); } $sqlk = "SELECT url FROM bx_sites_main WHERE url = '{$link['link']}'"; $duperaw = mysql_query($sqlk); if (mysql_num_rows($duberaw) > 0) { echo "already exists DB"; } else { $html = file_get_contents_curl($link['link']); //parsing begins here: $doc = new DOMDocument(); @$doc->loadHTML($html); $nodes = $doc->getElementsByTagName('title'); //get and display what you need: $title = $nodes->item(0)->nodeValue; $metas = $doc->getElementsByTagName('meta'); for ($i = 0; $i < $metas->length; $i++) { $meta = $metas->item($i); if($meta->getAttribute('name') == 'description') $description = $meta->getAttribute('content'); if($meta->getAttribute('name') == 'keywords') $keywords = $meta->getAttribute('content'); } $sql = "INSERT INTO bx_sites_main (url, title, entryUri, description, allowView, allowComments, allowRate, tags) VALUES ('{$link['link']}, $title, $title.date(DATE_ATOM, mktime(0, 0, 0, 7, 1, 2000), $description, 3, 3, 3, $keywords')"; $addlink = mysql_query($sql); } Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 14, 2013 Share Posted September 14, 2013 If you are not going to take suggestions - then why ask for help? You are not checking for errors on the INSERT query which I showed how to do. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.