You are here: Re: Creating Stored Procedure from PHP « PHP SQL « IT news, forums, messages
Re: Creating Stored Procedure from PHP

Posted by J.O. Aho on 10/14/85 11:37

Tim Mickelson wrote:
> Hello
>
> I have this problem, making a script file to use the "mysql"
> application, I can create the file my_sp.sql, and launch as:
> mysql -u pippo -ppluto test < my_sp.sql
> This works fine, the problem is doing this from php, with the
> mysql_query, how should I change the delimiter, this is not
> a standard SQL query, so I get an error, I have really tried all
> different possiblities to fix this, please help me someone.

I have never done this from php, so I don't know if this will work or not.

> This works fine, my problem is, how can I create this from PHP,
> using the mysql libraries?
>
> <?
> $con = mysql_connect('localhost','pippo','pluto');
> mysql_select_db('test');
>
> // HOW SHOULD THIS QUERY BE??
> $query = "DELIMITER $$ ";
> $query.= "CREATE PROCEDURE test.users(name VARCHAR(30)) ";
> $query.= "BEGIN";
> $query.= "DECLARE comment VARCHAR(100);";
> $query.= "SET comment = CONCAT(name,' is added');";
> $query.= "INSERT INTO users (id,name,comment) VALUES
> (NULL,name,comment);";
> $query.= "END";
> $query.= "$$";
> $query.= "DELIMITER ;";
>
> mysql_query($query);
> mysql_close($con);
> ?>

This creates a one line string

$query="DELIMITER $$ CREATE PROCEDURE test.users(name VARCHAR(30))
BEGINDECLARE comment VARCHAR(100);SET comment = CONCAT(name,' is
added');INSERT INTO users (id,name,comment) VALUES
(NULL,name,comment);END$$DELIMITER ;"

and this isn't what you want, think the '$$' has to be the first and only
thing on a line to the mysql server, try this instead

<?
$con = mysql_connect('localhost','pippo','pluto');
mysql_select_db('test');

// HOW SHOULD THIS QUERY BE??

$query = "DELIMITER $$
CREATE PROCEDURE test.users(name VARCHAR(30))
BEGIN
DECLARE comment VARCHAR(100);
SET comment = CONCAT(name,' is added');
INSERT INTO users (id,name,comment) VALUES (NULL,name,comment);
END
$$
DELIMITER ;";

mysql_query($query);
mysql_close($con);
?>



//Aho

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация