duangou6446 2015-04-21 15:58
浏览 44

使用js与php错误

hey guys i have a database and am trying to fetch some result ..and i can succesfully fetch the result with php..What i need is to run the result after 3 seconds ..So i used 'setTimeout()` in js but it didnt worked out..My code

<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
@$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}


$sql = 'SELECT age FROM honey';
mysql_select_db('test');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
 echo    '<script> 
 var c = 3000;
setTimeout(function() { '<?php echo $row['age'];'}, c);
 </script>';
}
mysql_close($conn);
?>

The error i get is Parse error: syntax error, unexpected '?' in C:\wamp\wwwefresh\index.php on line 24 ..

how can i properl use js inside this php code..Thanx for the help

  • 写回答

3条回答 默认 最新

  • duanniu3385 2015-04-21 16:01
    关注

    You cannot use client-side JS "inside" your server-side PHP. PHP is parsed and run on the server side before your client even gets (and parses and executes) the resulting JS. If you need to set a timer, you either need to do it on the server (yuck) or do it on the client (having the client issue another call to the server).

    评论

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等