2

I made a function to fetch a column and them render each row as a li. But I am getting "Errormessage:" as result. I tried it without functon and that works fine.

Please help.

$nkFetch = function($link){

    $table1Query = "SELECT * FROM table1";

    $res = mysqli_query($link, $table1Query);

    if (!mysqli_query($link, $table1Query)) {
        printf("Errormessage: %s\n", mysqli_error($link));
    }

    $table1 = array();

    while ($row = mysqli_fetch_array($res)){
        $table1[] = $row;
    }  

    shuffle($table1);

    foreach( $table1 as $row ){
        echo "<li>" . $row['WORD'] . "</li>";
    }

}
12
  • 4
    1. Please define "not working". 2. Please don't use mysql_*. Commented Feb 10, 2014 at 12:50
  • Problem is stated in the title. But I'll edit the post. What do you mean don't use mysql_? Commented Feb 10, 2014 at 12:52
  • @RaymondtheDeveloper: He means don't use mysql_* at all, not for new code, and refactor the old code ASAP: read the red box on the doc pages Commented Feb 10, 2014 at 12:53
  • Dear @RaymondtheDeveloper mysql_* is deprecated that's why @Ofir suggest you to don't use it. Commented Feb 10, 2014 at 12:54
  • 1
    @RaymondtheDeveloper: You say this is a function... is $con an actual connection that is passed as an argument, or is it just not declared in the function's scope. If the latter: that's your problem. PS: functions shouldn't echo, they should return Commented Feb 10, 2014 at 12:55

1 Answer 1

3

Update
Given that you're using a lambda-style function (instance of Closure), and wish to use a globally available variable $link, you should write:

$nkFetch = function () use ($link)
{
    //code here. $link is available
};
$nkFetch();
//or
$nkFetch = function ($link)
{
    //code here
};
$nkFetch($link);//<-- pass mysqli here

Read the manual on anonymous functions

Very well, I'll bite.

  1. You say this is a function. If so: are you passing the $con connection variable as an argument
  2. Have you made sure the connection (mysql_connect call) was successful
  3. As an asside: mysql_free_result really is a tad pointless if that's the last statement in your function. The resultset should be freed once all function variables go out of scope

Now, here's what your code should look like if you want to use that deprecated extension (BTW: try updating your PHP version, and set your error level to E_STRICT | E_ALL, you should see deprecated notices).
Assuming this is indeed a bona-fide function:

function getList($con)
{
    if (!is_resource($con))
    {//check if $con is a resource, if not... quit
        echo '$con is not a resource!', PHP_EOL;
        return;
    }
    $res = mysql_query('SELECT * FROM table1', $con);
    if (!$res)
    {//query failed
        echo 'Query failed: ', mysql_error($con);
        return;
    }
    while($row = mysql_fetch_assoc($res))
    {//echo here already
        echo '<li>', $row['WORD'], '</li>';
    }
}
//call like so:
$dbConnection = mysql_connect('127.0.0.1', 'user', 'pass');//pass your params here, of course
if (!$dbConnection) exit(mysql_error());
getList($dbConnection);

A more modern way of writing this would be:

$db = new PDO('127.0.0.1', 'user', 'pass');
$res = $db->query('SELECT * FROM table1');
while($row = $res->fetch(PDO::FETCH_ASSOC))
{
    echo '<li>', $row['WORD'], '</li>';
}

Check the PDO documentation

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

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.