0

I have the following code:

$result = mysqli_query($con, "SELECT * FROM da_questions");

while($row = mysqli_fetch_array($result))
{

}

mysqli_close($con);

which selects everything from the table da_questions. Now this is in a file called config.php which file is required in all my pages of my website. Now I want to do the following on my external pages... let's say index.php

<?php foreach($results as $question): ?>

   <p><?php echo $question['question_title']; ?></p>
   <br />
   <p><?php echo $question['question_text']; ?></p>
   <br />

<?php endforeach; ?>

My file outlay is like shown below:

index.php

config/
  config.php

How can I do this?

4
  • 1
    not sure I understand. why not just $results = array(); while($row = mysqli_fetch_array($reult)) { $results[] = $row; } Commented Jan 10, 2014 at 5:24
  • 2
    @FrankConry Or even easier $results = $result->fetch_all(MYSQLI_ASSOC) Commented Jan 10, 2014 at 5:26
  • @FrankConry post in answer? So I can abide by stackoverflow policies... Commented Jan 10, 2014 at 5:27
  • how did you you make $results on index.php? Commented Jan 10, 2014 at 5:29

2 Answers 2

1

Why not just

$results = array(); while($row = mysqli_fetch_array($reult)) { $results[] = $row; }

or as @Phil pointed out:

$results = $result->fetch_all(MYSQLI_ASSOC)
Sign up to request clarification or add additional context in comments.

Comments

0

With your code

   $result = mysqli_query($con, "SELECT * FROM da_questions");
   $results = array();
   while($row = mysqli_fetch_array($result))
   {
    $results[] = $row;
   }

   mysqli_close($con);

in the index.php write as below

  include('config/config.php');

 <?php foreach($results as $question): ?>

    <p><?php echo $question['question_title']; ?></p>
    <br />
    <p><?php echo $question['question_text']; ?></p>
   <br />

   <?php endforeach; ?>

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.