What is that code so i can make a variable like $username know to look in the username field in database and tell you the information?
Please reply fast.
| SitePoint Sponsor |




What is that code so i can make a variable like $username know to look in the username field in database and tell you the information?
Please reply fast.
It would be simple select
I haven't tried this code, so please check if everything is ok.PHP Code://connect to the base
if(!($db = @mysql_connect(BASESERVER, BASEUSER, BASEPASS))) //connect to base host
die("Cannot connect to the base server.");
if(!@mysql_select_db(BASENAME,$db)) //select base
die("Database doesn't exist!!!");
//this is the actual query
$sql = "SELECT * FROM your_table WHERE Username = '".$username."'";
//perform query
if(($result = @mysql_query($sql,$db)) == 0) //Execute SQL query
{
echo "\n<HR>Database error: <B>".mysql_error()."</B><BR><BR>\n";
die("Query was (<B>$sql</B>) in file <B>$PHP_SELF</B>");
}
//get results
if(mysql_num_rows($result) == 0)
{
echo "No results";
}
else
{
$user = mysql_fetch_assoc($result);
}
-- Jelena --


Hope this helps. Did it as fast as i could
RegardsPHP Code:
$Query = @mysql_query(Select * from tablename);
if (!$Query) {
exit('<p>Error retrieving stuff from database!<br />'.
'Error: ' . mysql_error() . '<p>Try Later</p>');
}
while ($Tablename = mysql_fetch_array($Query)) {
$variable = $Tablename['itemname'];
$variable2 = $Tablename['itemname2'];
}
Keith




ok i am using kivisons code and on this part $Query = @mysql_query(Select * from tablename);
what do i add or delete i tried deleting * and adding username but it didnt work
you need
SELECT * FROM table WHERE Username = '".$username."'
-- Jelena --




I get the error
Parse error: parse error, unexpected T_STRING in /home/chat/public_html/vars.php on line 3
The code on that line is
$Query = @mysql_query(SELECT * FROM table WHERE Username = '".$username."'
Change that toOriginally Posted by Tidal Wave
PHP Code:$Query = @mysql_query("SELECT * FROM table WHERE Username='$Username'");




Error retrieving stuff from database!
Error: No Database Selected
Try Later
THATS THE ERROR IM GETTING NOW
You need to connect to db first as I posted before.
-- Jelena --




Ok it works now but the filewont work.
Here is the vars.php file:
<?
include 'config.php';
$Query = @mysql_query("SELECT * FROM $table WHERE Username='$Username'");
if (!$Query) {
exit('<p>Error retrieving stuff from database!<br />'.
'Error: ' . mysql_error() . '<p>Try Later</p>');
}
while ($table = mysql_fetch_array($Query)) {
$username = $table['username'];
$variable2 = $Tablename['itemname2'];
}
?>
Here is the cpanel.php file:
<?
include 'config.php';
include 'vars.php';
echo "Hello $username here is your information:";
echo "<br>";
echo "First Name: $firstname";
echo "<br>";
echo "Last Name: $lastname";
echo "<br>";
echo "Email Address: $email";
echo "<br>";
?>
when i go to www.chat.ninechat.com/cpanel.php it should say hello $username ect.




Does anyone know how to make it work?
If username is unique then you don't need while part as it will return only one row, but I do recommend to check if your query returned any results with
after that just call mysql_fetch_assoc which will return associative array.PHP Code:if(mysql_num_rows($Query) == 0)
//no results
Your username should be in $user['Username']PHP Code:$user = mysql_fetch_assoc($Query)
-- Jelena --




im confused you want me to make a php file named mysql_fetch_assoc.php and put in it:
if(mysql_num_rows($Query) == 0)
//no results
then in the file cpanel.php change $username = $table['username'];
to $user = mysql_fetch_assoc($Query)
is that right?
No, I want you to call those functions after performing your query.
And yes, echo in cpanel username as $user['Username']
-- Jelena --




im still confused isnt there a more simple way
Huh... I don't think so.. This is the way I would use.
-- Jelena --




so is there anyone else who can help me?


honestly, Tidal, I think you need to read a tutorial. There's too much information for posts to help you. There are good ones on SitePoint, WebDev, even WebMonkey. Google "PHP MySQL tutorials" and I'm sure you'll get it.
Fesh
imagine what it's going to be like





Couple Questions::
1. Is your Username column in the database username or Username -- it matters.
2. Is your $table variable set? Try echoing it out and see what happens. If you don't have a table name than it won't return any values. Also remember that variable set within a function are only available within the function itself unless set otherwise.





BTW -- What's not working now? Can you post the code as it appears now? I'd be more than happy to give it a shot!
Bookmarks