1

Basically as it says in the question i am trying to take data from my database and have each row in the database display in a new row in a HTML table. I thought i was on the right track but when viewing my code in PhpStorm it throws up an error saying required parameter $query missing. I'm not sure where this parameter is meant to be but the error is showing up on the query line: $result = mysqli_query(....

<table cellpadding="0" cellspacing="0" width="100%" class="sortable">

                        <thead>
                            <tr>
                                <th>Project title</th>
                                <th>Start Date</th>
                                <th>Acc Manager</th>
                                <th>Designer</th>
                                <th>Stage</th>
                                <td>&nbsp;</td>
                            </tr>
                        </thead>

                        <tbody>
<?php
      function list_projects() {

          global $connection;

      $output = "";
      $result = mysqli_query("SELECT * FROM projects ORDER BY project_title ASC");
      while ($row = mysqli_fetch_array($result)){
      $output .= '
      <tr>
      <td>' . $row['project_title'] . '</td>
      <td>' . $row['start_date'] . '</td>                                                   
      <td>' . $row['acc_manager'] . '</td>
      <td>' . $row['designer'] . '</td>
      <td>' . $row['stage'] . '</td>                                    
      </tr>';
      }

      return $output;

     }
?>
</tbody>
</table>
3
  • you are actually on the right track you just have a typo in your code msqli_fetch_array( should be mysqli_fetch_array( , you're missing a y and mysqli_query first parameter should be $link, so your query should be like this mysqli_query($connection, "SELECT * FROM projects ORDER BY project_title ASC"); Commented Feb 24, 2014 at 21:21
  • Look at the parameters: us2.php.net/mysqli_fetch_array Commented Feb 24, 2014 at 21:21
  • As a sidenote, it would also be good to return an array of rows, not a string. This way you dont couple you data access with view logic. Commented Feb 24, 2014 at 21:23

3 Answers 3

1

As stated in the docs. mysqli_query takes 2 parameters when used in a procedural style. I'm assuming $connection is your mysqli link Try:

$result = mysqli_query($connection, "SELECT * FROM projects ORDER BY project_title ASC");
Sign up to request clarification or add additional context in comments.

5 Comments

As above thanks, yes this sorted out the error, however despite having no errors none of my data is being callled and displayed on the page
Make sure the query returns something. Also, I don't see any markup for the actual table. Just the table rows and columns. Where is the rest of your HTML?
I've added in the rest of the html for the table. I had dummy content in and it was all displaying fine obviously so i know its not my HTML markup its obviously my PHP
I don't see you ever calling the function. Either remove the code from the function or put the function somewhere else, call it and store it in a variable and echo the variable where your function is now.
Thank you, i moved the function to my functions.php file and did as you said echoing the stored variable and this has worked! Really appreciate your help!
1

You need to pass the $connection into you mysqli_query() function.

https://www.php.net/mysqli_query

$result = mysqli_query($connection, $query);

3 Comments

Thanks that actually got rid of the error! Even so though none of the data is being displayed when i load the page :( Don't know what i'm doing wrong!
first thing to check, make sure your query actually returns data (stupid error I've run into many times). run your query in PHPmyAdmin directly on your db.
try calling $result->num_rows. should return a positive int if you have results in your query
0

Do you run the function?

echo list_projects();

(I know, silly question, but I dont see that you are doing it?)

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.