lindgrenj6
lindgrenj6

Reputation: 13

Can't get PHP to pull line entry from mySQL as array

Ok fellas, I've been working with a SQL database via PHP for a couple days here now at work and I cannot for the life of me get it to work I have someone else who worked on this project and they have all sorts of functions that work at pulling one element out of the line, but I'm trying to pull an entire line IE one persons data in the SQL database.

Here is the code they have that pulls one entry from the database

public function interact($query,$resultVal) {

        // Create a new mysqli to get the database
        $db = new mysqli("$My_IP","$username","$password","calendar");

        // If there is an error connecting to mysql, show an error to the user
        if ($db->connect_errno) {
            echo "Failed to connect to MySQL: (" . $db->connect_errno . ") " . $db->connect_error;
        }
        // If there is no database to get the information from, send an error
        if(!$db) {
            echo "Error: Could not acces database in mySQL";
            exit;
        }

        // Select the calendar database to use
        mysql_select_db("calendar");

        if($resultVal) { 
            //Get result using the $query
            $result =  mysql_query($query);

            return $result;
        } 
        else {
            mysql_query($query);
        }
    }

Which is called using this:

$variable = $dbact ->interact($query, true);

So now I'm trying to get it to work by calling an entire row, heres the function I wrote:

public funciton get_person_data($username)
    {
        $dbact = new DbInteraction();
        $query = "SELECT first_name, last_name, phone, email, contact FROM person WHERE username = ".$username;

        $result = $dbact->interact($query, true);
        $data = mysql_fetch_array($result);

        return $data;
    }

Which I then call via

$info = $dbact->get_person_data($username);

Then I use a mySQL fetch array

while ($row = mysql_fetch_array($data)) {
            $name = $row['first_name'];
            echo $name;
        }

Any help would be appreciated. I've been stuck on this forever. Thank you for looking!

Upvotes: 0

Views: 431

Answers (2)

Machavity
Machavity

Reputation: 31624

So a couple of things. First, you're mixing mysql and mysqli in there. So it connects in mysqli (in OOP mode no less) and then it uses mysql_query() (which is procedural). That's just weird. The way to to a query in OOP is

$db->query($query);

Second, why are you creating a DB connection inside your function? I'd create it outside and pass your $db as a parameter.

$db = new mysqli("$My_IP","$username","$password","calendar");
// If there is an error connecting to mysql, show an error to the user
    if ($db->connect_errno) {
        echo "Failed to connect to MySQL: (" . $db->connect_errno . ") " . $db->connect_error;
    }
    // If there is no database to get the information from, send an error
    if(!$db) {
        echo "Error: Could not acces database in mySQL";
        exit;
    }

public function interact($query, $resultVal, \mysqli $db) {
    $result =  $db->query($query);
    if($resultVal) return $result;
}

Upvotes: 2

jraede
jraede

Reputation: 6896

You're calling mysql_fetch_array() twice. Once in get_person_data and once in your while loop. If you're just trying to get one row, then why do you have a while loop in the first place? It should be:

$info = $dbact->get_person_data($username);
$name = $info['first_name']; 

Note, you were also doing mysql_fetch_array on $data, which wasn't defined as far as I can tell (should have been $info, if mysql_fetch_array was the right code to use there)

Upvotes: 1

Related Questions