Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
php fetch username from Mysql
Old 01-21-2010, 05:20 AM php fetch username from Mysql
Novice Talker

Posts: 8
Name: robert
Trades: 0
Ok sorry to make this my first post but I am hoping I can maybe get guidance on probably an easy question .

I have a MySql database .

I have php code that is using Twitter API . it all works but at the moment you have to edit the code to add username . But this is no good as with over 300 sites I would find it easer if the people running the site could enter the details username them selves . Now I can get it submit to the database but how to I then get it out of the database to replace this code .

Code:
define('TWITTER_USER',        'username');

I know i could probably explain a bit better but I um banging my head on the desk and I have hit a brick wall . And help would be great
greatness is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 01-21-2010, 07:16 AM Re: php fetch username from Mysql
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
I suppose your users have to log in your system at a moment.
You probably have something in a session that reflect this.

What you need, is to know which id they have on your system.
If I suppose that you would store the twitter name in the same tbale as the user informations, it would be:
PHP Code:
$q="select twitterName from users where userId={$_SESSION['userId']}";
$r=mysql_query($r);
$twitterUser=NULL;
while(
$o=mysql_fetch_object($r)){
  
$twitterUser=$o->twitterName;
}

if(
$twitterUser!==NULL){
  
//and now, query twitter with the username, but only if the user have a twitter username defined.

And to even make it less db intense, we can cache the twitter username:
PHP Code:
if(!isset($_SESSION['twitterUser'])){
  
$q="select twitterName from users where userId={$_SESSION['userId']}";
  
$r=mysql_query($r);
  while(
$o=mysql_fetch_object($r)){
    
$_SESSION['twitterUser']=$o->twitterName;
  }
}
else{
  if(
$_SESSION['twitterUser']!==NULL){
     
//and now, query twitter with the username, but only if the user have a  twitter username defined.
  
}

Not sure it's what you asked, but does it answer your question ?
__________________
Only a biker knows why a dog sticks his head out the window.

Last edited by tripy; 01-21-2010 at 07:21 AM..
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 01-21-2010, 08:03 AM Re: php fetch username from Mysql
Novice Talker

Posts: 8
Name: robert
Trades: 0
this is where I'm going and what I am asking thank you ..

the table it is in is called configuration with the tile twitterName . How would I need to change the code so it would pulled out of this part of the database . or do you think it would be better to give this information its own part of the database ?
greatness is offline
Reply With Quote
View Public Profile
 
Old 01-21-2010, 08:33 AM Re: php fetch username from Mysql
Novice Talker

Posts: 8
Name: robert
Trades: 0
sorry but I am thinking this may be easer than using twitter api .. as all I am doing i wanting our website owners to display there profile information .. This still lands me with the same problem as how to get the username from the database . So same question but more information as far as code .

How can this code be changed so it pulls the username from the mysql database ? if it is in fact possible


Code:
 
<script src="http://widgets.twimg.com/j/2/widget.js"></script>
<script>
new TWTR.Widget({
  version: 2,
  type: 'profile',
  rpp: 4,
  interval: 6000,
  width: 250,
  height: 300,
  theme: {
    shell: {
      background: '#333333',
      color: '#ffffff'
    },
    tweets: {
      background: '#000000',
      color: '#ffffff',
      links: '#4aed05'
    }
  },
  features: {
    scrollbar: false,
    loop: false,
    live: false,
    hashtags: true,
    timestamp: true,
    avatars: false,
    behavior: 'all'
  }
}).render().setUser('username').start();
</script>
greatness is offline
Reply With Quote
View Public Profile
 
Old 01-21-2010, 09:53 AM Re: php fetch username from Mysql
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Quote:
the table it is in is called configuration with the tile twitterName . How would I need to change the code so it would pulled out of this part of the database .
Again, it depends on your db structure.
Still, considering that you configuration table have a link on a user table on the id, the sql query would be, if you store the user id in the session:
Code:
select c.twitterName
from configuration c
where c.userId={$_session['userId']}
and if you have the username in the session, something like that:
Code:
select c.twitterName
from configuration c
  inner join users u
    on u.userId=c.userId
where u.username={$_session['userName']}
Quote:
How can this code be changed so it pulls the username from the mysql database ? if it is in fact possible
No, it's not possible.
Javascript on the browser side cannot reach the database on the server side.
But after you'll have fetched it from the db, you will need to output it in javascript, to have the twitter API knowing it:
PHP Code:
<script  src="http://widgets.twimg.com/j/2/widget.js"></script>
<script>
<?php
$q
="select c.twitterName from configuration c where  c.userId={$_session['userId']}";
$r=mysql_query($r);
$twitterUser=NULL;
while(
$o=mysql_fetch_object($r)){
  
$twitterUser=$o->twitterName;
}

if(
$twitterUser!==NULL){
  
//and now, query twitter with the username, but only if the user have a  twitter username defined.
  
  //Output the javascript to set a variable with the twitter username  here
  
echo "var twitterUser=$twitterUser";
}  
?>
new TWTR.Widget({
  version: 2,
  type: 'profile',
  rpp: 4,
  interval: 6000,
  width: 250,
  height: 300,
  theme: {
    shell: {
      background: '#333333',
      color: '#ffffff'
    },
    tweets: {
      background: '#000000',
      color: '#ffffff',
      links: '#4aed05'
    }
  },
  features: {
    scrollbar: false,
    loop: false,
    live: false,
    hashtags: true,
    timestamp: true,
    avatars: false,
    behavior: 'all'
  }
}).render().setUser(twitterUser).start();
</script>
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 01-22-2010, 10:40 AM Re: php fetch username from Mysql
Novice Talker

Posts: 8
Name: robert
Trades: 0
Hello tripy ,

I have it all sorted now .. thanks so much for the help
greatness is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to php fetch username from Mysql
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.57138 seconds with 12 queries