2

I have 24 pieces of data on an SQL server. When my webpage starts it pulls all this data one at a time from the server and displays it. Looks like this:

$sql = "SELECT `$dateName` FROM `$user` WHERE hour=2";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
     while ($row = $result->fetch_assoc()) {
         $hour2 = $row[$dateName];
     }
}
$sql = "SELECT `$dateName` FROM `$user` WHERE hour=3";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
     while($row = $result->fetch_assoc()) {
         $hour3 = $row[$dateName];
     }
}

etc... until hour 24.

But I think it is better with a for loop. Like this:

for ($x = 1; $x <= 24; $x++) {

    $sql = "SELECT `$dateName` FROM `$user` WHERE hour=`$x`";
    $result = $conn->query($sql);

    if ($result->num_rows > 0) {
         while($row = $result->fetch_assoc()) {
             $hour.$x = $row[$dateName];
         }
    }                          
}

I want to call $hour1, $hour2, $hour3. By adding $x to $hour. How do I do this? Thank you so much!

5 Answers 5

3

Instead of generating dynamic variables, I would suggest to create array of $hours.

$hour = array();
for ($x = 1; $x <= 24; $x++) {

    $sql = "SELECT `$dateName` FROM `$user` WHERE hour=`$x`";
    $result = $conn->query($sql);


    if ($result->num_rows > 0) {
         while($row = $result->fetch_assoc()) {
             $hour[$x][] = $row[$dateName];
         }
    }
 }

So that you can access like $hour[1],$hour[2]...etc...

Sign up to request clarification or add additional context in comments.

1 Comment

Great answer! Thank you!
2

Wrap them into ${'hour'.$x} = $row[$dateName]

2 Comments

Not sure if this is the best solution or not (maybe the array is more efficient) but this worked for my problem, very simple integration and worked like a charm! Thank you so much.
Yes, are you right, this is not the best solution. The best solution is to create an array with the hours $hour[1], etc.. Like @Jayesh Chitroda posts. This is just answer for your question, nice to know it :)
1

You can do this with Variable variables:

${"hour" . $x} = $row[$dateName];

Comments

1

You can create $hour as an array, use $x as the keys and setting the appropriate values to the keys.

EG:

$hours = array();

for ($x = 1; $x <= 24; $x++) {

    $sql = "SELECT `$dateName` FROM `$user` WHERE hour=`$x`";
    $result = $conn->query($sql);

    if ($result->num_rows > 0) {
         while($row = $result->fetch_assoc()) {
             $hour[$x] = $row[$dateName];
         }
    }
}

echo $hour[1];

Note:

You should consider using a more efficient way, where you do not have to connect to the database 24 separate times for each hour.

EG:

$sql = "SELECT `$dateName` FROM `$user` ORDER BY hour ASC";

This will return all 24 rows in one query, then you can manipulate the data with PHP.

Comments

1

function pullRequest($dateName,$user,$x){
    //Define $conn here
    $sql = "SELECT `$dateName` FROM `$user` WHERE hour=`$x`";
    $result = $conn->query($sql);
    $hour = array();
    if ($result->num_rows > 0) {
         while($row = $result->fetch_assoc()) {
             $hour[$x] = $row[$dateName];
         }
    }
    return $hour[$x];

} 

Then you can call it like this
$dateName ="DATENAME";
$user="USER";
 for ($x = 1; $x <= 24; $x++) {
    pullRequest($dateName,$user,$x);
 }

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.