Array with database data

Asked

Viewed 45 times

-3

I need to mount an array by pulling the Slug from my table, example:

$array = array (
  'esportes',
  'mundo',
  'estado'
);

I tried so:

$slug = $con->prepare("SELECT slug FROM categorias");
$slug->execute();
$array = array (
   foreach ( $slug as $row ) {
      echo "'.$row['slug'].',";
   }
);

I need to use this array to make a friendly url that looks like this:

$url = $_GET['url'];
$sep = array_filter ( explode ( "/", $url ) );

Here I need the array:

if ( isset ( $sep[0] ) && $sep[0] in_array ( $sep[0], $array ) ) {
   $Categoria = new Categoria;
   $Categoria->GetCategoria;
}

Does anyone know any solution?

  • If you use PDO: https://www.php.net/manual/en/pdostatement.fetchall.php; otherwise, https://www.php.net/manual/en/mysqli-stmt.fetch.php

  • @Valdeir Par using the first option I can do that 'if'?

  • His last if..isset? It depends on your data structure and data return.

  • @Valdeir Par the return would be the array

1 answer

-1

In your consultation try like this:

$slug = $con->prepare("SELECT slug FROM categorias"); $slug->execute(); $array = array (); foreach ( $slug->fetchAll(\PDO::FETCH_ASSOC) as $row ) { $array[] = $row['slug']; }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.