asdasd asda
asdasd asda

Reputation: 65

PHP return Database row details by user id

I have my own cms that built ,

I used on the userid AUTO_INCREMENT My db users look like this till now:

    userid | username | fname | email | password | salt | timastamp
   ----------------------------------------------------------------
      1       test      b.k   [email protected]  dsfsd...  df...    11334544
      2       test1     s.k   [email protected]  dsfsd...  df...    11334544

Here i removed the AUTO_INCREMENT and change the userid row from int to varchar And now i have this :

    userid | username | fname | email | password | salt | timastamp
   ----------------------------------------------------------------
    4CsdGf     test      b.k   [email protected]  dsfsd...  df...    11334544
    5C6dd3     test1     s.k   [email protected]  dsfsd...  df...    11334544

I have removed from the userid Structure the AUTO_INCREMENT and using the following script to gerenate new id for every new register account:

function genID()
{
$seed = str_split('abcdefghijklmnopqrstuvwxyz' 
.'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 
.'0123456789' ); // and any other characters
shuffle($seed); // probably optional since array_is randomized; this may be redundant
$rand = '' ;
foreach (array_rand($seed, 5) as $k) $rand .= $seed[$k];

return $rand;

}

Till now i used this function to get all user data by userid:

 function get_user_data_by_userid($one_page_id)
{
    $one_page_id = $this->db_clean($one_page_id);

    $query = "SELECT * FROM users WHERE userid = ".$one_page_id;

    //die($query);
    mysql_query("SET NAMES 'utf8'");

    $result = mysql_query($query, $this->connection);

    if(!$result) //ERROR IN YOUR SQL QUERY
    return false;

    if(mysql_num_rows($result)==0) //NO ROWS IN TABLE pages
    return false;

    $row = mysql_fetch_array($result, MYSQL_ASSOC);

    return $row;
}

 $user_data = $func->get_user_data_by_userid($user_id);

 //example of take specific data :

  echo $user_data['userid'];

But its wont working anymore , its not get the data by the userid since i have change the user id from AUTO_INCREMENT to my own Generate ID'S function , Any idea what wrong ?

I have did die to query at $func->get_user_data_by_userid() and its working perfect :

SELECT * FROM users WHERE userid = 63FWo

but its not retrive the data from that id why any idea? Thanks allot.

UPDATE: The fix :\ :

 $query = "SELECT * FROM users WHERE userid = '$one_page_id'";

forgot quotes..

Upvotes: 0

Views: 86

Answers (2)

vinayak jadi
vinayak jadi

Reputation: 959

Use Apostrophe

when you try to select int values

$query = "SELECT * FROM users WHERE userid = 1";

when you try to select string values

$query = "SELECT * FROM users WHERE userid = '63FWo'";

Upvotes: 0

Gourav
Gourav

Reputation: 1774

change to this.

  SELECT * FROM users WHERE userid = '63FWo';

Upvotes: 2

Related Questions