0

I am trying this function it is possible please help me.

My code sample:

# Here is my select query
$con = mysqli_connect('localhost', 'root', '', 'test_database');
$select = "SELECT * FROM test_posts";
$select_query = mysqli_query($con, $select);

# Here is my function query
function getPosts() {
    global $con;
    global $select;
    global $select_query;
    $post = mysqli_fetch_array($select_query);
}

# Here is my homepage get posts method

while (getPosts()) {

 # I am triying this method and tried define, and echo but not working

 echo $post['sample_content'];

}

I am tried define('sample', '$post = mysqli_fetch_array($sample)'); but not worked.

Where is my mistake? How I should do this?

9
  • do you specifically need this getPosts() function? you can do an easy workaround using: while($post = mysqli_fetch_array($select_query) { .... } Commented Jul 11, 2018 at 19:43
  • yes I need, I now that method, I am triying new method :) Commented Jul 11, 2018 at 19:46
  • @IncredibleHat How I fix that? Commented Jul 11, 2018 at 19:46
  • 1
    $post only exists within the function. Return it, then define it as $post = getPosts() Commented Jul 11, 2018 at 19:46
  • You should read up a bit on scope. Commented Jul 11, 2018 at 19:47

3 Answers 3

2

I run your code but I change some line :

this is my test_posts table :

| sample_content | id |
|:--------------:|:--:|
|      test1     |  1 |
|      test2     |  2 |

and I add return $post; in getPosts function For sending records to while.

also used ‍‍$post=getPosts() instead of getPosts() to print the values insidewhile.

return:

test1 test2 finish

my code :

<?php

# Here is my select query
$con = mysqli_connect('localhost', 'phpmyadmin', '123456', 'test');
if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$select = "SELECT * FROM test_posts";
$select_query = mysqli_query($con, $select);

# Here is my function query
function getPosts() {
    global $con;
    global $select;
    global $select_query;
    $post = mysqli_fetch_array($select_query);
    return $post;
}

# Here is my homepage get posts method

$post=[];
while ($post=getPosts()) {

    # I am triying this method and tried define, and echo but not working

    echo $post['sample_content'];

}

echo 'finish';
Sign up to request clarification or add additional context in comments.

13 Comments

"Try this" does not make for a good answer. You should explain how and why this solves their problem. I recommend reading, "How do I write a good answer?"
i agree with @JohnConde and could you also elaborate on the $post=[] ?
Thanks but this is giving nothing. :)
Sorry. I'm explaining it now @JohnConde
@RamazanAlkan test again please
|
1

You can do this by making getPosts() a generator function.

function getPosts() {
    global $select_query;
    while ($post = mysqli_fetch_array($select_query)) {
        yield $post;
    }
}

Then you can use it in a foreach loop:

foreach (getPosts() as $post) {
    echo $post['sample_content'];
}

It would be better to make $select_query a parameter of the function rather than using a global variable.

Comments

0

I'm not sure if you're asking to make this work using this specific getPosts() function or you need this to work anyhow. If so you can do it as such:

# Here is my select query
$con = mysqli_connect('localhost', 'root', '', 'test_database');
$select = "SELECT * FROM test_posts";
$select_query = mysqli_query($con, $select);


while ($post = mysqli_fetch_array($select_query)) {


 echo $post['sample_content'];

}

4 Comments

I now this but I need function method I am triying new method
i agree with @IncredibleHat ... WHY :D
I don't now :) really I am trying this like 5 hours I want it!
"wordpress" ... ok, I'm out.

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.