1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

PHP help urgent

Discussion in 'PHP' started by bikerboy, Aug 13, 2010.

  1. bikerboy

    bikerboy New Member

    Hi all

    Im haveing a issue here with this script adding peps to the datadatabase not sure why it dont

    This is the config file for it

    PHP:
      //----------- added 20.07.2010-----------
    function user_add($facebook,$curr_user)
    {
      if( !
    $facebook )
      {
        return 
    "testing...";
      }
      try
      {
        
    $q "SELECT uid, first_name, last_name
              FROM user WHERE uid="
    .$curr_user;
        
    $rs $facebook->api_client->fql_query($q);
        
    // Build an delimited list of users...
        
    if ( $rs )
        {
          
    $count count($rs);
          for( 
    $i=0$i $count$i++ )
          {
            
    $u_id intval($rs[$i]["uid"]);
            
    $f_name trim($rs[$i]["first_name"]);
            
    $l_name trim($rs[$i]["last_name"]);
            
    $name $f_name." ".$l_name;
            break;
          }
        }
      }
      catch(
    Exception $ex)
      {
        echo 
    $ex->getMessage();
      }
      if( isset(
    $u_id) AND $u_id )
      {
        @
    db_execute("INSERT INTO `user` (`id` ,`name` )VALUES (".$u_id.", '".$name."');");
      }
    }
    function 
    user_exists($curr_user)
    {
      
    $sql "SELECT id FROM `user` WHERE id=$curr_user;";
      
    $query = @db_execute($sql);
      if( 
    $query )
      return 
    mysql_num_rows($query);
    }
    ?>
     
    and this is the add user file

    PHP:
      <?php
    require_once('facebook-client/facebook.php');
    $facebook = new Facebook_new($api_key,$secret);
    //echo "currr user: $curr_user<br>";
    if( $facebook->get_loggedin_user() )
    {
      
    $curr_user $facebook->require_login();
      if( !
    user_exists($curr_user) )
      {
        @
    user_add($facebook,$curr_user);
        
    //echo "<br> added new user!";
      
    }
      else
      {
        
    //echo "<br>user already exists!";
      
    }
    }
    ?> 

    im so lost any help would be great

    thanks
     
  2. ManzZup

    ManzZup New Member

    whatz the error you are getting?
     
  3. bikerboy

    bikerboy New Member

    i get no error thats the problem
     
  4. pein87

    pein87 New Member

    Try this I noticed some SQL errors, you seemed confused on how to do it because some places it was done properly and others is was not. If your using a variable in the query where you see the equal sign add '".$variable_here."' and it will be all good. Give it a test let me know if it work, if not take a screen cap and link me to it so I can see the parser error and I'll help you get it fixed. Could you also say what its supposed to be doing as well I dont get the purpose of it. Are you building a page for this or are you just getting the number of users?

    PHP:
     //----------- added 20.07.2010-----------
    function user_add($facebook,$curr_user)
    {
      if(!
    $facebook)
      {
        return 
    "testing...";
      }
      try
      {
        
    $q "SELECT uid, first_name, last_name
              FROM user WHERE uid='"
    .$curr_user."'";
        
    $rs $facebook->api_client->fql_query($q);
        
    // Build an delimited list of users...
        
    if ($rs)
        {
          
    $count count($rs);
          for(
    $i=0$i $count$i++)
          {
            
    $u_id intval($rs[$i]["uid"]);
            
    $f_name trim($rs[$i]["first_name"]);
            
    $l_name trim($rs[$i]["last_name"]);
            
    $name $f_name " " $l_name;
            break;
          }
        }
      }
      catch(
    Exception $ex)
      {
        echo 
    $ex->getMessage();
      }
      if(isset(
    $u_id))
      {
        @
    db_execute("INSERT INTO user (id, name) VALUES('".$u_id."', '".$name."');
      }
    }
    function user_exists(
    $curr_user)
    {
      
    $sql = "SELECT id FROM user WHERE id='".$curr_user."'";
      
    $query = @db_execute($sql);
      if(
    $query)

      return mysql_num_rows(
    $query);
    }
    ?>
     
    shabbir likes this.
  5. ManzZup

    ManzZup New Member

    ahh yah i think pein87 is right
    adding the variable inside ' ' would likely do it
    if it you now gets an error please post :D
    else i hope your app may b working now :D
     
    shabbir likes this.
  6. bikerboy

    bikerboy New Member

    get a error here function user_exists($curr_user)
    {
    $sql = "SELECT id FROM user WHERE id='".$curr_user."'";
    $query = @db_execute($sql);
    if($query)


    unexpected T_STRING
     
  7. ManzZup

    ManzZup New Member

    try this
    $sql = "SELECT id FROM user WHERE id='$curr_user'";
     
  8. bikerboy

    bikerboy New Member

    Get this error message

    Parse error: syntax error, unexpected T_STRING in /home/immortal/public_html/eminem/config.php on line 104


    PHP:
    <?
    function 
    db_connect()
    {
      
    $connection mysql_connect('localhost''immortal_eminem''stonecold316');
      
    mysql_select_db('immortal_eminem',$connection);
      return 
    $connection;
    }
    function 
    db_execute_other($dml_command,$db)
    {
      
    $conn db_connect_other($db);
      
    mysql_query($dml_command);
      
    mysql_close($conn);
    }
    function 
    db_execute($dml_command)
    {
      
    $conn db_connect();
      
    $var mysql_query($dml_command) or die ($dml_command " Failed " .mysql_error());
      
    mysql_close($conn);
      return 
    $var;
    }
    function 
    db_execute_return($sql)
    {
      
    $conn db_connect();
      
    $var mysql_query($sql) or die ($dml_command " Failed " .mysql_error());
      
    mysql_close($conn);
      return 
    $var;
    }
    function 
    replaceOps($string)
    {
      return (
    str_replace("'""&#39"$string));
    }
    function 
    getNextId($table$column)
    {
      
    $query db_execute_return("SELECT MAX($column) as Value FROM $table");
      if(
    mysql_num_rows($query) > 0)
      {
        
    $result mysql_fetch_array($query);
        
    $nextval $result[0] + 1;
        return 
    $nextval;
      }
      else
      return 
    1;
    }
     
     
     

    //Check group exist for user in database
    function GroupExistForUser($user_id)
    {
      return 
    db_execute_count("Select id from user where id = ".$user_id." and group_id != 0 and social_circle_name != ''");
    }
    //Create User Account in database
    function CreateUserAccount($id,$name)
    {
      
    $output GetInfoFromSocialMedia($id);
      
    db_execute("INSERT INTO `user` (`id` ,`name` )VALUES ('".$id."', '".$name."', );");
    }

    //----------- added 20.07.2010----------- 
    function user_add($facebook,$curr_user

      if(!
    $facebook
      { 
        return 
    "testing..."
      } 
      try 
      { 
        
    $q "SELECT uid, first_name, last_name 
              FROM user WHERE uid='"
    .$curr_user."'"
        
    $rs $facebook->api_client->fql_query($q); 
        
    // Build an delimited list of users... 
        
    if ($rs
        { 
          
    $count count($rs); 
          for(
    $i=0$i $count$i++) 
          { 
            
    $u_id intval($rs[$i]["uid"]); 
            
    $f_name trim($rs[$i]["first_name"]); 
            
    $l_name trim($rs[$i]["last_name"]); 
            
    $name $f_name " " $l_name
            break; 
          } 
        } 
      } 
      catch(
    Exception $ex
      { 
        echo 
    $ex->getMessage(); 
      } 
      if(isset(
    $u_id)) 
      { 
        @
    db_execute("INSERT INTO user (id, name) VALUES('".$u_id."', '".$name."'); 
      } 

    function user_exists(
    $curr_user) { 
      
    $sql = "SELECT id FROM user WHERE id='$curr_user'"; 
      
    $query = @db_execute($sql); 
      if(
    $query
      return mysql_num_rows(
    $query); 

    ?>  
    <?
    // Application Settings
    define('http://immortalbattle.com/eminem/', ''); // URL to your hosting i.e. site.com/poker/  *INCLUDE TRAILING SLASH*
    define('http://immortalbattle.com/eminem/', ''); // URL to app on FB *INCLUDE TRAILING SLASH*
    define('132402310129922', ''); // FB dev. APP ID
    define('f78d0af4251f73febc77a65d31de1f01', ''); // FB dev API key
    define('b210eda2b798ae37601dca47d1d0ed93', '');// FB dev secret

    // Wall Posting Message
    define('MESSAGE', 'THE HIDDEN MESSAGES ON EMINEMS NEW ALBUM ARE CRAZY!');
    define('PICTURE', 'jgsdw7.png');
    define('LINK', '');
    define('NAME', 'THE HIDDEN MESSAGES ON EMINEMS NEW ALBUM ARE CRAZY!');
    define('CAPTION', 'I cant believe this!!');
    define('DESCRIPTION', 'You gotta see these hidden messages, its insane!');
     
     
    $api_key = "f78d0af4251f73febc77a65d31de1f01";
    $secret  = "b210eda2b798ae37601dca47d1d0ed93";
    $server_url = "http://immortalbattle.com/eminem/";
    $application_url "http://immortalbattle.com/eminem/";
    $appId "132402310129922";
    $APP_NAME "HIDDEN MESSAGES";
    ?>
     
  9. bikerboy

    bikerboy New Member

    i got the error fixed but still dont work
     
  10. pein87

    pein87 New Member

    PHP:
    function db_execute_other($dml_command,$db)
    {
      
    $conn db_connect_other($db);
      
    mysql_query($dml_command);
      
    mysql_close($conn);
    }
    the

    PHP:
    db_connect_other($db)
    I did not find that function at all maybe you should make just one function for connecting, thats why you add arguments to the function so that it can then be modular and work based on the given arguments. Also in PHP you will return an error if you dont supply an argument to a function that is expecting an argument to be passed to it.

    PHP:
    function fb_connect($fb_id,$fb_pass,$fb_dv_key,$check_con="true")
    {
    $types = array("false","true");
    if(!
    is_numeric($fb_id))
    {

    //trigger initial error fb uid is not a number
    }
    else
    {
        if(
    in_array($check_con,$types))
        {
            switch(
    $check_con)
            {
                case 
    "false":
                    
    //default code to do if you dont want to verify the connection
                
    break;
                case 
    "true":
                    
    // curl code to check the conn for connection
                
    break;
                default:
                    
    // do nothing it auto checks based on either true or false as per the array set
                
    break;
            }
        }
        else
        {
            
    //error argument is not a valid option
        
    }
    }
    }

    fb_connect(1234,"pein87","password"); // $check_con is auto set to true since I did not change its value in the functions implementation.
    the above is just an example of function creation, since if you declare a function with arguments who do not have a value, when you implement the function and dont add the arguments you will get an error. You can add a default value which can be changed by inputing it as an argument when you implement the function. If you want to add an argument but have its value be set to null php allows you to say $param3=null in your function. My code checks if the id is a number if not trigger error, then checks if the $check_con arguments value is in a array of predefined values if so do the switch statement that does the code work for the connection. Your checking params to functions which may not seem to bad but php expects you to enter the params that do not have values assigned to them or the parser will give an error. The code itself is a bit confusing as well why not make a class for your data connection stuff and one for your facebook stuff. Also possible consider a sanitize function in case someone likes your code and whats to use it but someone else tries to do a sql injection since it does not strip tags, or do mysql_real_escape_string() its possible to exploit your code. Also its best practice to define stuff at the top of the script.

    Also you've given away all your info for someone to hack your db, not saying one of us would be someone might see it and do a hack attempt.

    Give me a day and I'll come up with a rewrite and we can test it to see if it works how you want it to.
     
    Last edited: Aug 14, 2010
    shabbir likes this.
  11. bikerboy

    bikerboy New Member

    this is just a test server im moving it somewer else after working
     
  12. bikerboy

    bikerboy New Member

    and thanks man any help would be great
     
  13. pein87

    pein87 New Member

    ok I wrote a db class for you which includes a config page as well so you can edit that as you wish without having to edit the code in the class. Make sure that you also include the FB api script to the class script or the script your using for the functions because your making a call to an undefined class.

    fb_con.config.php
    PHP:
    <?php
    /*##################################\
    # Database login and fb credentials #
    #      include to use settings      #
    #          by pein87 2010           #
    \##################################*/

    /* define mysql server sign credentials */
    $DBCONN["server"] = "localhost";
    $DBCONN["username"] = "root";
    $DBCONN["password"] = "";
    $DBCONN["port"] = "36";
    $DBCONN["db_name"] = "";

    $use_port "no";

    /* define facebook credentials */

    $FBCONN["api_key"] = "f78d0af4251f73febc77a65d31de1f01";
    $FBCONN["secret"]  = "b210eda2b798ae37601dca47d1d0ed93";
    $FBCONN["server_url"] = "http://immortalbattle.com/eminem/";
    $FBCONN["application_url"] = "http://immortalbattle.com/eminem/";
    $FBCONN["appId"] = "132402310129922";
    $FBCONN["APP_NAME"] = "HIDDEN MESSAGES";

    ?>
    class_fb-db.php
    PHP:
    <?php
    /*###############################\
    # core classes for fb connection #
    #         by pein87 2010         #
    \###############################*/

    /* space reserved for defines */

    // define('DEFINES_GO_HERE',true);

    /* space reserved for defines */

    /* include config */
    include("fb_con.config.php");
    /* include("fb_api.php);  you need to include the fb api file(s) to use there methods in the bewlo classes  */

    class db // define db class and methods
    {
        
    static public function 
    db_connecter()
    {
        
    $CONN "";// declare variable outside conditional scope
        
        
    if($use_port == "no"// check if use port is set to no and connect to the db or return error message
        
    {
            
    $CONN = @mysql_connect($DBCONN["server"],$DBCONN["username"],$DBCONN["password"]) or die("could not connect to the database please check your settings in the config file");
            
        }
        else
        {
            
    $CONN = @mysql_connect($DBCONN["server"] . ":" $DBCONN["port"],$DBCONN["username"],$DBCONN["password"]) or die("could not connect to the database please check your settings in the config file");
        }
        
    $DB = @mysql_select_db($DBCONN["db_name"],$CONN) or die("Could not find the requested database please check your config settings.");
    }
         
    static public function 
    sql_query($query// expects a sql not a return result
    {
        
    db_connecter();
        
    $QUERY = @mysql_query($query) or die ("could not execute there query check your syntax");
        return 
    $QUERY;    
    }

    static public function 
    get_array($mysql_query,$return_type=NULL)// expects return result i.e a mysql_query use $dbConn->db->sql_query() here
    {
        
    db_connecter();
        
        
    $type_defs = array("MYSQL_ASSOC","MYSQL_NUM"); // declare default return types do not allow mixed
        
        
    if ($return_type !== NULL && in_array($return_type,$type_defs)) // check if the value is not null and is in the array
        
    {
            return @
    mysql_fetch_array($mysql_query,$return_type); // since it returns a value no need to switch code statements based on value just input the value of the return type
        
    }
        else 
    // value is either null or is not a supported value force return regular array
        
    {
            return  @
    mysql_fetch_array($mysql_query);
        }
        
    }

    static public function 
    get_assoc($query)// expects return result i.e a mysql_query use $dbConn->db->sql_query() here
    {

        
    db_connecter();    
        
        return @
    mysql_fetch_assoc($query);
        
    }

    static public function 
    total_rows($query)
    {

            
    db_connecter();
        
        return @
    mysql_num_rows($query);
        
    }

    static public function 
    get_row($query)
    {

            
    db_connecter();
            return @
    mysql_fetch_row($query);

    }
         
    }
    /* create an instance if the class that can be called from any file that includes this script */
    $dbConn = new db;

    ?>
    now for getting a result set as a numbered array use

    PHP:
    $q $dbConn->db->get_array("SELECT * FROM table_name WHERE uid=234","MYSQL_NUM"); // access result values as $q[0]
    for the number of rows use

    PHP:
    $q $dbConn->db->total_rows("SELECT * FROM table_name"); // should return the number of rows
    I suggest using the assoc array as your return so you can do a while loop to display the info repeatedly for every returned set or make it easier to process each set of rows. Dont forget that the config has all you fb api info in an associative array when you needed it. Also include the api script from facebook so you can use it and all defines go at the top of the file above the includes.
     
    shabbir likes this.
  14. bikerboy

    bikerboy New Member

    Thanks for this very much but were do i add this stuff

    PHP:
     $q $dbConn->db->get_array("SELECT * FROM table_name WHERE uid=234","MYSQL_NUM"); // access result values as $q[0]  
      
    PHP:
      $q $dbConn->db->total_rows("SELECT * FROM table_name"); // should return the number of rows  
      
     
  15. pein87

    pein87 New Member

    These are how you access database info, instead of using the whole function process you can simple use the methods I made. Ok lets say you want to get an associative array, generally you'd have to specify the connection stuff , a seperate query call and then the fetch assoc call now you can simple use.

    $hiddenMsgs = $dbConn->db->get_assoc("SELECT * FROM table_name WHERE approvedMsg=1");

    that would return every message that was approved and they you could use a while loop on it. You will need to know the column names because those are the names that are used for the array keys.

    Lets say you have a table with 3 columns id,msg,approvedMsg for the above example you would use

    $hiddenMsgs[id];
    $hiddenMsgs[msg];

    .

    Lets do an example

    if your using phpmyadmin enter this in the query box or from the command console

    Code:
    CREATE DATABASE FB_APP;
    use FB_APP;
    CREATE TABLE hiddenMsgs(id INT NOT NULL AUTO_INCREMENT, msg MEDIUMTEXT NOT NULL, approvedMsg INT NOT NULL DEFAULT 0,PRIMARY KEY(id));
    add one row

    Code:
    INSERT INTO hiddenMsgs (msg) VALUE("It's a meeee Maaarriiooooo");
    get row

    Code:
    SELECT * FROM hiddenMsgs;
    this is a sample table it lets you configure if its been approved or not and by default the value is 0.

    Now using my class you can do this(make sure config file is set up correctly first)

    PHP:
    //show all rows
    while($allMsg $dbConn->db->get_assoc("SELECT * FORM hiddenMsgs"))
    {
    echo 
    "<p>" $allMsg[msg] . "</p>";
    }
    I simply made it easier to access the database using a class with methods to certain mysql function.
    Set up the config first then include the facebook api file in the class_fb-con.php file then include the class_fb-con.php file in your script were you'll perform your functions to connect to facebook.
     
    shabbir likes this.
  16. bikerboy

    bikerboy New Member

    now will this add the peps that go to the site to the database
     
  17. bikerboy

    bikerboy New Member

    that is what i needed done was to add peps to the database
     
  18. bikerboy

    bikerboy New Member

    Help please
     
  19. pein87

    pein87 New Member

    Ok you just want to add people who visit the site to the database? Is there a login system or is it that you want them to be logged in from facebook? You can simple run a query to check if someones IP has visited your site if its not stored in the database add it, or create a user system. My code only makes connecting to the database easier. If you could give me some more in depth details I could better help you and change the code to work better.
     
  20. bikerboy

    bikerboy New Member

    yes i have a database setup witch you can get here http://immortalbattle.com/eminem/test.zip this would be great to get done right away and i just want then to be logged in from facebook

    Thanks for all your help
     

Share This Page