0

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 :(

1
  • I see no error handling or debugging attempted here Commented Apr 13, 2019 at 16:40

2 Answers 2

1

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

$result = [];
Sign up to request clarification or add additional context in comments.

Comments

0

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);
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.