mysql php array to javascript functions

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
codebrick
Forum Newbie
Posts: 1
Joined: Mon Apr 28, 2008 2:00 am

mysql php array to javascript functions

Post by codebrick »

I'd like to be able to push each data cell in a sql query into a php array which I can then call upon in javascript...so something like this...
<?php

mysql_connect("localhost","root","password") or die (mysql_error("COULD NOT CONNECT TO HOST"));
mysql_select_db("database") or die (mysql_error("COULD NOT FIND DATABASE"));

$ids = mysql_query("SELECT * FROM location");

while ($row = mysql_fetch_array($ids, MYSQL_ASSOC)) {


$num1 = $row["firstNum"];
$num2 = $row["secondNum"];

//Here is where I want the array to function correctly.
//Lets say $num1[0] #=> 35
// $num2[0] #=> 10


}



mysql_free_result($ids);

?>

<script>

for(var i = 0; i<100; i++) {

//this is where $lat would matter, i want to push through all the numbers according to what index 'i' is on
document.write("Number <? echo "$lat | $long" ?> is awesome");

}

</script>

Any Suggestions?

Thanks,

- brIcK
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Re: mysql php array to javascript functions

Post by andym01480 »

Code: Select all

 
<script>
for(var i = 0; i<100; i++) {
 
<?php
mysql_connect("localhost","root","password") or die (mysql_error());
mysql_select_db("database") or die (mysql_error());
$ids = mysql_query("SELECT * FROM location") OR DIE(mysql_error());
while ($row = mysql_fetch_array($ids)) {
echo "document.write(\"Number {$row[0]} | {$row[1]} is awesome\");"; //might need an \ before first ; try it
}
mysql_free_result($ids);
?>
}
</script>
Post Reply