'Error: Query was empty' when I insert and read the data.

Discussion in 'PHP' started by philb, Nov 13, 2011.

  1. #1
    I'm inserting data from a XML datafeed into a mysql database

    Everything is working fine but I'm getting a 'Error: Query was empty' when I insert and read the data.

    Here's the code I'm using to insert the data.

    Can anyone help?

    <?php
    include('../shop/dbconn.php');
    
    $con = mysql_connect(localhost,$username,$password);
    if (!$con)
    {
    die('Could not connect: ' . mysql_error());
    }
    
    mysql_select_db("#########", $con);
    
    $xml = simplexml_load_file("../shop/datafeed_79329.xml");
    
    foreach($xml->merchant->prod as $product){ 
    $name = $product->text->name;
    $awThumb = $product->uri->awThumb;
    $buynow = $product->price->buynow;
    $mcat = $product->cat->mCat;
    $awTrack = $product->uri->awTrack;
    
    mysql_query("INSERT INTO shop (name, awThumb, buynow, mcat, awtrack)
    VALUES ('$name', '$awThumb', '$buynow', '$mcat', '$awTrack')");
    }
    
    
    if (!mysql_query($sql,$con))
    {
    die('Error: ' . mysql_error());
    }
    echo "1 record added";
    
    mysql_close($con)
    ?>
    
    PHP:
     
    philb, Nov 13, 2011 IP
  2. 2WDH.com

    2WDH.com Active Member

    Messages:
    143
    Likes Received:
    3
    Best Answers:
    5
    Trophy Points:
    68
    #2
    Hi.

    Probably following part of the code gives you an error:
    if (!mysql_query($sql,$con))
    {
    die('Error: ' . mysql_error());
    }
    PHP:
    , because $sql is not set.
    It looks like this is what you need:
    <?php
    include('../shop/dbconn.php');
    
    $con = mysql_connect(localhost,$username,$password);
    if (!$con)
    {
    die('Could not connect: ' . mysql_error());
    }
    
    mysql_select_db("#########", $con);
    
    $xml = simplexml_load_file("../shop/datafeed_79329.xml");
    
    foreach($xml->merchant->prod as $product){
    $name = $product->text->name;
    $awThumb = $product->uri->awThumb;
    $buynow = $product->price->buynow;
    $mcat = $product->cat->mCat;
    $awTrack = $product->uri->awTrack;
    
    $sql = "INSERT INTO shop (name, awThumb, buynow, mcat, awtrack)
    VALUES ('$name', '$awThumb', '$buynow', '$mcat', '$awTrack')";
    
    if (!mysql_query($sql,$con))
    {
    die('Error: ' . mysql_error());
    }
    
    echo "1 record added";
    
    }
    
    mysql_close($con);
    ?>
    PHP:
     
    2WDH.com, Nov 13, 2011 IP
  3. philb

    philb Peon

    Messages:
    61
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I tried the

    $sql but it only seemed to insert the first entry.

    I'm loading about 1207 products from an XML file I have.

    The code I have works but gives me an error message,

    It would be good to lose the error message and be told, "congratulations, 1207 have been uploaded"

    Can anyone help, here's the code.

    <?php
    include('../shop/dbconn.php');
    $con = mysql_connect(localhost,$username,$password);
    if (!$con)
    {
    die('Could not connect: ' . mysql_error());
    }
    
    
    mysql_select_db("###########", $con);
    
    
    $xml = simplexml_load_file("../shop/datafeed_79329.xml");
    
    
    foreach($xml->merchant->prod as $product){ 
    $name = $product->text->name;
    $awThumb = $product->uri->awThumb;
    $buynow = $product->price->buynow;
    $mcat = $product->cat->mCat;
    $awTrack = $product->uri->awTrack;
    $descrip = $product->text->desc;
    
    mysql_query("INSERT INTO shop2 (name, awThumb, buynow, mcat, awTrack, descrip)
    VALUES ('$name', '$awThumb', '$buynow', '$mcat', '$awTrack', '$descrip')");
    }
    
    
    if (!mysql_query($sql,$con))
    {
    die('Error: ' . mysql_error());
    }
    echo "1 record added";
    
    
    mysql_close($con);
    ?>
    PHP:
     
    philb, Nov 14, 2011 IP
  4. 2WDH.com

    2WDH.com Active Member

    Messages:
    143
    Likes Received:
    3
    Best Answers:
    5
    Trophy Points:
    68
    #4
    Philb,

    It looks like you just tried to modify your code based on our reply, but didn't notice all the changes. Please just try the whole code we have stated in our previous reply (simply replace your whole code with it and don't forget to enter DB name instead of #########). It should work as desired (it will add all the records), showing you "1 record added" for each record it inserts.

    To have the "congratulations, 1207 have been uploaded" message you can add an inserted record counter inside foreach. But we would recommend to try the mentioned code first, and confirm that it works.
     
    Last edited: Nov 14, 2011
    2WDH.com, Nov 14, 2011 IP
  5. philb

    philb Peon

    Messages:
    61
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks for getting back to me.

    I've tried the first code you gave me again and it's working, inserting all 1207 entries.

    I must have done something wrong the first time I tried it.

    Thank you very much.
     
    philb, Nov 14, 2011 IP
  6. philb

    philb Peon

    Messages:
    61
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Actually while I'm on topic I'm also having the same Error: query was empty when I read the products from the database.

    Can you tell me where I'm going wrong?

        <?php
    include('../dbconn.php');
    
    $con = mysql_connect(localhost,$username,$password);
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
    
    mysql_select_db("philb_bdolls", $con);
    //SELECT * FROM tablename WHERE field1-name='1'
    $query="SELECT * FROM shop WHERE mcat='Bondage Kits' ORDER BY buynow ASC";
    $result=mysql_query($query);
    $nrows = mysql_num_rows($result);
    
    echo "<table>";
    for ($i=0;$i<$nrows;$i++)
    {
    	$n = $i + 1;
    	$row = mysql_fetch_array($result);
    	extract($row);
     ?>
     
      <table border="0" cellpadding="0" cellspacing="10">
      <tr>
        <td><a href="<?php echo $awtrack ?>"><img src="<?php echo $awThumb  ?>" width="80" /></a></td>
        <td width="400" valign="top" class="descrip"><a href="<?php echo $awtrack ?>"><?php echo $name  ?></a><br />
        <span class="descript"><?php echo $descrip; ?> <a href="<?php echo $awtrack ?>">...read more</a></span></td>
        <td width="100" valign="top">£ <?php echo $buynow;  ?></td>
      </tr>
      <?php
    }
     ?> 
    </table>
    
    <?php
    if (!mysql_query($sql,$con))
      {
      die('Error: ' . mysql_error());
      }
    
    mysql_close($con)
    ?>
    PHP:
     
    philb, Nov 14, 2011 IP
  7. 2WDH.com

    2WDH.com Active Member

    Messages:
    143
    Likes Received:
    3
    Best Answers:
    5
    Trophy Points:
    68
    #7
    Here is the code with inserted products counter for you:
    <?php
    include('../shop/dbconn.php');
    
    $con = mysql_connect(localhost,$username,$password);
    if (!$con)
    {
    die('Could not connect: ' . mysql_error());
    }
    
    mysql_select_db("#########", $con);
    
    $xml = simplexml_load_file("../shop/datafeed_79329.xml");
    $insertedProductsCounter = 0;
    
    foreach($xml->merchant->prod as $product){
    $name = $product->text->name;
    $awThumb = $product->uri->awThumb;
    $buynow = $product->price->buynow;
    $mcat = $product->cat->mCat;
    $awTrack = $product->uri->awTrack;
    
    $sql = "INSERT INTO shop (name, awThumb, buynow, mcat, awtrack)
    VALUES ('$name', '$awThumb', '$buynow', '$mcat', '$awTrack')";
    
    if (!mysql_query($sql,$con))
    {
    die('Error: ' . mysql_error());
    }
    else
    {
    $insertedProductsCounter++;
    }
    
    }
    
    mysql_close($con);
    
    echo "congratulations, $insertedProductsCounter products have been uploaded";
    ?>
    PHP:
     
    2WDH.com, Nov 14, 2011 IP
  8. 2WDH.com

    2WDH.com Active Member

    Messages:
    143
    Likes Received:
    3
    Best Answers:
    5
    Trophy Points:
    68
    #8
    About your second "empty query" question. Just remove the following code:
    if (!mysql_query($sql,$con))
      {
      die('Error: ' . mysql_error());
      }
    PHP:
     
    2WDH.com, Nov 14, 2011 IP
  9. philb

    philb Peon

    Messages:
    61
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    You're a start.

    I'm very grateful for your help.

    Thanks

    Phil
     
    philb, Nov 14, 2011 IP
  10. 2WDH.com

    2WDH.com Active Member

    Messages:
    143
    Likes Received:
    3
    Best Answers:
    5
    Trophy Points:
    68
    #10
    Phil,

    No problem. Feel free to ask the programming and/or hosting related questions.
     
    2WDH.com, Nov 14, 2011 IP