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:
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:
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, 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.
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.
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:
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:
About your second "empty query" question. Just remove the following code: if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } PHP: