View Full Version : sql wont insert my varible values? not sure why

04-03-2009, 11:42 PM
hi all,

i am having some trouble with entering my data into a database table

i have loaded a rss feed and have managed to pull out the specific elements from it and i have echo'd them out to check that they are correct which they are, but for some reason it doesnt like my varibles

here is my code


$xmlDoc = new DOMDocument();

for ($i = 0; $i < 10; $i++) {
//get elements from "<channel>"
$channel_title = $channel->getElementsByTagName('title')->item(0)->childNodes->item(0)->nodeValue;
$channel_image = str_replace("30x30-50","100x100-75",$channel->getElementsByTagName('image')->item(0)->childNodes->item(0)->nodeValue);
$channel_link = $channel->getElementsByTagName('id')->item(0)->childNodes->item(0)->nodeValue;
$channel_album = $channel->getElementsByTagName('name')->item(0)->childNodes->item(0)->nodeValue;
$channel_artist = $channel->getElementsByTagName('artist')->item(0)->childNodes->item(0)->nodeValue;
$channel_cost = $channel->getElementsByTagName('price')->item(0)->childNodes->item(0)->nodeValue;

$sql = "INSERT INTO product_database (pID,store,productTitle) VALUES ($i,'itunes',$channel_title)";
$query = mysql_query($sql);

//output elements from "<channel>"

echo ("<a href='$channel_link'>".$channel_title."</a>");
echo("<br />");
echo $channel_album;
echo("<br />");
echo $channel_artist;
echo("<br />");
echo $channel_cost;
echo("<br />");
echo ("<img src='$channel_image'>");
echo("<br />");

it adds the $i and the storeName but nothing else?

any ideas where im going wrong, please


04-03-2009, 11:47 PM
ok i have solved it turns out you need to add '' around the varible name oops :)


04-04-2009, 12:14 AM
ok another problem,

i am trying to get my search function to search this table for all productTitles which are like $test

$test is the value that the user inputted into my search bar,

before i was using preg_match but not in my sql code can this be done in sql?

this is the code i was using

if (preg_match("/\b$test\b/i",productTitle)){
list($width, $height) = getimagesize($item->uri->mImage);


but i tried to add preg_match into my sql code and it didnt like the /\'s

here is my current code

$sql = "SELECT *
FROM product_database
WHERE productTitle LIKE '$test'";

$query = mysql_query($sql);
$row = mysql_fetch_array($query);
$num_rows = mysql_num_rows($query);
$title = $row['productTitle'];
for ($i < $num_rows; $i = 0; $i++){
echo $title;

if i change $test to poker i expect it to pull out poker face by lady gaga, but it doesnt even though its in the database. i have tried entering it directly into my sql engine but it still returns nothing?

how do i get it to pull out info similar or includes the string value of $test