0

Ok. So this code I'm running does not work at all. When I ran the query directly on the server it worked, however this does not work on my webpage. I've been through all the help pages and can't pinpoint the error. Please help.

function getUserData($id) {
        $servername = "******************";
        $dbname = "***********";
        $sql_username = "************";
        $sql_password = "****************";
        $conn = mysql_connect($servername, $sql_username, $sql_password, $dbname);
        $userData = array();
        $sql = mysql_query("SELECT * FROM users WHERE user_id ='".$id."'");
        while ($row = mysql_fetch_assoc($sql)) {

        $userData['id'] = $row['id'];   
        $userData['user_name'] = $row['username'];
        $userData['password'] = $row['password'];       
        $userData['aboutme'] = $row['aboutme'];     
        $userData['first_name'] = $row['fname'];
        $userData['last_name'] = $row['lname'];     
        $userData['gender'] = $row['gender'];
        $userData['user_email'] = $row['email'];    
        $userData['country'] = $row['country']; 
        $userData['language'] = $row['language'];   
        $userData['send_emails'] = $row['sendemails'];
        $userData['date'] = $row['date'];           
        }
        return $userData;
    }
    function getUserId($username) {
        $servername = "*********************";
        $dbname = "*********";
        $sql_username = "***********";
        $sql_password = "***********";
        $conn = mysql_connect($servername, $sql_username, $sql_password, $dbname);
        $sql = mysql_query("SELECT user_id FROM users WHERE user_name = '".$username."'");
        while ($row2 = mysql_fetch_assoc($sql)) {
            return $row2['user_id'];            
        }
    }

This is what calls the fucntions.

    $userData = getUserData(getUserId($_SESSION['valid_user']))
k_feez
  • 1
  • possible duplicate of [PHP - function for mysql\_fetch\_assoc](http://stackoverflow.com/questions/12502568/php-function-for-mysql-fetch-assoc) – Rizier123 Jan 07 '15 at 00:38
  • 3
    `mysql_connect($servername, $sql_username, $sql_password, $dbname)` `mysql_connect()` only accepts 3 basic parameters http://php.net/manual/en/function.mysql-connect.php. The 4th is not for what you think. This is [`mysqli_()`](http://php.net/manual/en/function.mysqli-connect.php) syntax. You need to use `mysql_select_db()` http://php.net/manual/en/function.mysql-select-db.php – Funk Forty Niner Jan 07 '15 at 00:40
  • You're welcome. If you want, I can make it an answer to post it below, instead of just a comment, in order to close the question, where you can accept it and marked as solved. The choice is yours ;-) – Funk Forty Niner Jan 07 '15 at 01:12

1 Answers1

1

mysql_connect($servername, $sql_username, $sql_password, $dbname)

bool mysql_select_db ( string $database_name [, resource $link_identifier = NULL ] )

As per the manual:

http://php.net/manual/en/function.mysql-select-db.php

  • The 4th parameter is not for what you think. This is mysqli_() syntax.

You need to use mysql_select_db() http://php.net/manual/en/function.mysql-select-db.php

From the manual also:

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Not connected : ' . mysql_error());
}

// make foo the current db
$db_selected = mysql_select_db('foo', $link);
if (!$db_selected) {
    die ('Can\'t use foo : ' . mysql_error());
}

From mysql_connect():

resource mysql_connect ([ string $server = ini_get("mysql.default_host") [, string $username = ini_get("mysql.default_user") [, string $password = ini_get("mysql.default_password") [, bool $new_link = false [, int $client_flags = 0 ]]]]] )

Opens or reuses a connection to a MySQL server.


Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141