PHP with PDO connection?

chevezupr

New Member
Licensed User
Longtime User
Hello Erel, hello guys, I would like to know is it possible to changed the Erel's PHP code to connect to Mysql into PHP Code with PDO connection?

Thanks in advance for your help and have a great day..


Ricardo

-----------------------------------------------------
The following PHP code belongs to EREL to connect to mysql

<?

$databasehost = "localhost";
$databasename = "xxxx";
$databaseusername ="xxxx";
$databasepassword = "xxxx";

$con = mysql_connect($databasehost,$databaseusername,$databasepassword) or die(mysql_error());
mysql_select_db($databasename) or die(mysql_error());
$query = file_get_contents("php://input");
$sth = mysql_query($query);

if (mysql_errno()) {
header("HTTP/1.1 500 Internal Server Error");
echo $query.'\n';
echo mysql_error();
}
else
{
$rows = array();
while($r = mysql_fetch_assoc($sth)) {
$rows[] = $r;
}
print json_encode($rows);
}
?>
 

edwar8405

Member
Licensed User
Longtime User
Aquí dejo mi pequeño aporte de como conectar con PDO y poder tener resultados para aplicaciones tras varias pruebas. Espero te sirva y a muchas también ya que veo que no hay mucha información sobre este tema.

Here is my small contribution on connecting with PDO and applications to have results after several tests. I hope you serve and many also because I see that there is not much information on this subject.



<?php
$pdo=new PDO('mysql:host=localhost;dbname=prueba', $usuario, $contraseña);
$query = file_get_contents("php://input");
$statement=$pdo->prepare($query);
$statement->execute();
$results=$statement->fetchAll(PDO::FETCH_ASSOC);
print json_encode($results);
?>
 
Upvote 0
Top