将 Mysql 过程转换为 PHP 代码

Convert Mysql procedure into PHP code

我的 Mysql 数据库上有一个存储过程,但我需要更改它才能在 PHP 之前完成它。

我一直在四处寻找,我知道可以从 PHP 制作 SP,但它需要 Mysqli(我没有更新它,正在更新它由于个人原因不可能)或者至少这是我找到的唯一方法(source)。

那么对于只有 Mysql 的我来说,可以从 php 开始吗?还是我应该 "translate" 将程序转换为 PHP 格式?我的意思是不是制作一个过程而是一个函数(php 函数),它实际上对 PHP 代码(循环、selects、调用等)做同样的事情但不存储过程在数据库上。

这是我的程序

DELIMITER //

create procedure autor_products (in id int)
begin

update authors set authors_products= 
            (SELECT DISTINCT count(products_id)
FROM products
INNER JOIN authors_to_manufacturers as am ON am.manufacturers_id = products.manufacturers_id
WHERE authors_id = id)
WHERE manufacturers_id = id;

end
//

DELIMITER $$

CREATE PROCEDURE authors()

   BEGIN
      DECLARE a INT Default 0 ;
      DECLARE manu_length int;

      select max(manufacturers_id) into manu_length from authors;

      simple_loop: LOOP
         SET a=a+1;

         call autor_products(a);

         IF a >= manu_length THEN
            LEAVE simple_loop;
         END IF;
   END LOOP simple_loop;
END $$
DELIMITER ;

commit;

我设法做到了,这是 "working" 至少一次更新

<?php

require('includes/configure.php');  //To get the DB name, user and pass
require_once (DIR_FS_INC.'xtc_db_connect.inc.php');  //I use this to connect the DB

$conn = xtc_db_connect() or die('Unable to connect to database server!');

$manu_lenght="select max(manufacturers_id) from authors;";


for ($i = 0;$i<=$manu_lenght;$i++){  //"for" not in use nor not working if i replace 1 for $i

$update= " update authors set products_amount = (SELECT DISTINCT count(products_id)
FROM products
INNER JOIN authors_to_manufacturers as am ON am.manufacturers_id = products.manufacturers_id
WHERE authors_id = 1)    
WHERE manufacturers_id = 1;"; 
} //will only update 1 author with authors_id = 1 so i needed to make a loop, but if i replace 1 for $i. it doesn't update at all

$retval = mysql_query( $update, $conn );
if(! $retval )
{
  die('Could not update data: ' . mysql_error());
}
echo "Updated data successfully\n";
?>

我如何使用 $i 变量而不是特定数字? (所以它会因为循环而一次更新)

编辑:我的 manu_lenght 不工作(它不显示 select 的结果)但我只是将该部分更改为数字但它仍然不工作,有什么想法吗?

编辑 2:为什么这段代码不起作用?如果它只是 $a=1; 它就可以工作;但如果我循环或制作 $a.

的 bucle 则不会
$a = 1;
do{
$update= " update authors set products_amount = (SELECT DISTINCT count(products_id)
FROM products
INNER JOIN authors_to_manufacturers as am ON am.manufacturers_id = products.manufacturers_id
WHERE authors_id =$a)
WHERE manufacturers_id =$a;";
echo $a;
$a++;
}
while($a <= 10);

我做错了什么?

经过几次尝试,我得到了我需要的东西。

这是我的代码:

<?php


require('includes/configure.php');
require_once (DIR_FS_INC.'xtc_db_connect.inc.php');

$conn = xtc_db_connect() or die('Unable to connect to database server!'); 

//getting the total amount of authors into $manu_lenght variable
$manu_lenght = mysql_query("select max(manufacturers_id) as sum from authors;"  );


if(!$manu_lenght){
    die("DB query failed : " . mysql_error());
    $sum = "";

} //needed to fetch the result of the select query 
while ( $result = mysql_fetch_assoc($manu_lenght)){
    $sum = $result["sum"];
}
do{
    $author_update= " update authors set products_amount = (SELECT DISTINCT count(products_id)
    FROM products
    INNER JOIN authors_to_manufacturers as am ON am.manufacturers_id = products.manufacturers_id
    WHERE authors_id =$a)
    WHERE manufacturers_id =$a;";

    $a++;
    //while was here, so that's why it wasn't working
    $ret_author = mysql_query( $author_update, $conn );
}
while($a <= $sum);  //now is correct, it sends the query now

if(!$ret_author )
{
  die('Could not update data: ' . mysql_error());
}
echo "Updated authors successfully\n";

?>

我基本上做错的是过早结束循环,并且没有让 mysql_query 函数发送查询(在本例中为更新)

我没有调用过程,而是通过 PHP 进行了过程翻译。希望这对其他人有用。