Foros del Web » Programando para Internet » PHP »

Recuperar parametro salida de un sp con mysqli

Estas en el tema de Recuperar parametro salida de un sp con mysqli en el foro de PHP en Foros del Web. Hola a todos, quisiera saber si alguien podria ponerme el codigo en php de ejemplo de como ejecuta un sp de mysql usando mysqli y ...
  #1 (permalink)  
Antiguo 01/05/2012, 04:53
 
Fecha de Ingreso: abril-2010
Mensajes: 298
Antigüedad: 14 años
Puntos: 1
Recuperar parametro salida de un sp con mysqli

Hola a todos, quisiera saber si alguien podria ponerme el codigo en php de ejemplo de como ejecuta un sp de mysql usando mysqli y recupera un parametro de salida del mismo, he buscado y probado todo lo que encuentro y no consigo realizarlo.

He probado cosas como:
Código PHP:
Ver original
  1. $res=mysqli_query($cn,"call I_Familia(@res,'".$this->quitar($nombre)."');");
  2.    
  3. $res2=mysqli_query($cn2,"Select @res");
  4.  
  5.  
  6.  $row=mysqli_fetch_assoc($res2);
  7.   $variable=$row['@res'];
  8.   echo $variable;
pero no funcionan....

Muchas gracias.
  #2 (permalink)  
Antiguo 01/05/2012, 05:19
 
Fecha de Ingreso: abril-2012
Ubicación: Trelew
Mensajes: 14
Antigüedad: 12 años
Puntos: 2
Respuesta: Recuperar parametro salida de un sp con mysqli

A sample connection to a MySQL database using the library database described
earlier might look like the following code snippet. Keep in mind that this example
uses the object-oriented interface for mysqli.

Código PHP:
Ver original
  1. $mysqli = new mysqli(’localhost’, ’dbuser’, ’dbpass’, ’library’);
  2.  
  3. echo    ’Connect failed: ’ . mysqli_connect_error();
  4. }
  5.  
  6. // All other database calls go here
  7.  
  8. $mysqli->close();



The same connection using a procedural approach instead of OOP might look
like this. Note that there are not many differences, the main one being the use
of the database resource in the subsequent mysqli_* function calls, such as in
mysqli_close() here.


Código PHP:
Ver original
  1. $dbh = mysqli_connect(’localhost’, ’dbuser’, ’dbpass’, ’library’);
  2.  
  3. if  (!$dbh) {
  4. echo    ’Connect failed: ’ . mysqli_connect_error();
  5. }
  6.  
  7. // All other database calls go here
  8.  
  9.  
  10.  
  11. // Filter input from $_GET
  12. $author = ’’;
  13. if  (ctype_alpha($_GET[’author’]))
  14. {
  15. $author = $_GET[’author’];
  16. }
  17.  
  18. // Escape the value of $author with mysqli->real_escape_string()
  19. $sql = ’SELECT author.*, book.* FROM author
  20. LEFT JOIN book ON author.id = book.author_id
  21. WHERE author.last_name = ’ . $mysqli->real_escape_string($author);
  22.  
  23. // Execute the statement and echo the results
  24.  
  25. if  (!$mysqli->real_query($sql)) {
  26. echo    ’Error in query: ’ . $mysqli->error;
  27. }
  28.  
  29. if  ($result = $mysqli->store_result()) {
  30. while   ($row = $result->fetch_assoc())
  31. {
  32. echo    "{$row[’title’]}, {$row[’last_name’]}\n";
  33. }
  34. $result->close();
  35. }
  36.  
  37. For the procedural style, the code would look like this:
  38.  
  39. // Filter input from $_GET
  40. $author = ’’;
  41. if  (ctype_alpha($_GET[’author’]))
  42. {
  43. $author = $_GET[’author’];
  44. }
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53. Database Programming    ” 165
  54.  
  55.  
  56. // Escape the value of $author with mysqli->real_escape_string()
  57. $sql = ’SELECT author.*, book.* FROM author
  58. LEFT JOIN book ON author.id = book.author_id
  59. WHERE author.last_name = ’ . mysqli_real_escape_string($dbh, $author);
  60.  
  61. // Execute the statement and echo the results
  62. if  (!mysqli_real_query($dbh, $sql)) {
  63. echo    ’Error in query: ’ . mysqli_error();
  64. }
  65.  
  66. if  ($result = mysqli_store_result($dbh)) {
  67. while   ($row = mysqli_fetch_assoc($result))
  68. {
  69. echo    "{$row[’title’]}, {$row[’last_name’]}\n";
  70. }
  71. }
  72.  
  73.  
  74. Prepared Statements and Bound Parameters With mysqli
  75.  
  76. // Filter input from $_GET
  77. $author = ’’;
  78. if  (ctype_alpha($_GET[’author’]))
  79. {
  80. $author = $_GET[’author’];
  81. }
  82.  
  83. // Set a named placeholder in the SQL statement for author
  84. $sql = ’SELECT book.title FROM author
  85. LEFT JOIN book ON author.id = book.author_id
  86. WHERE author.last_name = ?’;
  87. if  ($stmt = $mysqli->prepare($sql)) {
  88.  
  89. $stmt->bind_param(’s’, $author);
  90. $stmt->execute();
  91. $stmt->bind_result($title);
  92.  
  93. while   ($stmt->fetch()) {
  94. echo    "{$title}, {$author}\n";
  95. }
  96.  
  97. $stmt->close();
  98. }
  99.  
  100.  
  101.  
  102. Again, the same code using the procedural approach looks like this:
  103.  
  104. // Filter input from $_GET
  105. $author = ’’;
  106. if  (ctype_alpha($_GET[’author’]))
  107. {
  108. $author = $_GET[’author’];
  109. }
  110.  
  111. // Set a named placeholder in the SQL statement for author
  112. $sql = ’SELECT book.title FROM author
  113. LEFT JOIN book ON author.id = book.author_id
  114. WHERE author.last_name = ?’;
  115.  
  116. if  ($stmt = mysqli_prepare($dbh, $sql)) {
  117.  
  118. mysqli_stmt_bind_param($stmt, ’s’, $author);
  119.  
  120. while   (mysqli_stmt_fetch()) {
  121. echo    "{$title}, {$author}\n";
  122. }
  123.  
  124. }
  125.  
  126.  
  127. Transactions With mysqli
  128.  
  129.  
  130. $mysqli->autocommit(FALSE);
  131.  
  132. $mysqli->query("INSERT INTO book (isbn, title, author_id, publisher_id)
  133. VALUES (’0395974682’, ’The Lord of the Rings’, 1, 3)");
  134. $mysqli->query("INSERT INTO book (title) VALUES (’Animal Farm’, 3, 2)");
  135.  
  136. if  (!$mysqli->commit()) {
  137. $mysqli->rollback();
  138. }
  139.  
  140. The procedural version of the code is very similar:
  141.  
  142. mysqli_autocommit($dbh, FALSE);
  143.  
  144. mysqli_query($dbh, "INSERT INTO book (isbn, title, author_id, publisher_id)
  145. VALUES (’0395974682’, ’The Lord of the Rings’, 1, 3)");
  146. mysqli_query($dbh, "INSERT INTO book (title) VALUES (’Animal Farm’, 3, 2)");
  147.  
  148. if  (!mysqli_commit($dbh)) {
  149. }
  #3 (permalink)  
Antiguo 01/05/2012, 05:59
 
Fecha de Ingreso: abril-2010
Mensajes: 298
Antigüedad: 14 años
Puntos: 1
Respuesta: Recuperar parametro salida de un sp con mysqli

Nose si decir gracias, porque no entiendo nada jajaja, podrias ser algo más concreto???? ahi veo sql preparadas pero no stored procedures... no entiendo
  #4 (permalink)  
Antiguo 01/05/2012, 08:58
 
Fecha de Ingreso: abril-2010
Mensajes: 298
Antigüedad: 14 años
Puntos: 1
Respuesta: Recuperar parametro salida de un sp con mysqli

encontre la solucion, espero que a alguien le sirva:

$ivalue=1;
$res = $cn->multi_query( "call I_Familia(@res,'".$this->quitar($nombre)."');SELECT @res" );
if( $res ) {
$results = 0;
do {
if ($result = $cn->store_result()) {
printf( "<b>Result #%u</b>:<br/>", ++$results );
while( $row = $result->fetch_row() ) {
foreach( $row as $cell ) echo $cell, "&nbsp;";
}
$result->close();
if( $cn->more_results() ) echo "<br/>";
}
} while( $cn->next_result() );
}
$cn->close();

Etiquetas: mysql, mysqli, parametro, salida, sql, variables
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 14:14.