Need help with PHP code

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rodman
Forum Newbie
Posts: 5
Joined: Thu May 20, 2004 6:42 am

Need help with PHP code

Post by rodman »

I need to fetch result from mySQL table and insert them in php file.
I have list of URL's in my mySQL table

Here is my file which should insert data in to key.php":
<?php
mysql_connect("localhost", "helpdemo_helpdem", "2ert45gy") or
die("Could not connect: " . mysql_error());
mysql_select_db("helpdemo_xcart");

$result = mysql_query("SELECT url FROM xcart_customers");

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo"{$row['url']},";
}

//Open a file in write mode
$fp = fopen("key.php", "w");
if(fwrite($fp, $row)) echo "writing=Ok";
else echo "writing=Error";
fclose($fp);

mysql_free_result($result);
?>

My "key.php" file:
<?php
echo "doman=www.v6e.cz,www.domain.com,www.rosu.com,www.url_from_mysql_here.com";
?>

So basically data should be inserted in format as it is in key.php file.
I need insert many url's from mySQL table to key.php, it should be inserted as it is in file above.
And I can not add anything else to "key.php" file, just url's in format as it is in file above.



any advices are welcome

Thanks a lot!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

?

Code: Select all

<?php
$fp = @fopen("key.php", "wb") or die("couldn't open key.php");

// write out the "header"
fwrite($fp, '<?php'."\n".'echo "');

$num = mysql_num_rows($result);
for($x = 0; $x < $num; $x++)
{
$row = mysql_fetch_assoc($result);
fwrite($fp, (($x !== 0)?',':'').$row['url']);
}

fwrite($fp, ""\n?>");
?>
Post Reply