0

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>
2
  • 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. Commented Jun 25, 2014 at 6:53
  • Is this question answered? Commented Jun 27, 2014 at 6:57

3 Answers 3

4

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.
Sign up to request clarification or add additional context in comments.

4 Comments

change it to mysqli_ ... there's no point on teaching deprecated functions
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.
@Wouter0100 Its giving this two errors- Warning: mysqli_query() expects at least 2 parameters, 1 given in Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, null given in
@user3773928 Excuus me, I always use the Object oriented one. I've edited my answer, please try again. :)
0

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.

1 Comment

This answer is totally out of the scope of the question.
0

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

1 Comment

-1. ; doesn't matter. And whole mysqli_query() and mysqli_fetch_assoc() are missing. Read Wouter0100's answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.