0

I was wondering how to get values from mysql?

In php i would use something like

$sql = mysql_query("SELECT * FROM table");
while($row = mysql_fetch_array($sql)){
    $value = $row['name'];
}

But how do I get something from $value in jquery? Say I want to use a value from $value and put in a

var Value = $value;

or something like that?

Thank you

1
  • do you want to make php communicate with your javascript? Commented Apr 19, 2011 at 3:19

4 Answers 4

2
var Value = <?php echo json_encode($value) ?>;

Assuming $value contains a string, it will be encoded as ".." after escaping any double quote characters present in the string itself.

Sign up to request clarification or add additional context in comments.

Comments

2

You're trying to combine server-side scripting with client-side scripting here. I suggest you output data from your PHP code like this:

<div id="myDiv"><?php echo $value; ?></div>

and then write your jQuery code to pick it from there:

var Value = $('#myDiv').text();

4 Comments

And just like this you cluttering up your DOM model.
From what I understood, he never mentioned it was supposed to be done with inline JavaScript. I'm sure he can take it from here :)
What does this mean by cluttering up my DOM and inline javascript?
@andrewliu It means there's a <div node in your document whose only purpose is passing the value, which is kinda dirty. And the answer by @Anurang just outputs the string inside your <script> tag.
1

You could do something like:

var Value = "<?php echo $value ?>";

Comments

1
<script type='text/javascript'>
var value = <?php echo "'$value'"; ?>
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.