如何在 SQL 中使用 PHP 在 SQL TIGGERING 中获取查询
HOW TO GET QUERY IN SQL USING PHP IN SQL TIGGERING
这是我的代码,我的 $sql 变量没有提供查询请帮我解决这个问题我尝试了这个但是我不能帮我解决这个问题
<?php
$connect = mysqli_connect("localhost", "root", "", "finger");
$f= "";
$l= "";
$sql = "CREATE TRIGGER `ersdmmmmecv` AFTER INSERT ON `event` FOR EACH ROW SELECT fname,Lname INTO $f,$l FROM user WHERE id=NEW.id;"
$result = mysqli_query($connect, $sql);
?>
您不能使用 MySQL 触发器来更新 PHP 变量。如果您希望 $f
和 $l
的值在您的 event
table 中插入新记录时更新,您需要完全在 PHP 中执行此操作.
按照这些思路应该可以工作(注意:我自己没有测试过):
$f = "";
$l = "";
$new_id = "id_value";
$insert = $connect->prepare("INSERT INTO `event` (`id`, `column2`, `column3`) VALUES (?, ?, ?)");
$insert->bind_param("sss", $new_id, "value2", "value3");
if ($insert->execute() === FALSE) {
echo 'Could not insert event: ' . $insert->error;
} else {
// If `event`.`id` is actually an AUTO_INCREMENT column, and you don't
// specify it in your INSERT query, use this here:
// $new_id = $insert->insert_id;
$select = $connect->prepare("SELECT `fname`, `Lname` FROM `user` WHERE `id` = ?");
$select->bind_param("s", $new_id);
$select->execute();
$select->bind_result($f, $l);
$success = $select->fetch();
if ($success !== TRUE) {
echo 'Could not update $f and $l with new values: '
. ($select->error ?: 'No user with id: ' . $new_id);
}
}
如果您的代码中有多个地方可以将数据插入 events
table,我会亲自将其包装在一个函数中,这样我就不必每次都重复此操作。
这是触发解决方案
<?php
$connect = mysqli_connect("localhost", "root", "", "finger");
$sql1 = "CREATE TRIGGER `ersdmmmmecv` AFTER INSERT ON `event` FOR EACH ROW INSERT INTO res (fres,lres) VALUES SELECT fname,Lname FROM user WHERE id=NEW.id;";
$result2 = mysqli_query($connect, $sql1);
$sql = "SELECT * FROM res;";
if( !( $selectRes = mysqli_query($connect, $sql) ) ){
echo 'Retrieval of data from Database Failed - #';
}else{
?>
<table border="2">
<thead>
<tr>
<th>fName</th>
<th>lname</th>
</tr>
</thead>
<tbody>
<?php
if( mysqli_num_rows( $selectRes )==0 ){
$print_output= '<tr><td colspan="4">No Rows Returned</td></tr>';
}else{
while( $row = mysqli_fetch_assoc( $selectRes ) ){
$print_output="<tr><td>{$row['fres']}</td><td>{$row['lres']}</td></tr>\n";
}
}
?>
</tbody>
</table>
<?php
try
{
$fp=pfsockopen("127.0.0.1", 80);
fputs($fp, $print_output);
fclose($fp);
echo 'Successfully Printed '.$print_output;
}
catch (Exception $e)
{
echo 'Caught exception: ', $e->getMessage(), "\n";
}
?>
<?php
}
?>
<?php
$sql2= "DROP TRIGGER ersdmmmmecv";
$result1 = mysqli_query($connect, $sql2);
$sql3= "DELETE FROM res;";
$result3 = mysqli_query($connect, $sql3);
?>
<script>
setTimeout(function () { window.location.reload(); }, 1*60*1000);
// just show current time stamp to see time of last refresh.
document.write(new Date());
</script>
这是我的代码,我的 $sql 变量没有提供查询请帮我解决这个问题我尝试了这个但是我不能帮我解决这个问题
<?php
$connect = mysqli_connect("localhost", "root", "", "finger");
$f= "";
$l= "";
$sql = "CREATE TRIGGER `ersdmmmmecv` AFTER INSERT ON `event` FOR EACH ROW SELECT fname,Lname INTO $f,$l FROM user WHERE id=NEW.id;"
$result = mysqli_query($connect, $sql);
?>
您不能使用 MySQL 触发器来更新 PHP 变量。如果您希望 $f
和 $l
的值在您的 event
table 中插入新记录时更新,您需要完全在 PHP 中执行此操作.
按照这些思路应该可以工作(注意:我自己没有测试过):
$f = "";
$l = "";
$new_id = "id_value";
$insert = $connect->prepare("INSERT INTO `event` (`id`, `column2`, `column3`) VALUES (?, ?, ?)");
$insert->bind_param("sss", $new_id, "value2", "value3");
if ($insert->execute() === FALSE) {
echo 'Could not insert event: ' . $insert->error;
} else {
// If `event`.`id` is actually an AUTO_INCREMENT column, and you don't
// specify it in your INSERT query, use this here:
// $new_id = $insert->insert_id;
$select = $connect->prepare("SELECT `fname`, `Lname` FROM `user` WHERE `id` = ?");
$select->bind_param("s", $new_id);
$select->execute();
$select->bind_result($f, $l);
$success = $select->fetch();
if ($success !== TRUE) {
echo 'Could not update $f and $l with new values: '
. ($select->error ?: 'No user with id: ' . $new_id);
}
}
如果您的代码中有多个地方可以将数据插入 events
table,我会亲自将其包装在一个函数中,这样我就不必每次都重复此操作。
这是触发解决方案
<?php
$connect = mysqli_connect("localhost", "root", "", "finger");
$sql1 = "CREATE TRIGGER `ersdmmmmecv` AFTER INSERT ON `event` FOR EACH ROW INSERT INTO res (fres,lres) VALUES SELECT fname,Lname FROM user WHERE id=NEW.id;";
$result2 = mysqli_query($connect, $sql1);
$sql = "SELECT * FROM res;";
if( !( $selectRes = mysqli_query($connect, $sql) ) ){
echo 'Retrieval of data from Database Failed - #';
}else{
?>
<table border="2">
<thead>
<tr>
<th>fName</th>
<th>lname</th>
</tr>
</thead>
<tbody>
<?php
if( mysqli_num_rows( $selectRes )==0 ){
$print_output= '<tr><td colspan="4">No Rows Returned</td></tr>';
}else{
while( $row = mysqli_fetch_assoc( $selectRes ) ){
$print_output="<tr><td>{$row['fres']}</td><td>{$row['lres']}</td></tr>\n";
}
}
?>
</tbody>
</table>
<?php
try
{
$fp=pfsockopen("127.0.0.1", 80);
fputs($fp, $print_output);
fclose($fp);
echo 'Successfully Printed '.$print_output;
}
catch (Exception $e)
{
echo 'Caught exception: ', $e->getMessage(), "\n";
}
?>
<?php
}
?>
<?php
$sql2= "DROP TRIGGER ersdmmmmecv";
$result1 = mysqli_query($connect, $sql2);
$sql3= "DELETE FROM res;";
$result3 = mysqli_query($connect, $sql3);
?>
<script>
setTimeout(function () { window.location.reload(); }, 1*60*1000);
// just show current time stamp to see time of last refresh.
document.write(new Date());
</script>