943,704 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 1664
  • PHP RSS
Jul 30th, 2008
0

Problem in calling store procedure in php

Expand Post »
Hi

Can anyone please help me sort this problem out

I create store procedure in mysql and I want to call this in php. The store procedure is executing ok in mysql but when I call this into php I get fatal error: call to a member function prepare() on a non-object in (path of file) store procedure line. can you tell be with below codes that where I am doing wrong. please help me thanks

Below is my store procedure

PHP Syntax (Toggle Plain Text)
  1. DELIMITER $$
  2.  
  3. DROP PROCEDURE IF EXISTS `publication_search`.`searchPublications` $$
  4.  
  5. CREATE DEFINER=`root`@`localhost` PROCEDURE `searchPublications`(IN author VARCHAR(100),
  6.  
  7. IN publication_title VARCHAR(250),
  8.  
  9. IN co_author VARCHAR(100),
  10.  
  11. IN group_name VARCHAR(250),
  12.  
  13. IN fromDate INT,
  14.  
  15. IN toDate INT,
  16.  
  17. IN paper_type VARCHAR(100))
  18.  
  19. BEGIN
  20.  
  21. SELECT * from publication p
  22.  
  23. where
  24. author is NULL OR (
  25.  
  26. p.pub_id IN
  27.  
  28. (SELECT sp.pub_id from staff_publication sp
  29.  
  30. where sp.staff_id =
  31.  
  32. (select s.staff_id from staff s where
  33.  
  34. s.name = author))) AND
  35.  
  36. (title is NULL OR
  37.  
  38. p.title = title) AND
  39.  
  40. (co_author is NULL OR p.co_author = co_author) AND
  41.  
  42. (group_name is NULL OR (p.pub_id IN (SELECT gp.pub_id from group_publication gp
  43.  
  44. where gp.Group_id = (SELECT g.group_id from group1 g
  45.  
  46. where g.group_name = group_name)) )) AND
  47.  
  48. ((fromDate is NULL OR p.year >= fromDate) AND
  49.  
  50. (toDate is NULL OR p.year <= toDate)) AND
  51.  
  52. (paper_type is NULL OR p.type = paper_type);
  53.  
  54.  
  55.  
  56.  
  57. END $$
  58.  
  59.  
  60.  
  61. DELIMITER ;
___________________________________________________________________________

//Here is my pho code where I connect this first to database and then call store procedure in php. I have searching form so when I click on search button that connect to the database but not calling store proceduer

PHP Syntax (Toggle Plain Text)
  1. <?php
  2. $submit = $_POST['submit'];
  3.  
  4. if($submit)
  5. {
  6. $author_keyword = $_POST['author_keyword'];
  7. $pub_title_keyword = $_POST['pub_title_keyword'];
  8. $co_author_keyword = $_POST['co_author_keyword'];
  9. $group_name_keyword = $_POST['group_name_keyword'];
  10. $year1 = $_POST['year1'];
  11. $year2 = $_POST['year2'];
  12. $select = $_POST['select'];
  13.  
  14. $server = "localhost";
  15. $user = "shahid";
  16. $password = "khan";
  17. $database_name = "publication_search";
  18.  
  19. //database connection (1. creation)
  20. $connection = @mysql_connect($server,$user,$password); //method to connect with mysql (three parameters server,user,password
  21.  
  22. //error detection when connecting to mysql
  23.  
  24. if (!$connection)
  25. {
  26. die("connection with MySQL is failed".mysql_error()); //die() is for print the message and will show the error
  27. }//end of if (connection error)
  28. else
  29. echo "Database is connected";
  30.  
  31. //(2. selecting a database
  32. $database_select = mysql_select_db($database_name,$connection);
  33.  
  34. if(!$database_select)
  35. {
  36. die("database selection is failed".mysql_error()); //die() is for print the message and show the error
  37. }
  38. else
  39. echo"<br> Database is selected Congrate!";
  40. $mysql = 'CALL searchPublications(?,?,?,?,?,?,?)';
  41. $stmt = $connection -> prepare($mysql);
  42. $author_keyword;
  43. $pub_title_keyword;
  44. $con_author_keyword;
  45. $group_name_keyword;
  46. $year1;
  47. $year2;
  48. $select;
  49.  
  50. $stmt -> bindParam(1, author, PRO:: PARM_STR, 100);
  51. $stmt -> bindParam(2, publication_title, PRO:: PARM_STR, 250);
  52. $stmt -> bindParam(3, co_author, PRO:: PARM_STR, 100);
  53. $stmt -> bindParam(4, group_name, PRO:: PARM_STR, 250);
  54. $stmt -> bindParam(5, fromDate, PDO:: PARAM_INT);
  55. $stmt -> bindParam(6, toDate, PDO:: PARAM_INT);
  56. $stmt -> bindParam(7, paper_type, PRO:: PARM, 100);
  57.  
  58. $rows = $stmt ->fetchAll(PRO::FETCH_NUM);
  59. if ($rows) {
  60. print_r($rows);
  61. }
  62. }
  63. ?>
Last edited by Tekmaven; Jul 30th, 2008 at 9:55 am. Reason: Code tags
Reputation Points: 10
Solved Threads: 0
Newbie Poster
khanbaba is offline Offline
1 posts
since Jul 2008
Sep 4th, 2008
0

Re: Problem in calling store procedure in php

Hi Khanbaba,
the problem is not related to your stored procedure.
mysql_connect doesn't return an object but a resource pointer. Therefore you cannot use
php Syntax (Toggle Plain Text)
  1. $resource = mysql_connect(...);
  2. $resource->prepare(...);
You have probably confused it with mysqli() set of functions:
php Syntax (Toggle Plain Text)
  1. $mysqli = new mysqli(...);
  2. $stmt = $mysqli->prepare(...);
btw: if you want to display a value of $author_keyword you have to type
echo $author_keyword; not just $author_keyword;
Reputation Points: 27
Solved Threads: 16
Junior Poster
petr.pavel is offline Offline
116 posts
since Mar 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: how to upload images to mysql with a foreign key using php
Next Thread in PHP Forum Timeline: Problem with none object property





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC