Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

How do I write:

$count = mysql_num_rows($result);
print "<h3>$count metal prices available</h3>";

to a file, index.php?

I've tried:

echo "$count = mysql_num_rows($result);
print "<h3>$count metal prices available</h3>";" > index.php

but I don't understand how to escape the double quotes that are in the input.

Would it be better to use something other than echo? I'd rather not rewrite the whole PHP script if possible (it's longer than the 2 lines given in the example!).

share|improve this question
add comment

2 Answers

There would be several ways to do so:

 cat >index.php <<'EOT'
 $count = mysql_num_rows($result);
 print "<h3>$count metal prices available</h3>";
 EOT

or

 echo '$count = mysql_num_rows($result);
 print "<h3>$count metal prices available</h3>";' > index.php

or

 echo '$count = mysql_num_rows($result);' >index.php  # overwrites file if it already exists
 echo 'print "<h3>$count metal prices available</h3>";' >>index.php  # appends to file

there are much more possible ways - use this as a starting point for test things...

share|improve this answer
add comment

In bash, all you need to do is replace the outer quotes with single quotes:

echo '$count = mysql_num_rows($result);                                                                  
print "<h3>$count metal prices available</h3>";' > index.php

If you need to do more complicated stuff, you can echo multiple times using ">>" which appends instead of overwrites:

echo '$count = mysql_num_rows($result);' >> index.php
echo 'print "<h3>$count metal prices available</h3>";' >> index.php
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.