0

I'm new to PHP and MySQL and I'm trying to pull data from individual columns to input into my website. Now, I've seen tons of examples; however, I feel like I must be missing something simple because I can't seem to get it to work. How do I actually pull individual columns and do this repeatedly for everything in that particular table?

Here is my PHP function I'm trying to run:

getPosts();

function getPosts() {

    // Create connection
    $link = new mysqli($servername, $username, $password, $dbname);

    $sql = 'SELECT 
                        title,
                        date,
                        body,
                        author,
                        owner
                    FROM
                        posts';

    $results = $link->query($sql);

    echo "<div class='post'>
        <div class='post-title'>
            <span class='post-heading'>" . $row["title"] .
                "</span><span class='post-date'>" . $row["date"] .
                "</span>
        </div>

        <div class='post-body'>
            <p>" . $row["body"] . "</p>
        </div>

        <div class='post-footer'>
            <hr/>
            <span class='author-note'><span class='glyphicon
                glyphicon-triangle-top'></span> <i>Posted by
               <a href='#'>" . $row["author"] . "</a> | Owned
               by <a href='#'>" . $row["owner"] . "</a></i></span>
        </div>
    </div>";

    $link->close();
}

And then I'm getting these errors when I run this:

Warning: mysqli::mysqli(): (28000/1045):
  Access denied for user 'root'@'localhost' (using password: NO)
  in /home/mynameis/public_html/bardassets/main.php on line 13

Warning: mysqli::query(): Couldn't fetch mysqli in
  /home/mynameis/public_html/bardassets/main.php on line 24

Warning: mysqli::close(): Couldn't fetch mysqli in
  /home/mynameis/public_html/bardassets/main.php on line 41

I guess I just want to know what key part I'm missing here.

2 Answers 2

1

Use it like this as you have not defined $row. Also you have not defined $servername, $username, $password, $dbname variables which usedto create mysqli connection. Please add it. it will work

function getPosts() {

$servername = 'host';
$username = 'username';
$password = 'passwprd';
$dbname = 'dbname';
// Create connection
$link = new mysqli($servername, $username, $password, $dbname);

$sql = 'SELECT 
                    title,
                    date,
                    body,
                    author,
                    owner
                FROM
                    posts';

$results = $link->query($sql);
while($row = $results->fetch_assoc()) {
    echo "<div class='post'>
        <div class='post-title'>
            <span class='post-heading'>" . $row["title"] .
                "</span><span class='post-date'>" . $row["date"] .
                "</span>
        </div>

        <div class='post-body'>
            <p>" . $row["body"] . "</p>
        </div>

        <div class='post-footer'>
            <hr/>
            <span class='author-note'><span class='glyphicon
                glyphicon-triangle-top'></span> <i>Posted by
               <a href='#'>" . $row["author"] . "</a> | Owned
               by <a href='#'>" . $row["owner"] . "</a></i></span>
        </div>
    </div>";
}

$link->close();
  }
Sign up to request clarification or add additional context in comments.

4 Comments

I'm getting two more errors as a result: Warning: mysqli::query()::: Couldn't fetch mysqli in /home/mynameis/public_html/bardassets/main.php on line 24 &&&& Fatal error: Call to a member function fetch_assoc() on a non-object in /home/mynameis/public_html/bardassets/main.php on line 26
please add you servername, username, password ,dbname in the variables I have defined. the error is caused becaused mysqli connection has not defined due to empty undefined variables inside new mysqli() instanciation
Ah, I had my variables declared outside of the function and that was screwing it up. What's the php equivalent to static variables?
create connection outside function and use that $link as global inside your function
0

It simply says one or more of your $servername, $username, $password, $dbname is/are wrong.

Warning: mysqli::mysqli(): (28000/1045):
  Access denied for user 'root'@'localhost' (using password: NO)
  in /home/mynameis/public_html/bardassets/main.php on line 13

1 Comment

How can that be? I've checked the connection prior and it said it connected successfully. I'm very confused as to how that error is occurring.

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.