简体   繁体   中英

how to create view and get data mysql php javascript

I have database 'testdatabase' and want to create a view having column 'value' from table 'testtabl'e , then want to sum the rows in value of view created before . Then store it in javascript array . I have put on this code but not able to know where I am making mistake . It does not show any thing not even a blank messagebox.

<script>



    var data=[
    <?php


$connection =mysql_connect("localhost","root","") or die ("we couldn't connect!");

mysql_select_db("testdatabase");

$query = mysql_query("CREATE VIEW view1 AS SELECT value FROM testtable") or die(mysql_error());

mysql_fetch_array($query);

$r2 = mysql_query("SELECT SUM(value) FROM view1;") or die(mysql_error());

 while($row = mysql_fetch_array($r2))
 {
       echo $row['value'].',';
     }
     ?>];


    window.alert(data);
    </script>

First, you're nesting PHP code in a JavaScript function which is a really bad idea. You will get errors that will be hard to track down if you continue to code like that. If you need data in your JavaScript you can output the PHP result or use AJAX to retrieve the value.

Here is an example that keeps things much cleaner:

<?php
$connection =mysql_connect("localhost","root","") or die ("we couldn't connect!");
mysql_select_db("testdatabase");
$query = mysql_query("CREATE OR REPLACE VIEW view1 AS SELECT value FROM testtable") or die(mysql_error());
$r2 = mysql_query("SELECT SUM(value) as value FROM view1;") or die(mysql_error());
$result = mysql_fetch_array($r2); // there is only one result
?>

<script>
    var data = [ <?php echo $result['value']; ?> ];
</script>

As stated in comments, the mysql_* functions are deprecated and have been removed in PHP 7. Anyone using PHP should learn how to use prepared statements with the MySQLi API or, preferably, the PDO API. The added benefit to using prepared statements is the prevention of SQL Injection attacks.

Second, 'value' is a MySQL keyword . You should change the name of the column. If you insist on keeping the column that name you will have to use back ticks to differentiate it:

CREATE VIEW `view1` AS SELECT `value` FROM `testtable`

SELECT SUM(`value`) FROM `view1`

In addition, there is no need to fetch an array after the first query, where you create the view. You are not using the result of the CREATE anywhere.

The name of the returned column changes when you add the function to it. Forcing the declared name 'as value' should work:

"SELECT SUM(value) as value FROM view1;"

Complete answer:

<script>
var data=[
<?php


$connection =mysql_connect("localhost","root","") or die ("we couldn't connect!");

mysql_select_db("testdatabase");

$query = mysql_query("CREATE OR REPLACE VIEW view1 AS SELECT value FROM testtable") or die(mysql_error());

$r2 = mysql_query("SELECT SUM(value) as value FROM view1;") or die(mysql_error());

 while($row = mysql_fetch_array($r2))
 {
   echo $row['value'].',';
 }
 ?>];


window.alert(data);
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM