views:

53

answers:

1

Hi all ..

I have a problem in using Samrtm Mysql and Jquery or Ajax and if I use a div auto reloader it shows a php error .. these are my files: accept.html (Template)

{literal}
<script type="text/javascript">
var auto_refresh = setInterval(
function ()
{
$('#invite').load('accept.php').fadeIn("slow");
}, 1000);
</script>
{/literal}
<div id="invite">
<fieldset>
<legend>Invites</legend>
{section name=ct loop=$invites}
You have an invite {$sender.user}<br />
<a href="?action=accept_contest&sid={$invites[ct].sendid}">Accept</a> | <a href="?action=cancel_contest&sid={$invites[ct].sendid}">Cancel</a>
{/section}
</fieldset>
</div>

accept.php (Main file)

<?php

$time = date("i:s",time("s"));
$sql_select_invite = mysql_query("SELECT * FROM invite WHERE recid = '$_SESSION[id]' AND accept = '0' AND endtime > '$time'");

while($invite = mysql_fetch_array($sql_select_invite)){
 $sender = mysql_fetch_array(mysql_query("SELECT * FROM member WHERE id = '$invite[sendid]'"));
    $contest->assign("sender", $sender);
    $invites[] = $invite;
}
$contest->assign("invites", $invites);

?>

and when I use the Jquery thing or Ajax it shows this error:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\AppServ\www\somefo\accept.php on line 6

Fatal error: Call to a member function assign() on a non-object in C:\AppServ\www\somefo\accept.php on line 11

I don't know why it shows this .. and I hope you answer me and what or how to solve this problem

Thanks ..

A: 

For php:

<?php

$sql = mysql_connect(......);  // connect to your server
if (!$sql) {
    die('Can\'t connect to database');
}

// select your database
mysql_select_db('your_db', $sql) or die('Can\'t select database');

$time = date("i:s",time("s"));
$sql_select_invite_query = "SELECT * FROM invite WHERE recid = '$_SESSION[id]' AND accept = '0' AND endtime > '$time'";

// print it if you want to see sql query to test it in phpmyadmin
echo $sql_select_invite_query; 
// --------------------------------------------------------------

$sql_select_invite = mysql_query($sql_select_invite_query, $sql);

if (!$sql_select_invite) {    // check for error in query
    die(mysql_error());       // and print it
}

$invites = array();  // don't forget to make an empty array

while($invite = mysql_fetch_array($sql_select_invite, $sql)){
    // this place is not good, just make it as you do before, with checks
    $sender = mysql_fetch_array(mysql_query("SELECT * FROM member WHERE id = '$invite[sendid]'"));

    $contest->assign("sender", $sender);
    $invites[] = $invite;
}
$contest->assign("invites", $invites);   // in your case there is no $invite 
                                         // array cause mysql_fetch_array returned
                                         // false and it is not defined before [while]

?>

For JS to test:

setInterval( function () {
  $('#invite').load('accept.php', function() {
    alert('loaded');  // it is to check content is loaded
  }).fadeIn("slow");
}, 1000);
silent
Thanks for your reply .. but now it says No database selected
phplover
you have to do this at the beginning of a script: 1. connect to db (mysql_connect), 2. select a database (mysql_select_db). Post edited.
silent
it has been solved but it hides the template instead of reloading it
phplover
what means hides? hides a div or ajax return empty data?
silent
hides the div ..
phplover
is there javascript in your ajax html template? Or maybe something in main html code? What code do you have that can hide your div?
silent
I don't know .. theres nothing that would hide the div
phplover
I think the problem is from the jquery script .. can you check it ??
phplover
maybe you can give us a url to test it?
silent
its not finished yet to get it online .. sorry for that, but is there another auto div reloader for jquery?
phplover
it is loaded for the first time right and on next time it is hiding. right? look at the post to test it.
silent
yeah right, wheres the post
phplover
i've just update it
silent
its says loaded but still hiding
phplover
try to remove fadeIn function
silent
same thing ....
phplover
try to put here full ajax reply
silent
what do you mean?
phplover
install firebug (firefox addition), click the "bug icon" at the bottom, go to the "Net" tab and check the ajax activity, especially the "Response" tab of request. And put that content here.
silent
this is the content: <meta http-equiv="content-type" content="text/html; charset=utf-8"><meta http-equiv="content-type" content="text/html; charset=utf-8">
phplover
if that is all, your php script return nothing and you should check it again. For example you can print something at the beginning of script to check it is really executing.
silent
I print something and this is the content: <meta http-equiv="content-type" content="text/html; charset=utf-8"><meta http-equiv="content-type" content="text/html; charset=utf-8">sometext
phplover
So your queries just return nothing. Print sql from queries to the result and try to execute it in phpmyadmin. So you can check some data is returning by your queries.
silent
how ? can you make a code for me?
phplover
my answer updated
silent
thanks so so much !! it worked finally :)
phplover
but why it uses high memory usage and the computer be slow ? is there another way for java ?
phplover
1 second update ajax is not very good solution.
silent