-2

I have a perfectly working small database, there is a table exactly named "Category" which in it´s turn has a row named exactly "Name". I've tried every version (Capitalized and not) of the names but my function doesn't return what I want it to return. Anyone out there spotting an obvious error? The database connects just fine... Or am I looking at something seriously troublesome db-conflict.

This is my functions.php which I've included into index.php and had a function call at body. "<?php display_menus(); ?>

<?php 

//Connect to database

$link = mysqli_connect('localhost', 'root', 'root');
if (!$link)
{
    $output = 'Unable to connect to the database server.';
    echo $output;
    exit();
}

if (!mysqli_set_charset($link, 'utf8'))
{
    $output = 'Unable to set database connection encoding.';
    echo $output;
    exit();
}

if (!mysqli_select_db($link, 'Asperod6'))
{
    $output = 'Unable to locate the "Asperod6" database.';
    echo $output;
    exit();
}

    $output = 'Database connection established.';
    echo $output;

//Funktion som skriver ut meny


function display_menus()
{
    $result = mysqli_query($link, "SELECT * FROM Category");

    if (!$result)
        {
            $error = 'Error fetching Kategorier: ' . mysqli_error($link);
            echo ("There is none");
        }



        if (mysqli_num_rows($result) > 0) 
        {
            echo "<ul>";

            while ($row = mysqli_fetch_array($result)) 
            {
                echo "<li>" . $row['Name'] . "</li>";
            }

            echo "</ul>";

            mysqli_free_result($result);

        }

} 





mysqli_close($link);


?>
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Michael Scott
  • 99
  • 1
  • 1
  • 5
  • try adding the db name before the table name `SELECT * FROM DBNAME.Category` - and shows us the db connection function, and the db schemata –  May 06 '13 at 03:49
  • what is in $link ? and do what @Dagon suggested – swapnesh May 06 '13 at 03:50
  • ... if $link is defined out side the function, it needs to be made global or parsed –  May 06 '13 at 03:51
  • 2
    Is `$link` in global scope ? – The Alpha May 06 '13 at 03:51
  • oops I dunno where to post further code – Michael Scott May 06 '13 at 03:55
  • You can edit your question. – j883376 May 06 '13 at 03:55
  • *above* function it may be, but each function has its own scope –  May 06 '13 at 03:56
  • so the connection is out of scope, im retarded. how do u suggest implementing the link thing... and thanks for spotting it so quick :D – Michael Scott May 06 '13 at 04:03
  • call with `display_menus($link)` change the function to `display_menus($link){..}` –  May 06 '13 at 04:05
  • the thing is I was following this tutorial on youtube "http://www.youtube.com/watch?v=CKk5iUYtrgQ" and he did it exactly this way and got it to work, with the scope issue and all. so I thought, but I should´nt had. – Michael Scott May 06 '13 at 04:07
  • Thanks Dagon! very much... btw how do I give thumbs up to people who answer below my post? – Michael Scott May 06 '13 at 04:08
  • When I some day get rich out of this patent pending issue regarding super easy mysql menus, you will ALL get a piece of the cake. – Michael Scott May 06 '13 at 04:11
  • multiple bad practices in that video, so i wouldn't recommend it. –  May 06 '13 at 04:12
  • But i´m like we get these assignments in school. I love to spend time coding, but really. Where do I find good practises? I have some books from school, but I can´t seem to find stuff about making a good, easy mysql/php menu. Like I really need basic stuff, made good to learn from but I don´t find it and if I do, maybe it´s a little bit too complicated or advanced. Do you pro´s have a special place that you went before u became pros? I can admit, should of figured out the scope thing, since it´s pretty basic, but the video got me blind and it´s late :( – Michael Scott May 06 '13 at 04:18
  • pro=amateur+lots of practice. no magic involved :-) –  May 06 '13 at 04:28

2 Answers2

2

Because for some reason you are not displaying the actual error message to yourself, nor have php error reporting turned on. While if you let PHP to tell you errors, you'd be told that $link is undefined.

  1. display an actual error

    $result = mysqli_query($link, "SELECT * FROM Category"); 
    if (!$result) {
       trigger_error(mysqli_error($link));
    } 
    
  2. Make PHP report errors

    ini_set('display_errors',1);
    error_reporting(E_ALL);
    
  3. Finally, after having all there errors reported to you, make $link global inside function

    function display_menus()
    {
        global $link;
        ...
    
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
-1

Try placing a die() statement along with your connection statement and get the actual error. How are you certain it is connecting to the database?

Update -

Looking at the update now, the problem might be that the scope of that variable.

Add

global $link 

in your function. Adding the die() statement in the mysqli_query() statement will give you the exact error.

A23
  • 1,596
  • 2
  • 15
  • 31