Forum Moderators: open

Message Too Old, No Replies

PHP in JS

Retrive a database using PHP and MySQL in JS.

         

MartinWeb

5:08 pm on Aug 20, 2009 (gmt 0)

10+ Year Member



Hello. I have been attempting to create a page that will refresh itself when a variable changes in a database.

function myMove() {

var php = "<?php
$q=\"SELECT * FROM variable where id='$theId'\";
$result=mysql_query($q) or die(\"Could not execute query : $q.\" . mysql_error());
while($row=mysql_fetch_array($result)){
$variable=$row[\"variable\"];

}
?>";

var check = setTimeout(function (){
myMove();
},100);
}

Additionally, I connect to the database at the top, as well as set the variable $theId. myMove() runs when the page loads.

When I attempt to run this, the page remains blank, and I get the following error message:


PHP Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /hermes/web09/b1333/moo.123star/cgi/board_games/chess/loggedIn/daily/board.php on line 1792
PHP Parse error: syntax error, unexpected T_CHARACTER, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /hermes/web09/b1333/moo.123star/cgi/board_games/chess/loggedIn/daily/board.php on line 1795

If you have any ideas, please tell me. Thank you.

CrazyMerlin

9:24 am on Aug 22, 2009 (gmt 0)

10+ Year Member



You need to keep your PHP code in a PHP file and call it using AJAX...it will simplify everything and will also allow you to run the PHP on it's own to confirm it returns what you expect.

It looks like whatever you are trying to do, you are doing so every 100 milliseconds, in which case you will never get the results you expect because that is simply too fast.

If you wish to be notified client-side when something on the server changes, i.e. a database field value, then you should do so using sockets, not direct HTTP like you are.

Using a socket you can send data from the server to the client only when the server data changes, and so you don't have to keep checking the server...which is resource intensive.

Your whole approach to the problem needs a cleaner solution or you will definitely run into more problems.

MartinWeb

5:29 pm on Aug 22, 2009 (gmt 0)

10+ Year Member



OK. Can you please tell me how to do that? I don't normally use AJAX.