Hi, im new here need some help with a VERY simple code

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
InfestedOnes
Forum Newbie
Posts: 3
Joined: Sun Nov 16, 2003 8:03 am

Hi, im new here need some help with a VERY simple code

Post by InfestedOnes »

Hey can someone help me fix this up?

Code: Select all

<?php
 mysql_connect("localhost"," db", "pass"); 
 mysql_select_db("db");
session_start();
session_register("$id");
$id=username;  $sql="select * from bb1_user_table where userid='$id'"; 
$res=mysql_query($sql) or die(mysql_error());$row=mysql_fetch_object($res);
echo "$row->username ";
?>
Since my var id is empty if i did $id=1 it would display user one, i want it to dsiplay whatever username your logged in as, i think it has to do with $id=1 only change it to $id=row username? please help[/quote]
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

Code: Select all

$sql="SELECT `username` FROM `bb1_user_table` WHERE `userid` = '$id'";
mysql_result(mysql_query($sql), 0,0);
I believe that's what you're looking for.
Cruzado_Mainfrm
Forum Contributor
Posts: 346
Joined: Sun Jun 15, 2003 11:22 pm
Location: Miami, FL

Post by Cruzado_Mainfrm »

note this line:

Code: Select all

<?php
mysql_connect("localhost"," db", "pass"); 
?>
it has an space before 'db', see?
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

Guys, it'll also throw up an error, not a valid link resource at line bla bla this way, you need two arguments for mysql_query(). And InfestedOnes, please post here, do not ask for help via a PM. It's under the rules. I'm not trying to be mean or anything, being a newbie is not an excuse either.

Anyhow, corrections:

Code: Select all

<?php
session_start(); // put session start on top

$connection = mysql_connect("localhost","user", "pass"); // store it under a variable so you can use it later on
$database = mysql_select_db("db"); // same thing here

$id = $_GET['username']; // im just guessing it came form a $_GET, $id=username wasn't correct either, you'll need quotes

$_SESSION['id'] = $id; // use $_SESSION and register it AFTER you declare $id

$sql = "SELECT * FROM 'bb1_user_table' WHERE userid='$id'";

$res = mysql_query($sql, $connection) or die(mysql_error()); // you need two arguements for the mysql_query()

$row = mysql_fetch_array($res); // what was it doing in or die()? you want it only to fetch if it died? O_O

echo $row['user']; // what's echo "$row->username ";??? it's not a object, believe me, don't get into OOP so fast
?>
-Nay
InfestedOnes
Forum Newbie
Posts: 3
Joined: Sun Nov 16, 2003 8:03 am

Post by InfestedOnes »

thanks im gonna try it
InfestedOnes
Forum Newbie
Posts: 3
Joined: Sun Nov 16, 2003 8:03 am

Post by InfestedOnes »

well if i enter from username this happens:
You have an error in your SQL syntax near 'from bb1_user_table where userid=''' at line 1
EDIT: What im trying to do is display the username of who your logged in as , my var id is empty so i needed id = 1 or whatever number for that username but that only displays the username for that row, how could I display whatever username your logged in as using sessions and select?
Paddy
Forum Contributor
Posts: 244
Joined: Wed Jun 11, 2003 8:16 pm
Location: Hobart, Tas, Aussie
Contact:

Post by Paddy »

Concatenate it like this and see if it works.

Code: Select all

<?php
$sql = "SELECT * FROM 'bb1_user_table' WHERE userid='".$id."'"; 
?>
If that doesn't work replace $id with 1 and see if that even works. If that doesn't or does at least you know where the problem is.
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

also, a few things : are you using mysql or SQL? Also, open up your HTML document file where you type in the USERNAME/PASSWORD, and edit the source. What you are looking for is if you are passing a POST statement in the form itself. If you are, instead of using $_GET, try using $_POST.
Post Reply