Problem in calling store procedure in php

Reply

Join Date: Jul 2008
Posts: 1
Reputation: khanbaba is an unknown quantity at this point 
Solved Threads: 0
khanbaba khanbaba is offline Offline
Newbie Poster

Problem in calling store procedure in php

 
0
  #1
Jul 30th, 2008
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

  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

  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
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 100
Reputation: petr.pavel is an unknown quantity at this point 
Solved Threads: 14
petr.pavel's Avatar
petr.pavel petr.pavel is offline Offline
Junior Poster

Re: Problem in calling store procedure in php

 
0
  #2
Sep 4th, 2008
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
  1. $resource = mysql_connect(...);
  2. $resource->prepare(...);
You have probably confused it with mysqli() set of functions:
  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;
Petr 'PePa' Pavel

The more information you give the more relevant answer you get.
Please consider using "Add to ... Reputation" and mark your thread as Solved if you found what you were looking for. By giving feedback you help others.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC