mysql am i doing something wrong ?

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
DTV
Forum Newbie
Posts: 3
Joined: Sat Feb 06, 2010 1:22 am

mysql am i doing something wrong ?

Post by DTV »

im trying to show info about the user on the page there id and stuff but for some resson it ent showing



Code: Select all

 
<?php
 
                          
mysql_connect("..", "_", "") or die(mysql_error());
mysql_select_db("_") or die(mysql_error());
 
                                                  
                                                  
$sql = ("SELECT username FROM users WHERE username = '$username'")or die(mysql_error());
$result = mysql_query($sql) or die(mysql_error());
$values = mysql_fetch_array($result);
print $values['username'];
 
 
?>
                          - Trainer #
  <?php                                           
                                                  
$sql = ("SELECT ID FROM users WHERE username = '$username'")or die(mysql_error());
$result = mysql_query($sql) or die(mysql_error());
$values = mysql_fetch_array($result);
print $values['ID'];
 
 
?>
                        </p>
                          <p class="style6">  </p>
                          <p>
                                <?php
         $sql = ("SELECT pokemon1_pic FROM users WHERE username = '$username'")or die(mysql_error());
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_array($result);?>
    </p>
                          <p align="center"><img src="<?php echo $row['pokemon1_pic'] ?>" width="94" height="93" /> </p>
                          <p><span class="style6">Pokemon:</span>
                                  <?php
                                                                                                 
$sql = ("SELECT pokemon FROM users WHERE username = '$username'")or die(mysql_error());
$result = mysql_query($sql) or die(mysql_error());
$values = mysql_fetch_array($result);
print $values['pokemon'];
 
 
?>
                          </p>
                          <p><span class="style6">Level:</span>
                                  <?php
                                                  
$sql = ("SELECT poke1lvl FROM users WHERE username = '$username'")or die(mysql_error());
$result = mysql_query($sql) or die(mysql_error());
$values = mysql_fetch_array($result);
print $values['poke1lvl'];
 
 
?>


it does not show anything none of them work im not getting any errors i am starting the session at top of the page two but still ent working


i am making the variable $username on the login page

Code: Select all

 
// if login is ok then we add a cookie
$_POST['username'] = stripslashes($_POST['username']);
$hour = time() + 3600;
setcookie(ID_my_site, $_POST['username'], $hour);
setcookie(Key_my_site, $_POST['pass'], $hour);
 
//if there is, it logs you in and directes you to the members page
{
$username = $_COOKIE['ID_my_site'];
$pass = $_COOKIE['Key_my_site'];
$check = mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error());
while($info = mysql_fetch_array( $check ))
{
if ($pass != $info['password'])
{
}
else
{
header("Location: members.php");



all the login works fine just showing the info dont and i dont know why ?
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: mysql am i doing something wrong ?

Post by social_experiment »

You have 3 seperate queries to display information about just one user. Try using the code below.

Code: Select all

<?php $query = "SELECT id, username, pokemon1_pic, poke1lvl FROM table WHERE username = '$username'";
$result = mysql_query($query) or die(mysql_error());
while ($pointer = mysql_fetch_array($result)) {
 echo $pointer['id']."\n";
 echo $pointer['username']."\n";
 echo $pointer['pokemon1_pic']."\n";
 echo $pointer['poke1lvl']."\n";
} ?>
I dont think there is something wrong with the other code but i'd assign the username slightly diffrently

Instead of using :

Code: Select all

<?php $_POST['username'] = stripslashes($_POST['username']); ?>
I would :

Code: Select all

<?php $username = stripslashes($_POST['username']); ?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
DTV
Forum Newbie
Posts: 3
Joined: Sat Feb 06, 2010 1:22 am

Re: mysql am i doing something wrong ?

Post by DTV »

social_experiment : I tryed putting your code in but i got an error i think i put it in the wrong place

Could u copy the first code and add
the new code u gave me please?
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: mysql am i doing something wrong ?

Post by social_experiment »

Hope this helps :)

Code: Select all

<?php
    $connect = mysql_query('host', 'username', 'password');
    mysql_select_db('db') or die(mysql_error());
    
    $sql = "SELECT ID, username, pokemon1_pic, poke1lvl, pokemon FROM users WHERE username = '$username'";
    $result = mysql_query($sql) or die(mysql_error());
    
    while ($value = mysql_fetch_array($result)) {
        echo "<p class=\"style6\">";        
        //will echo username
        echo $value['username'];
        echo "</p>";
        echo "<p class=\"style6\">";
        //will echo "Trainer #3" or whatever the ID value is
        echo "<p class=\"style6\">";
        echo "- Trainer #".$value['ID']."";
        echo '</p>';
        //will display the image
        echo "<p class=\"style6\">";
        echo "<img src=\"".$value['pokemon1_pic']."\" />";
        echo '</p>';
        //will echo Pokemon : pokemon or whatever the value is
        echo "<p class=\"style6\">";
        echo "Pokemon : ".$value['pokemon'];
        echo "</p>";
        //will echo the value of the poke1lvl
        echo "<p class=\"style6\">";
        echo "Level : ".$value['poke1lvl'];
        echo '</p>';
    }
?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
DTV
Forum Newbie
Posts: 3
Joined: Sat Feb 06, 2010 1:22 am

Re: mysql am i doing something wrong ?

Post by DTV »

Thx (:

but it didnt work :'(

PHP Error Message

Parse error: syntax error, unexpected T_DNUMBER in /home/a8310104/public_html/register.php on line 77

Sorry, im so noobish but im 13 and im trying to prove my teacher wrong he said that it would be a massive challenge to a yr 8 like you it would even challenge a yr 12 student and im like i can do it :)
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: mysql am i doing something wrong ?

Post by social_experiment »

Could you paste the code giving the error?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: mysql am i doing something wrong ?

Post by social_experiment »

There is a error in one of the lines of the script i gave you (this is not relevant to your current problem i think)

Code: Select all

<?php $connect = mysql_query('host', 'username', 'password'); ?>
should be :

Code: Select all

<?php $connect = mysql_connect('host', 'username', 'password'); ?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply