Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This code is when i hardcore the sentence "Have a nice day!", it will echo out the exact same line. My question is what if i want to retrieve sentence from the database, instead of hard-coding it.

<?php
    $php_var = "Have a nice day!";
?>
  <html>
  <head>
  <title>Hello</title>
  </head>
  <body>

  <script>
    var js_var = "<?php echo $php_var; ?>";
    //var js_var = "Try123";
    document.writeln(js_var);
   //document.writeln(js_var);
  </script>
  </body>
  </html>

I am suppose to do something like this is it? but it cant work. It printed out "SELECT * FROM sen WHERE id=1 ;" on the page.

<?php
   $con = mysql_connect(localhost,root,password,database_name);
   $php_var = "SELECT * FROM sen WHERE id=1 ;";
?>


    <script>
  var js_var = "<?php echo  $php_var ; ?>";
  //var js_var = "Try123";
 document.writeln(js_var);
 //document.writeln(js_var);
</script>
share|improve this question
1  
currently you only assign your query to a variable you're supposed to run it on your connection and then extract the result, im currently looking up how to do this again. –  kpp Jun 25 at 6:53
    
Is this question answered? –  Wouter0100 Jun 27 at 6:57
    
No. @Wouter0100 –  user3773928 yesterday
add comment

3 Answers

You're not executing the query and fetching the result. Something like this should work:

<?php
$con = mysqli_connect(localhost,root,password,database_name);
$php_var = mysqli_fetch_assoc(mysqli_query($con, "SELECT * FROM sen WHERE id=1 LIMIT 1")); 
?>

<script>
  var js_var = "<?php echo  $php_var['id']; ?>";
  //var js_var = "Try123";
 document.writeln(js_var);
 //document.writeln(js_var);
</script>

Please be aware of some things:

  • Don't forgot error handling on the right way. (Not or die)
  • Check if the MySQL connecton was successfully made.
  • Possibility of MySQL injection
  • I've updated mysql_* to mysqli_*, this because mysql_* is deprecated and will being removed in the future.
share|improve this answer
1  
change it to mysqli_ ... there's no point on teaching deprecated functions –  gbestard Jun 25 at 6:58
    
Indeed, that's why I added the "be aware of things"-list :-)... –  Wouter0100 Jun 25 at 7:00
1  
Instead of showing someone how not to do it, just show them how it should be done, and add a sidenote about the deprecated functions. –  Cerbrus Jun 25 at 7:01
    
Updated my answer. :) –  Wouter0100 Jun 25 at 7:04
    
@Wouter0100 Cannot work. –  user3773928 Jun 25 at 7:29
show 2 more comments

My suggestion is that you create a Rest api with json response backend in PHP and then have a javascript frontend using like JQuery $get or something.

share|improve this answer
    
This answer is totally out of the scope of the question. –  Cerbrus Jun 25 at 7:02
add comment

Remove ; from the query. Use $php_var = "SELECT * FROM sen WHERE id=1";

share|improve this answer
    
-1. ; doesn't matter. And whole mysqli_query() and mysqli_fetch_assoc() are missing. Read Wouter0100's answer. –  Jigar Jun 25 at 7:18
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.