View Full Version : How does I gets this thang?
tinghank12
04-24-2008, 06:18 AM
I needs some helps with ma thingy...
<?
$query = "SELECT name FROM users";
$result = mysql_query($query);
if($session) {
echo "<b>Hello,<font color=red> $result</font></b>";
} else {
?>
Ya see, I gots a scripty that peoples can registar and then their datas goes into my funny little database :)
I is trying to display users name on site when they login.. but it not work!
It just says, Resource id # 4!
I is mad.!
abduraooft
04-24-2008, 07:37 AM
You need to fetch the values from the $result, see http://in.php.net/mysql_fetch_array
RMcLeod
04-24-2008, 09:52 AM
Although you've run the mysql query, you haven't extracted the data from the result. There are a couple of ways of doing this.
mysql_result (http://uk2.php.net/manual/en/function.mysql-result.php) is probably the easiest if you are only returning one row from the database.
<?php
$query = "SELECT name FROM users";
$result = mysql_query($query);
$name = mysql_result($result, 0, 'name');
if($session) {
echo "<b>Hello,<font color=red> $name</font></b>";
} else {
}
?>
tinghank12
04-24-2008, 04:01 PM
That code before didn't work...
And I don't understand really mysql_fetch_array because there is 3 options...
MYSQL_NUM, MYSQL_ASSOC and MYSQL_BOTH
I've came up with this code so far...
if($session) {
$result = mysql_query("SELECT name FROM users");
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
echo "Hello, $row.";
}
mysql_free_result($result);
BUt now it says, "Hello, Array.Hello, Array.Hello, Array.Hello, Array."
Help please. :(
abduraooft
04-24-2008, 04:04 PM
Try
<?
$query = "SELECT name FROM users";
$result = mysql_query($query) or die(mysql_error);
$row=mysql_fetch_array($result);
if($session) {
echo "<strong style=\"color:red;\">Hello,{$row['name']}</strong>";
} else {
?>
(haven't tested though)
tinghank12
04-24-2008, 04:07 PM
No, it didn't work.
It just said, Hello,
abduraooft
04-24-2008, 04:15 PM
Check the result of echo "rows selected: ".mysql_num_rows($result);
tinghank12
04-24-2008, 04:21 PM
It says rows selected: 4.. which is how many is in that part of the table
rafiki
04-24-2008, 04:24 PM
there are four names and your trying to echo one? how is the script going to no which one to echo?
use foreach() to echo them all
or add a WHERE clause to your sql statement.
You need the 2nd one.
Andrew Johnson
04-24-2008, 04:28 PM
There's no need to use mysql_fetch_array if you are getting only 1 column from 1 row.
Use mysql_result
EG:
//mysql; connect & select db
$query = "SELECT column FROM table WHERE column='value'";
$result_query = mysql_query($query);
$result = mysql_result($result_query);
echo $result;
obviously change to your column,table,value
tinghank12
04-24-2008, 05:15 PM
But what would 'value' be?
WHen each user logs in, it will be different, so how would I tell the script which name to pluck out and display? There would be no other variable or anything to go by I don't think.
abduraooft
04-24-2008, 05:22 PM
WHen each user logs in, Some clue is there!
Pick the name of the logged user and store it in sessions
tinghank12
04-24-2008, 05:37 PM
OK, well I just tried something totally different that I thought would work. Instead of going into the database, when the user logged in, I made a hidden input field, with the value of <? echo "$username"; ?> with $username being the one that they logged in with. I then POSTED that data back to the index.php page, where I am not trying to display their username. For instance, hello, josh123.
Unfortunately, this still doesn't suffice.
I'll post some code here, maybe you guys can figure out if there is something similar that will work?
index.php snippet
<?
$hidden = $_POST['hidden_username'];
if($session) {
echo "Hello, $hidden";
} else {
?>
and the login.php snippet
<form action="index.php" method="POST">
<input type="hidden" name="hidden_username" value=" <? echo "$username"; ?> " />
</form>
And..
$username = $_POST['username'];
$password = $_POST['password'];
Which is where I get the username from, they enter it on a different form.
You should be able to do something like
<?php
$name = $_POST['username'];
if ($session) {
echo "omg lol, what's up, $name";
} ?>
Since $_POST['username'] is already set, I don't see any need for an extra hidden value just to echo something that should already be a displayed form value...
Inigoesdr
04-24-2008, 08:14 PM
If you store the name, or even the user id, in a session when the user logs in, you won't need to post anything. If you store the name, you can just echo it, and if you store the id you can select the name from the database by using the id in the where clause. Your query would look something like this(assuming your user id index is named "id"):
SELECT `name` FROM `users` WHERE `id` = $id
tinghank12
04-24-2008, 09:20 PM
This is a snippet of my login.php file...
<?
include("connection.php");
$username = $_POST['username'];
$password = $_POST['password'];
$query = "SELECT * FROM users WHERE username='$username' AND password='$password'";
$result = mysql_query($query);
if(mysql_num_rows($result) != 1) {
echo "<font color=red><b>Wrong Credentials!</b></font><br>";
echo "<a href=forgot.php class=forgot>forgot your password?<br></a>";
die("<a href=index.php class=forgot>try again</a>");
} else {
$_SESSION['username'] = "$username";
}
?>
Thank you for logging, in, please visit the links below to get started!
<?
}
?>
As you see, I think I store the username into the session, but I don't know if I do it right.
Then at the top of every page, I have this..
<?
session_start();
$session = $_SESSION['username'];
include("connection.php");
?>
I tried echoing just $session but that didn't work either.
tinghank12
04-24-2008, 09:41 PM
Well, I've achieved in displaying the username by just echoing off the session data that I showed in my previous post.
Now, I want to take it a step farther and display the members name from the database.
So far I've got this, but it doesn't work, it says Hello,
<?
if($session) {
$name = mysql_query("SELECT 'name' FROM 'users' WHERE 'username' = '$session'");
echo "Hello, $name";
} else {
?>
Inigoesdr
04-24-2008, 09:47 PM
Please go back an read the third post in this thread. You aren't actually getting the result of the query, just executing it. Does $session contain the user's username? You also need to be sanitizing (http://php.net/mysql_real_escape_string) input for your queries.
tinghank12
04-24-2008, 10:05 PM
Yes but when I use mysql_result
I get this....
Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /home/www/****************/index.php on line 102
My code:
<?
$name = $_POST['username'];
if($session) {
$name = mysql_query("SELECT 'name' FROM 'users' WHERE 'username' = '$session'");
$result = mysql_result($name, 0, 'result');
echo "Hello, $result";
} else {
?>
Inigoesdr
04-24-2008, 10:11 PM
Try this:
$username = mysql_real_escape_string($_POST['username']);
if(!empty($username))
{
$result = mysql_query("SELECT `name` FROM `users` WHERE `username` = '$username'") or die(mysql_error());
if(mysql_num_rows($result) > 0)
{
$name = mysql_result($result, 0);
echo "Hello, $name";
}
}
You need to use backticks, not quotes around your columns/tables. Added error checking too, so if/when your query fails it will tell you why.
tinghank12
04-24-2008, 10:21 PM
Sweet it worked! Thanks for everybodies help!
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.