0

Hello all,
im having some problem, i have a function call dbconn which i run after my session. i can successfully connect to the database and run the mysql statement outside of the function with no problems. However, when i put it in the function and return it, nothing happens. Example:

index.php

<?php
include_once 'includes/db_connect.php';

sec_session_start();
dbconn();
echo $rows["username"];
?>

db_connect.php

<?php
function dbconn($autoclean = false) {
    if (!@mysql_connect(HOST, USER, PASSWORD)) {
        switch (mysql_errno()) {
            case 1040:
            case 2002:
            if ($_SERVER['REQUEST_METHOD'] == "GET") 
                die("<html><head><meta http-equiv='refresh' content=\"5 $_SERVER[REQUEST_URI]\"></head><body><table border='0' width='100%' height='100%'><tr><td><h3 align='center'>The server load is very high at the moment. Retrying, please wait...</h3></td></tr></table></body></html>");
            else
                die("Too many users. Please press the Refresh button in your browser to retry.");
        default:
            die("[" . mysql_errno() . "] dbconn: mysql_connect: " . mysql_error());
        }
    }
    mysql_select_db("sh") or die(mysql_error());
    mysql_set_charset('utf8');
    $res = mysql_query("SELECT * FROM users WHERE id = ".htmlentities($_SESSION["id"])." AND enabled='yes' AND status = 'confirmed'");// or die(mysql_error());
    $rows = mysql_fetch_assoc($res);
    if (!$rows)
        return $rows;

    if ($autoclean)
        register_shutdown_function("autoclean");
}
?>

if i echo a statement after return $rows, it echos. What am i doing wrong?

3
Contributors
13
Replies
34
Views
3 Years
Discussion Span
Last Post by diafol
Featured Replies
  • 1
    diafol 3,669   3 Years Ago

    ## functions.php ## function connect() { $mysqli = new mysqli(HOST, USER, PASSWORD, DATABASE); return $mysqli; } ## mypage.php ## require 'functions.php'; $db = connect(); $result = $db->query("SELECT * FROM users WHERE id = '2'"); $rows = $result->fetch_array(MYSQLI_ASSOC); ... OK, no error handling, but that's what I'd expect. You can wrap … Read More

  • 1

    > ^^That right there tells me you don't even know how to use functions. Go back to basics bro. If i wanted a dickhead answer, i would have asked for it. Thanks diafol. Im still learning how that works. Read More

0

Could this be an issue to do with global connections?

If you explicitly pass in conn,host,username, password as variables to the function does it work?

Second shouldn't you be mysql escaping sessionid. Htmlentities is not the same as mysql escape.

0

No issue with global connections. if i put the mysql_query and assoc in index.php and run it that way, everything works. but since if have it in the function, it just wont return.

if i put an echo after the if statement for the connection, it echos. Also, i include above the function, an include which has the defines for username password and db.

Edited by fobos

0

I think I sussed it

return $rows;

change that to

echo $rows;

When you call a function that returns something you need to echo it. Using return 'something' won't print anything to the web browser. That's probably why it is blank... but you need to do as diafol says... post a more complete code sample.

Edited by iamthwee

0

This is the code i use. I will just revert to mysqli to see what i get. Thanks for the help right now. If i have anything else, ill repost it.

Thanks again guys.

0

Man this is rough, how do i return rows in a function using mysqli. Heres what i have:

db_connect.php

<?php
function dbconn($autoclean = false) {
    $mysqli = new mysqli(HOST, USER, PASSWORD, DATABASE);
    $result = $mysqli->query("SELECT * FROM users WHERE id = '2'");
    $rows = $result->fetch_array(MYSQLI_ASSOC);
    if(!$rows) {
        return;
    }
    if ($autoclean) {
        register_shutdown_function("autoclean");
    }
}
?>

index.php

<?php
dbconn();
echo $rows["username"];
?>
1

functions.php

function connect()
{
    $mysqli = new mysqli(HOST, USER, PASSWORD, DATABASE);
    return $mysqli;
}

mypage.php

require 'functions.php';
$db = connect();
$result = $db->query("SELECT * FROM users WHERE id = '2'");

$rows = $result->fetch_array(MYSQLI_ASSOC);
...

OK, no error handling, but that's what I'd expect. You can wrap up your sql routines in functions too though. When you start doing this, you may find it easier to create a 'class' and move into the realms of OOP.

-2
<?php
dbconn();
echo $rows["username"];
?>

^^That right there tells me you don't even know how to use functions. Go back to basics bro. The issue here isn't using mysql over mysqli although diafol makes a good point... it is deprecated.

The issue is you don't know the basics of using a function and how to return it back and print it out!

Edited by iamthwee

1

^^That right there tells me you don't even know how to use functions. Go back to basics bro.

If i wanted a dickhead answer, i would have asked for it.

Thanks diafol. Im still learning how that works.

0

Perhaps we should remind ourselves that this is primarly a learning forum. Users of all abilities are welcome, and as long as OPs have made an effort, they deserve courteous answers. We can decide whether to reply or not.

0

Wait, so was i in the wrong? i noticed aimthwee got two down votes. I do agree with you, but what he said is something that i wasnt expecting. I like this site and i have helped out many of people, but never am im i blunt like that. I was just mearly asking how to return a fetch array in a function.

I know your way works diafol, but just trying to, again return it in a function. Thanks again guys for your support.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.