Adjust variable text by editing it from the database

0

I have this code:
To edit the path where the image is saved from the database:

<?php
  $host = 'localhost'; 
  $user = 'root'; 
  $pass = ''; 
  $db = 'img'; 
  //creo mi conexion a la base de datos 
  $mysqli = new mysqli($host, $user, $pass, $db); 

//capturamos los datos del fichero subido    
$type=$_FILES['img_up']['type'];
$tmp_name = $_FILES['img_up']["tmp_name"];
$name = $_FILES['img_up']["name"];
$consulta = $mysqli -> query("SELECT * FROM datos") or die($mysqli->error);

//Creamos una nueva ruta (nuevo path)
//Así guardaremos nuestra imagen en la carpeta "images"
$nuevo_path = $consulta.$name;
//Movemos el archivo desde su ubicación temporal hacia la nueva ruta
# $tmp_name: la ruta temporal del fichero
# $nuevo_path: la nueva ruta que creamos
move_uploaded_file($tmp_name,$nuevo_path);
//Extraer la extensión del archivo. P.e: jpg
# Con explode() segmentamos la cadena de acuerdo al separador que definamos. En este caso punto (.)
$array=explode('.',$nuevo_path);
# Capturamos el último elemento del array anterior que vendría a ser la extensión
$ext= end($array);
//Imprimimos un texto de subida exitosa.
echo "<h3>La imagen se subio correctamente</h3>";
// Los posible valores que puedes obtener de la imagen son:
echo "<b>Info de la imagen subida:</b>";
echo "<br> Nombre: ".$_FILES['img_up']["name"];      //nombre del archivo
echo "<br> Tipo: ".$_FILES['img_up']["type"];      //tipo
echo "<br> Nombre Temporal: ".$_FILES['img_up']["tmp_name"];  //nombre del archivo de la imagen temporal
echo "<br> Tamanio: ".$_FILES['img_up']["size"]." bytes";      //tamaño
?>

But in the line of: $nuevo_path = $consulta.$name;

I get an error:

  

Recoverable fatal error: Object of class mysqli_result could not be converted to string in C: \ xampp \ htdocs \ upload \ upload.php on line 17

Supposedly the $consulta is where the route I want to edit from the base is.

    
asked by MatiPHP 07.08.2018 в 00:18
source

2 answers

2

This happens because the result of the query:

  

SELECT * FROM data

It does not return a string that can be concatenated with $ query.

Try with:

$consulta = $mysqli->query("SELECT * FROM datos") or die($mysqli->error);
$resultado = $consulta->fetch_row();
$nuevo_path = $resultado['elatributodelselect*'].$name;

The above works, only if you know with certainty that the result you need is in the first result of the query. Otherwise you should make a more restrictive query and iterate. Greetings.

    
answered by 07.08.2018 в 00:33
0

You have to keep in mind that mysqli-> query will return an object of type mysqli_result that,

  

Represents the set of results obtained from a query in the database.

To obtain a data of that set of results you can use different methods listed here , depending on your case.

For example you can use fetch_all when you have several rows as a result returned:

$name = $_FILES['img_up']["name"]; 

$consulta = $mysqli ->query("SELECT * FROM datos") or die($mysqli->error);

$filas = $consulta->fetch_all($consulta);

// asumiendo que lo que deseas está en tu primera fila
$nuevo_path = $filas[0]->ruta.$name;

In the case of only one row you can use fetch_array :

$name = $_FILES['img_up']["name"]; 

$consulta = $mysqli ->query("SELECT * FROM datos") or die($mysqli->error);

$fila = $consulta->fetch_array($consulta);

$nuevo_path = $fila['ruta'].$name;
    
answered by 07.08.2018 в 00:56