Re: connecting php long-polling to the database
| From: | Ashley Sheridan | Date: | Wed, 04 Feb 2015 20:49:40 +0000 |
| Subject: | Re: connecting php long-polling to the database | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-324384@lists.php.net to get a copy of this message | ||
On Wed, 2015-02-04 at 23:45 +0300, hadi wrote:
> Thank you Ash for your reply.
>
> When I open my php script direct I can see my table. Working fine.
> But throw AJAX not working.
> I think the problem from server.php script, can you please check my code in your end.
>
> Thanks and regards
> Hadi
>
> > On Wed, 2015-02-04 at 22:47 +0300, hadi wrote:
> > > i have script,php-long-polling, im unable to connected to my database.
> > > can someone look to my script and let me know where im wrong. i wanted
> > > to connect to my database and display database table in the html page.
> > >
> > > index.html
> > >
> > > <html>
> > > <head>
> > > <script type="text/javascript"
> > >
> > > src="http://code.jquery.com/jquery.min.js"></script>
> > > <script type="text/javascript"
> > > src="client.js"></script> </head>
> > > <body>
> > > <h1>Response from server:</h1>
> > > <div id="response"></div>
> > > </body>
> > > </html>
> > > server.php
> > >
> > > <?php
> > >
> > > // set php runtime to unlimited
> > >
> > > $servername = "localhost";
> > > $username = "recorduser";
> > > $password = "recorduser";
> > > $database = "record";
> > >
> > > // Create connection
> > > $conn = mysqli_connect($servername, $username, $password,
> > $database);
> > > // Check connection if (!$conn) { die("Connection failed: " .
> > > mysqli_connect_error()); }
> > >
> > > $sql = "SELECT * from username ORDER BY id DESC LIMIT 1";
> > > $result1 = mysqli_query($conn, $sql);
> > >
> > > set_time_limit(0);
> > > // where does the data come from ? In real world this would be a SQL
> > > query or something $data_source_file = '$result1'; // main loop while
> > > (true) { // if ajax request has send a timestamp, then $last_ajax_call
> > > = timestamp, else $last_ajax_call = null $last_ajax_call =
> > > isset($_GET['timestamp']) ? (int)$_GET['timestamp'] :
> > > null;
> > > // PHP caches file data, like requesting the size of a file, by default.
> > > clearstatcache() clears that cache
> > > clearstatcache();
> > > // get timestamp of when file has been changed the last time
> > > $last_change_in_data_file = filemtime($data_source_file); // if no
> > > timestamp delivered via ajax or data.txt has been changed SINCE last
> > > ajax timestamp if ($last_ajax_call == null ||
> > > $last_change_in_data_file > $last_ajax_call) {
> > > // get content of data.txt
> > > #$data = file_get_contents($data_source_file);
> > >
> > > $data = while($row = mysqli_fetch_array($result1))
> > >
> > > {
> > >
> > > echo "<tr>";
> > > echo "<td>" . $row['name'] . "</td>";
> > > echo "<td>" . $row['lastname'] . "</td>";
> > > echo "</tr>"; } // put
> > > data.txt's content and timestamp of last data.txt change into array
> > > $result = array(
> > > 'data_from_file' => $data,
> > > 'timestamp' => $last_change_in_data_file
> > > );
> > > // encode to JSON, render the result (for AJAX)
> > > $json = json_encode($result);
> > > echo $json;
> > > // leave this loop step
> > > break;
> > > } else {
> > > // wait for 1 sec (not very sexy as this blocks the PHP/Apache
> > > process, but that's how it goes)
> > > sleep( 1 );
> > > continue;
> > > }
> > > }
> > >
> > > ?>
> > > client.js
> > >
> > > /**
> > > * AJAX long-polling
> > > *
> > > * 1. sends a request to the server (without a timestamp parameter)
> > > * 2. waits for an answer from server.php (which can take forever)
> > > * 3. if server.php responds (whenever), put data_from_file into
> > > #response
> > > * 4. and call the function again
> > > *
> > > * @param timestamp
> > > */
> > >
> > > function getContent(timestamp)
> > > {
> > > var queryString = {'timestamp' : timestamp};
> > >
> > > $.ajax(
> > > {
> > > type: 'GET',
> > > url:
> > > 'http://192.168.206.129/record/server.php',
> > > data: queryString,
> > > success: function(data){
> > > // put result data into "obj"
> > > var obj = jQuery.parseJSON(data);
> > > // put the data_from_file into #response
> > > $('#response').html(obj.data_from_file);
> > > // call the function again, this time with the timestamp
> > > we just got from server.php
> > > getContent(obj.timestamp);
> > > }
> > > }
> > > );
> > > }
> > >
> > > // initialize jQuery
> > > $(function() {
> > > getContent();
> > > });
> > >
> > >
> >
> >
> > What happens when you make a direct request to the PHP script, i.e. not via
> > an AJAX request. What do you see? Are you getting any connection errors, or
> > output? If not, what do your error logs say?
> >
> > --
> > Thanks,
> > Ash
> > http://www.ashleysheridan.co.uk
> >
> >
> >
> >
> > --
> > PHP General Mailing List (http://www.php.net/)
> > To unsubscribe, visit: http://www.php.net/unsub.php
>
>
>
First, please do not top post, the list rules do ask that you bottom
post.
What do you see in your browser network inspector? Is the query
parameter being passed correctly? Are the URLs that both your AJAX
request and the direct call in your browser the same?
Also, what do the error logs say?
--
Thanks,
Ash
http://www.ashleysheridan.co.uk