Php Questions
<div class="IPBDescription">I'll be updating this regularly</div> Number ONE:
Can I not use superglobals in mySQL queries?
I tried this:
<!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->$query = "INSERT INTO tfposts (post_topic, post_author, post_body) VALUES ('" , $_POST['topic'] , "', '" , $_POST['author'] , "', '" , $_POST['postbody'], "')";<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->
But it didn't work. Do I have to define all of the form entries I take as separate variables?
Can I not use superglobals in mySQL queries?
I tried this:
<!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->$query = "INSERT INTO tfposts (post_topic, post_author, post_body) VALUES ('" , $_POST['topic'] , "', '" , $_POST['author'] , "', '" , $_POST['postbody'], "')";<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->
But it didn't work. Do I have to define all of the form entries I take as separate variables?
Comments
<!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->$query = "INSERT INTO tfposts (post_topic, post_author, post_body) VALUES ('".$_POST['topic']."', '".$_POST['author']."', '".$_POST['postbody']."')";<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->
Second off, make sure you do addslashes() to each of those variables in case your users decide to post a topic containing ' or " or \ :P
And you are doing mysql_query($query) right? heh.
I thought you always used commas when you jump out of inverted quotes? What's with the periods?
Edit - oh, is it because they're variables and not strings?
And for a reason I don't understand, I'm able to get away without using a function to addslashes and it still works (even when I use entries with backslashes and suchlike).
but anyway, I've never seen the comma used as a concat operator before. That's just wierd. You wanna use period.
Question TWO:
Formatting a date made with NOW(). Do I have to use PHP (exploding the string into variables that I can shuffle around) or is there a way to do it with the SQL query, or in the mySQL table itself?
To do it w/ PHP, check out <a href='http://us4.php.net/manual/en/function.strtotime.php' target='_blank'>strtotime</a> and read the user comments. There's another function you need there too, I think the unix timestamp conversion, but not sure. Its 3:20am.
To do it w/ MySQL, check out the <a href='http://dev.mysql.com/doc/mysql/en/date-and-time-functions.html' target='_blank'>DATE_FORMAT</a> function (about 1/5 down the page)
As far as which is better, well it depends on the situation. I use both about equally.
Mwahahaha.
Anyways, if you scroll down on the MySQL manual page I linked past the table, it gives you several examples:
<!--QuoteBegin--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> </td></tr><tr><td id='QUOTE'><!--QuoteEBegin-->mysql> SELECT DATE_FORMAT('1997-10-04 22:23:00', '%W %M %Y');
-> 'Saturday October 1997'
mysql> SELECT DATE_FORMAT('1997-10-04 22:23:00', '%H:%i:%s');
-> '22:23:00'
mysql> SELECT DATE_FORMAT('1997-10-04 22:23:00',
'%D %y %a %d %m %b %j');
-> '4th 97 Sat 04 10 Oct 277'
mysql> SELECT DATE_FORMAT('1997-10-04 22:23:00',
'%H %k %I %r %T %S %w');
-> '22 22 10 10:23:00 PM 22:23:00 00 6'
mysql> SELECT DATE_FORMAT('1999-01-01', '%X %V');
-> '1998 52'<!--QuoteEnd--></td></tr></table><div class='postcolor'><!--QuoteEEnd-->
SELECT *, DATE_FORMAT('time_post', '<format>') FROM tfposts ETC ETC?
For example, if you do SELECT * on a table w/ 50 different fields, and you only need 2, and you're pulling back 6000 records, that is a ton of waste.
However if you did SELECT name, id or whatever with just the 2 you need, then you save a lot of overhead.
So my code's as efficient as possible if I want to extract everything from the table?
<!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->require('mysql_connect.php') or die ("<p>A server error has occurred. Please try again later. We're sorry for the inconvenience.</p><p>Error description: " . mysql_error() . "</p>");<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->
Isn't 'or die' a global PHP thing? Why can't I apply it here? (the script runs fine until I try to access the database (and mysql_connect.php), where I get:
<!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->Warning: main(1): failed to open stream: No such file or directory in (address) on line 23
Fatal error: main(): Failed opening required '1' (include_path='(address)') in (address) on line 23<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->
The script works fine if I comment out the die section.
Also, should I use require('./mysql_connect.php') or ('mysql_connect.php') or is there no difference?
This is of course just my guessing though, I don't know for sure.
It wouldn't make sense to have that error message anyway, because its not like the file is going to up and disappear cause your mysql server went down...
Why when i do this:
<!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->mkdir(const_doc_root."tutorials/$newTutID/", 0777);
if ($_FILES['xml']['name'] != "") {
copy ($_FILES['xml']['tmp_name'],
const_doc_root."tutorials/$newTutID/tutorial.xml");
}
if ($_FILES['thumb']['name'] != "") {
copy ($_FILES['thumb']['tmp_name'],
const_doc_root."tutorials/$newTutID/thumb.jpg");
}
<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->
Do i get this:
<!--QuoteBegin--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> </td></tr><tr><td id='QUOTE'><!--QuoteEBegin-->Warning: mkdir(/export2/webroot/users/d4025579/hawxby/tutorials/10/):
Permission denied in
/export2/webroot/users/d4025579/hawxby/admin/tutorials/new/index.php on
line 27
Warning:
copy(/export2/webroot/users/d4025579/hawxby/tutorials/10/tutorial.xml):
failed to open stream: No such file or directory in
/export2/webroot/users/d4025579/hawxby/admin/tutorials/new/index.php on
line 30
Warning:
copy(/export2/webroot/users/d4025579/hawxby/tutorials/10/thumb.jpg):
failed to open stream: No such file or directory in
/export2/webroot/users/d4025579/hawxby/admin/tutorials/new/index.php on
line 34<!--QuoteEnd--></td></tr></table><div class='postcolor'><!--QuoteEEnd-->
My uni server admins claim they don't know why, damn them all to hell.