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?

Member Avatar for iamthwee

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.

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.

Member Avatar for diafol

nothing should be echoed after return. SHow the code you used.

Member Avatar for diafol

You'd do far better to use mysqli/PDO. This code looks 5-10 years out of date.

Member Avatar for iamthwee

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.

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.

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"];
?>
Member Avatar for diafol
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.

Member Avatar for iamthwee
<?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!

^^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.

Member Avatar for diafol

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.

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.

Member Avatar for diafol

My comment was not directed at you fobos :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.