Jump to content


Photo

mysql_query kills my code


  • Please log in to reply
7 replies to this topic

#1 NathanSenn

NathanSenn

    Newbie

  • New Members
  • Pip
  • 3 posts

Posted 09 September 2013 - 01:30 PM

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


#2 gizmola

gizmola

    Advanced Member

  • Administrators
  • 4,126 posts
  • LocationLos Angeles, CA USA

Posted 09 September 2013 - 01:40 PM

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


#3 DavidAM

DavidAM

    Advanced Member

  • Gurus
  • 1,974 posts
  • LocationSpring, TX USA

Posted 09 September 2013 - 01:45 PM

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"

-- I haven't lost my mind, it's backed up on tape ... somewhere!

#4 NathanSenn

NathanSenn

    Newbie

  • New Members
  • Pip
  • 3 posts

Posted 09 September 2013 - 02:41 PM

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


#5 Barand

Barand

    Sen . ( ile || sei )

  • Gurus
  • 14,431 posts
  • LocationCheshire, UK

Posted 09 September 2013 - 04:19 PM

Check mysql_error() after the insert


moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts
|baaSelect| generate js and php code for dynamic linked dropdowns

 


#6 Psycho

Psycho

    Advanced Member

  • Gurus
  • 10,861 posts
  • LocationCanada

Posted 09 September 2013 - 05:45 PM

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

The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net

#7 NathanSenn

NathanSenn

    Newbie

  • New Members
  • Pip
  • 3 posts

Posted 14 September 2013 - 02:15 PM

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


#8 Psycho

Psycho

    Advanced Member

  • Gurus
  • 10,861 posts
  • LocationCanada

Posted 14 September 2013 - 05:31 PM

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.


The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com