Jump to content

Recommended Posts

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);
    ?>
Link to comment
https://forums.phpfreaks.com/topic/282020-mysql_query-kills-my-code/
Share on other sites

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']}')");

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"

Its not making my page quit working now but its not adding anything to the DB
 
This is my Table in my database
 

 
 

s_fulltext.png id url title entryUri description status photo date ownerid allowView allowComments allowRate tags categories views rate rateCount commentsCount featured dot.gif Edit dot.gif Copy dot.gif 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 dot.gif

 

 

 

 

 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);
    ?>

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);

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);
           
        
            }
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.