Jump to content

Resource Id #4


Ads

Recommended Posts

I am trying to acess the Databse to get a username and echo it out..But for some reason it gives that error (See title :P) I have no idea what it means, Can any one help out :) i am pretty noobish at php so Forgive me :)

Link to comment
Share on other sites

<?php
include "include/db.php";

?>
<html>
<head>
<title>Apples xD </title>
    <link href="style1.css" rel="stylesheet" type="text/css">
</head>
<body>

<?php

  $getuser="SELECT * FROM players WHERE  id='6'";
  $getuser2=mysql_query($getuser) or die("Could not get user info");
  $getuser3=mysql_fetch_array($getuser2);
  
  echo $getuser2;

Link to comment
Share on other sites

try

echo $getuser3[0];

and see what happens

 

 

or you can replace 0 with the name of the username column in players

 

Cool thanx that works, Got another Question. I don;t accutly wont to Display the id Number, I want to Display the User name after the Person is logged in.

 

EXAMPLE:

Welcome: USERNAME GOES HERE :)

 

but it wont let me do it :( it just comes up blank

 

Heres the code:

 

  $username = $_POST['username'];
  $getuser="SELECT * FROM players WHERE  id='6'";
  $getuser2=mysql_query($getuser) or die("Could not get user info");
  $getuser3=mysql_fetch_array($getuser2);
  
  echo $getuser3[1];

 

I don;t want the Number 6 there, it is only there becasue i have no idea how to do it :):P

Link to comment
Share on other sites

echo $getuser3['username']

 

Hmm That works, but it doesn;t Display a different Username For a different..well username, if that makes sense.

 

If i login with a different user name it doesn;t Change it at all :(

Link to comment
Share on other sites

here's something to work with

  $username = $_POST['username']; //This line is only good if you have a form beforehand
  $getuser="SELECT username, email, firstname FROM players'";
  $getuser2=mysql_query($getuser) or die("Could not get user info");
  while($row = mysql_fetch_assoc($getuser2)) {
     echo $row['username'] . "
";
     echo $row['email'] . "
"
     echo $row['firstname'] . "
"
  }

 

Link to comment
Share on other sites

You have WHERE  id='6' hardcoded in, therefore it's always going to select the record for user #6

 

PhREEEk

 

I have taken out the Id 6 thing

 

NEW CODE so far still not working :P:(

  $username = $_POST['username'];
  $getuser="SELECT * FROM players";
  $getuser2=mysql_query($getuser) or die("Could not get user info");
  $getuser3=mysql_fetch_array($getuser2);
  
  echo $getuser3['username'];

Link to comment
Share on other sites

You need a criteria for which user you want to view the record for. Your newest code posted selects all player records, because there is no WHERE clause to specify a single record or group of records.

 

Do you want ALL the records, or just one, or what?

 

PhREEEk

 

I want the Username of the User. Hence they Jusy logged in Theere username displayed.

Link to comment
Share on other sites

Well, just insert the variable holding the username as the WHERE clause:

 

  $username = $_POST['username'];
  $getuser="SELECT * FROM players WHERE username = '$username'";
  $getuser2=mysql_query($getuser) or die("Could not get user info");
  $getuser3=mysql_fetch_array($getuser2);
  
  echo $getuser3['username'];

 

But, you already have that variable in the POST...

 

So,

 

echo "Welcome $username!";

 

should work just as easily...

 

PhREEEk

Link to comment
Share on other sites

You have $username = $_POST['username']; in your script, so we're assuming someone has submitted a username through a form. If so, $_POST['username'] would be populated, and then converted to $username, and then Welcome $username would work...

 

If nothing is posted, and $_POST['username'] is EMPTY, then Welcome $username! would just print Welcome !

 

PhREEEk

Link to comment
Share on other sites

Seems to be one Problem after another with me :(

 

Current Error:

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource

 

  $username = $_POST['username'];
  $sql = "SELECT username, FROM players WHERE username='$username'";
  $result = mysql_query($sql);

  while($row = mysql_fetch_assoc($result)) {  // Retrieve data until no more
    echo $row['username'], '<br />';

}

Link to comment
Share on other sites

Remove the comma after SELECT username

 

$sql = "SELECT username, FROM players WHERE username='$username'";

becomes

$sql = "SELECT username FROM players WHERE username='$username'";

 

Use commas to separate more than one field request, last field (the one before FROM) should not have a comma (otherwise MySQL assumes you want to retrieve the field 'from').

 

PhREEEk

Link to comment
Share on other sites

Cool got rid of the error..Now it is a blamk screen.

 

I dunno if this is Relevent or not, Proberly is, and if it is i am sorry :(

 

I have a login Page(login.php) Once the user has put in there Email and password, it links to another page (loginck.php) This chekcs if the email and password are correct, It get the email and password from the Login.php which posts them, if the email and password is correct loginck.php redirects the page to main.php. On Main.php is where i want it to display the username the player Registerd With.

 

But When the player Logs in the Username isn;t put into the field, they login with your email adress

Link to comment
Share on other sites

<?php
$username = $_POST['username'];
$sql = "SELECT username, FROM players WHERE username='$username'";
$result = mysql_query($sql)or die(mysql_error());

while($row = mysql_fetch_assoc($result)) {  // Retrieve data until no more
echo $row['username'], '<br />';
}
?>

 

this code makes no sence at all what you doing......

 

example

 

form with username just echo $username; dont even need the database lol.........

Link to comment
Share on other sites

<?php
$username = $_POST['username'];
$sql = "SELECT username, FROM players WHERE username='$username'";
$result = mysql_query($sql)or die(mysql_error());

while($row = mysql_fetch_assoc($result)) {  // Retrieve data until no more
echo $row['username'], '<br />';
}
?>

 

 

 

this code makes no sence at all what you doing......

 

example

 

form with username just echo $username; dont even need the database lol.........

 

I do need the Databse, Becasue i havent posted the username, as i just figured out, so i need to get the username from the Data base

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.