PHP - De PDO a mysqli

 
Vista:

De PDO a mysqli

Publicado por Pedro (8 intervenciones) el 26/12/2017 21:04:54
Hola por fin he conseguido que me vaya file input por finnnn la cuestión es que utiliza una conexion de pdo y me gustaría utilizar una conexión my_sqli. ¿Cómo lo cambiaría?

Los ficheros son:

index.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8"/>
    <title>Bootstrap File Upload</title>
 
	<link href="http://netdna.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" rel="stylesheet">
    <link href="css/fileinput.min.css" media="all" rel="stylesheet" type="text/css" />
 
	<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <script src="js/fileinput.min.js" type="text/javascript"></script>
    <script src="http://netdna.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js" type="text/javascript"></script>
 
	</head>
 
	<body>
 
	<input id="archivos" name="imagenes[]" type="file" multiple=true class="file-loading">
	</body>
	<?php
	$directory = "imagenes_/";
	$images = glob($directory . "*.*");
	?>
 
	<script>
	$("#archivos").fileinput({
	uploadUrl: "upload.php",
    uploadAsync: false,
    minFileCount: 1,
    maxFileCount: 20,
	showUpload: false,
	showRemove: false,
	initialPreview: [
	<?php
 
	$pdo=new PDO("mysql:dbname=basededatos;host=127.0.0.1","root","123");
	$statement=$pdo->prepare("SELECT id,info FROM archivos");
	$statement->execute();
	$results=$statement->fetchAll(PDO::FETCH_ASSOC);
 
 
	foreach($results as $result){?>
		"<img src='view.php?elid=<?php echo $result['id']; ?>' height='120px' class='file-preview-image'>",
	<?php } ?>],
    initialPreviewConfig: [<?php foreach($results as $result){ $infoImagenes=$result['info'];?>
	{caption: "<?php echo $infoImagenes;?>",  height: "120px", url: "borrar.php", key:"<?php echo $result['id'];?>"},
	<?php } ?>]
	}).on("filebatchselected", function(event, files) {
 
	$("#archivos").fileinput("upload");
 
	});
 
	</script>
</html>

upload.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?php
$carpetaAdjunta="imagenes_/";
// Contar envían por el plugin
$Imagenes = count($_FILES['imagenes']['name']);
 
for($i = 0; $i < $Imagenes; $i++) {
 
	// El nombre y nombre temporal del archivo que vamos para adjuntar
	$nombreArchivo=$_FILES['imagenes']['name'][$i];
	$nombreTemporal=$_FILES['imagenes']['tmp_name'][$i];
 
	$rutaArchivo=$carpetaAdjunta.$nombreArchivo;
 
 
	$pdo=new PDO("mysql:dbname=basededatos;host=127.0.0.1","root","123");
	$statement = $pdo->prepare("INSERT INTO  archivos(id,info,imagen) VALUES(NULL,:info,:texto);");
	$statement->execute(array("info" => $_FILES['imagenes']['name'][$i],"texto" => file_get_contents($_FILES['imagenes']['tmp_name'][$i])));
 
 
 
	move_uploaded_file($nombreTemporal,$rutaArchivo);
 
	$infoImagenesSubidas[$i]=array("caption"=>"$nombreArchivo","height"=>"120px","url"=>"borrar.php");
	$ImagenesSubidas[$i]="<img  height='120px'  src='$rutaArchivo' class='file-preview-image'>";
 
	}
 
$arr = array("file_id"=>0,"overwriteInitial"=>true,"initialPreviewConfig"=>$infoImagenesSubidas,
	 "initialPreview"=>$ImagenesSubidas);
echo json_encode($arr);print_r($ID);
?>


borrar.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?php
$carpetaAdjunta="imagenes_/";
 
if($_SERVER['REQUEST_METHOD']=="DELETE"){
 
	parse_str(file_get_contents("php://input"),$datosDELETE);
 
	$key= $datosDELETE['key'];
 
	$pdo=new PDO("mysql:dbname=basededatos;host=127.0.0.1","root","123");
	$statement = $pdo->prepare("DELETE FROM archivos  WHERE id=:id");
	$statement->execute(array("id" => $key));
 
 
	//unlink($carpetaAdjunta.$key);
 
	echo 0;
}
 
?>


View,php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php
 
header('Content-Type: image/jpg');
 
if(isset($_GET['elid'])){
 
	$pdo=new PDO("mysql:dbname=basededatos;host=127.0.0.1","root","123");
 
	$statement=$pdo->prepare("SELECT * FROM archivos WHERE id=:id");
 
	$statement->execute(array("id" => $_GET['elid']));
 
	$results=$statement->fetchAll(PDO::FETCH_ASSOC);
 
	print_r($results[0]['imagen']);
}
 
?>


La otra pregunta es: El archivo upload me guarda el archivo en blob y yo lo que quiero es que me lo guarde en la ruta $rutaArchivo=$carpetaAdjunta.$nombreArchivo; Paara no ocupar espacio en la bdd.

A ver si me podéis ayudar.
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
sin imagen de perfil
Val: 52
Ha aumentado su posición en 4 puestos en PHP (en relación al último mes)
Gráfica de PHP

De PDO a mysqli

Publicado por David (27 intervenciones) el 03/01/2018 06:10:28
Hola! He estado echando un vistazo al código y no me queda muy claro todo pero...

Si quieres utilizar Mysqli simplmente:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
//Orientado a objetos
$mysqli=new mysqli("127.0.0.1", "root", "123", "basededatos");
$query=$mysqli->query("SELECT id, info FROM archivos");
$row=$query->fetch_assoc();
 
//estilo por procedimientos.
 
$mysqli=mysqli_connect("127.0.0.1", "root", "123", "basededatos");
$query="SELECT id, info FROM archivos";
$result=mysqli_query($mysqli, $query);
$row=mysqli_fetch_assoc($result);
 
 
?>
Estaba mirando también el archivo que decías de upload...
Creo que debería ser:

1
move_uploaded_file($carpetaAdjunta. $nombreArchivo);

Espero haberte ayudado en algo :)
Un saludo.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
sin imagen de perfil
Val: 729
Bronce
Ha aumentado 1 puesto en PHP (en relación al último mes)
Gráfica de PHP

De PDO a mysqli

Publicado por gonzalo (615 intervenciones) el 04/01/2018 02:04:05
Hola Pedro.

pdo es mas reciente que mysqli, a menos que utilices una version de php anterior a la actual.

porque quieres usar mysqli?
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar