-2

I would like to perform actions when there is no key in the table "home" or "reload"

now I have something like this but it does not work

$pages = array();
$sql = "SELECT page_name FROM pages";
$result = $con->query($sql);
while($row = $result->fetch_assoc()) {
$pages[] = $row;    
}   
$page = $_GET['page'];
if( !in_array( $page ,$pages ) )
{
    $_GET['page'] = 'home';
}

$pages[] result:

    Array ( 
[0] => Array ( [page_name] => Home ) 
[1] => Array ( [page_name] => reload) 
Patryk LL
  • 11
  • 2

2 Answers2

2

You're calling "key" where is "value". Just to be clear and avoid mistakes. You're loading MySQL results as array to an array. To your code works, just change:

$result = $con->query ( "SELECT `page_name` FROM `pages`");
$pages = array();
while ( $row = $result->fetch_assoc ())
{
  $pages[] = $row["page_name"];
}
if ( ! in_array ( $_GET["page"], $pages))
{
  $_GET["page"] = "home";
}
Ernani Azevedo
  • 461
  • 2
  • 6
0

You should use isset(). See why here : what is faster: in_array or isset?.

If you want to use 2d array see this : http://sandbox.onlinephpfunctions.com/code/f12fbc6a922fc8583b83534642ef652a4a123010

I prefer Ernani Azevedo answer

Keep coding

André DS
  • 1,823
  • 1
  • 14
  • 24
Ivan Ganchev
  • 174
  • 2
  • 10