0

I would like to ask why is this query unable to create the Procedure from the .php file upon execution from a link ? So far the queries works within the SQL as shown here: Original Code Here

**Update I am able to execute the query 1-3 but not 0 and 4.

It's not able to run query0 to drop the database to make a new one and also unable to CALL the Procedure ...

<?php
    function Connection(){
        $server="SERVER";
        $user="ADmin";
        $pass="password";
        $db="Apex";


    $query0= " DROP DATABASE IF EXISTS Apex";
    $query1= "CREATE DATABASE Apex";
    $query2= "CREATE TABLE Apex.Imagevalues(id int NOT NULL)";
    $query3= "CREATE PROCEDURE Apex.MYLOOP()
    BEGIN 
          DECLARE i int;
          DECLARE str varchar(255);
          SET i = 0;
          WHILE i < 32 DO
                   SET str = CONCAT('Data_',i);
                   SET @sql = CONCAT('ALTER TABLE Apex1.Imagevalues ADD ',str,' DECIMAL(2,2);');
                   SET i = i + 1;
                   PREPARE stmt FROM @sql;
                   EXECUTE stmt;
                   DEALLOCATE PREPARE stmt;
          END WHILE;
     END";
     $query4=" CALL MYLOOP() "; 

        $connection = mysqli_connect($server, $user, $pass);
        if (!$connection) {
            die('MySQL ERROR: ' . mysql_error());
        }

        if ( !mysqli_select_db($connection, $db) )
        {
        
        mysqli_query($connection,$query0);
        mysqli_query($connection,$query1);
        mysqli_query($connection,$query2);
        mysqli_query($connection,$query3);
        mysqli_query($connection,$query4);  
        }

        return $connection;
    }
    ?>
Dharman
  • 30,962
  • 25
  • 85
  • 135
  • 1
    Have you tried checking [for errors](https://www.php.net/manual/en/mysqli.error.php) when making the queries? It might give you some help if you know what the server actually says. – M. Eriksson Mar 07 '21 at 08:44
  • You need to reselect the database after `$query1`. If you enable mysqli error reporting you will see error `No database selected` – Dharman Mar 07 '21 at 23:27
  • @Dharman how do I insert the mysqli error reporting to a php ? I read through the document but unsure how it works –  Mar 08 '21 at 02:38

0 Answers0