Joppieb
Joppieb

Reputation: 39

SQL array var_dump shows nothing

I have some code for load business-hours. My databas looks like

id - int(11)
day - varchar(255)
starttime - (time)
endtime - (time)
date - (date)
type - int(1)

I try to get the data with:

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
echo "Connected successfully";

function get_all_records($sql){
    global $conn;

    $result = $conn->query($sql);
    $result = [];
    while($row = $result->fetch_assoc()) $result[array_shift($row)] = $row;
    return $result;
}

$sql = "SELECT day, starttime, endtime FROM schedule WHERE type =  ";

$shop_hours = array_merge(
    get_all_records($sql . "0 ORDER BY id"),
    get_all_records($sql . "1 and YEARWEEK('DATE') = '201915'")
);


var_dump ($shop_hours);
?>

Only what is displayed is Connected successfully

I can't see the issue :(

Upvotes: 0

Views: 76

Answers (2)

Joppieb
Joppieb

Reputation: 39

I found the issue on line 14

while($row = $query->fetch_assoc()) $result[array_shift($row)] = $row;

    <?php    
$conn = new mysqli($dbhost, $dbusername, $dbpassword, $dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
echo "Connected successfully";

function get_all_records($sql){
  global $conn;

  $query = $conn->query($sql);
  $result = [];
  while($row = $query->fetch_assoc()) $result[array_shift($row)] = $row;
  return $result;
}

$sql = "SELECT day, starttime, endtime FROM schedule WHERE type = ";
$shop_hours = array_merge(
  get_all_records($sql . "0 ORDER BY id"),
  get_all_records($sql . "1 and YEARWEEK('DATE') = '201915'")
);

var_dump($shop_hours);
?>

Upvotes: 0

Steve T
Steve T

Reputation: 572

You are overwriting the result variable with an empty array. You should change the name of this array:

$result = [];

Upvotes: 1

Related Questions