Page 1 of 1

I need information for WHERE in mysql_query.

Posted: Fri Aug 19, 2011 12:45 am
by wizzuriz
Hello all.

I like to hear if there is anyone that can help me out with my problem here.

I like to show the user_information on the protected.php page.
Now I can show the user information but only from line one in the database table, if I try to add the
where `user_name` = '{$user}'"); I just don´t know how to call the $user so it will match the user that is logged in.
<?php
include ('core/init.inc.php');

?>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
        <div>
            <p>
                hejsa du er logged ind som <?php echo $_SESSION['username']; ?>
            </p>
        </div>
        <div>
            <h1>Welcome         <?php echo $_SESSION['username']; ?></h1>
            <p>Username:        <?php echo $row['user_name']; ?></p>
                <p>First name:  <?php echo $row['user_firstname']; ?></p>
                <p>Last name:   <?php echo $row['user_lastname']; ?></p>
                <p>Weight:      <?php echo $row['user_weight']; ?></p>
                <p>Hight:       <?php echo $row['user_hight']; ?></p>
        </div>
        <?php
                echo 'log out ? <a href="logout.php">logout here</a>';
        ?>
    </body>
</html>
This is the code in the user_inc.php file

// this is not a function or class if I put it in a function I get a error on protected.php 
$query = "SELECT * FROM `users`"; 
	 
$result = mysql_query($query) or die(mysql_error());

$row = mysql_fetch_array($result) or die(mysql_error());

Please let me know if you know how to make this work.

Best regards
Wizzuriz.

Re: I need information for WHERE in mysql_query.

Posted: Sat Aug 20, 2011 12:17 pm
by jacek
The code in the user_inc.php does need to be in a function, then you can pass the username or id to that function and have it return the information you need.

Re: I need information for WHERE in mysql_query.

Posted: Sun Aug 21, 2011 12:18 am
by wizzuriz
Hey again.

My questing is where do I get the username from? $user?

How would I make the function?

please let me know.

best regards
wizzuriz

Re: I need information for WHERE in mysql_query.

Posted: Sun Aug 21, 2011 1:46 am
by jacek
You have the username in the session don't you, $_SESSION['username'] ?

Re: I need information for WHERE in mysql_query.

Posted: Wed Aug 24, 2011 9:39 pm
by wizzuriz
Hi Jacek,

First I like to say that I really appreciate your help.

Now after watching your video on user profile (2)

I Don't understand why my function don't work, I did check for syntax errors and there is non but I get a error of the information I pass in to the function is not valid.

ERROR:
Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in /Applications/XAMPP/xamppfiles/htdocs/user_system/core/inc/user.inc.php on line 144
$user_email = $_SESSION['email'];

//fetches all of the users from the table.

function fetch_users(){
    $result = mysql_query('SELECT `user_id` AS `id`,
                                  `user_email` AS `email`
                                  FROM `users`');
    $users = array();
    
    while (($row = mysql_fetch_assoc($result)) !== false){
        $users[] = $row;
    }
    return $users;
}
-------------------- fetch user function works just fine I see the list of all user from the table. ------------
// fetches profile information for the given user.
function get_user_information($user_email){
    $user_email = htmlentities($user_email);
    
    $sql = "SELECT
                `user_firstname` AS `firstname`,
                `user_lastname` AS `lastname`,
                `user_weight` AS weight`,
                `user_hight` AS `ight`
                FROM `users`
                WHERE `user_email` = {$user_email}";
    
    $result = mysql_query($sql);
    
    return mysql_fetch_assoc($result);
    
}

$user_information = get_user_information($user_email);

why is parameter 1 not a resource, how does it becomes a boolean is it because it don't see the $user_email? I can each it and it will show the email of the user that is logged in.

I really need some help here.

Best regards
Wizzuriz.

Re: I need information for WHERE in mysql_query.

Posted: Thu Aug 25, 2011 3:17 pm
by jacek
This happens when a query fails, mysql_query returns false (a boolean value) and not the result resource expected.

Add
echo mysql_error();
after the failing query and it will tell you what's wrong.

Re: I need information for WHERE in mysql_query.

Posted: Fri Aug 26, 2011 8:05 pm
by wizzuriz
This is my function that don´t work.
$u_email = $_SESSION['email'];
// $u_email I believe is the problem it don't work in the function but it works fine out of the function. 

function get_user_information($u_email){
    
// get the current users information from batabase. 
$result = mysql_query("SELECT * FROM users
 WHERE user_email='$u_email'") or die(mysql_error());  

$row = mysql_fetch_array( $result );
}
I just don't understand how it does not know the value of the §u_email, I do pass it in the information into the function in the parameter (). Should I use $global to pass it in?

If I remove the function line my information will display just fine but then its not a function.. ( what is the bad and good about function or not a function? I can still show the information.

I am getting so much new information each day, but still there is basic things I don't understand 100% yet.

best regards
wizzuriz.

Re: I need information for WHERE in mysql_query.

Posted: Sat Aug 27, 2011 1:10 pm
by jacek
wizzuriz wrote:Should I use $global to pass it in?
No that doesn't make any sense :?

Can you post the code where you call this function ?

Re: I need information for WHERE in mysql_query.

Posted: Sun Aug 28, 2011 3:21 pm
by wizzuriz
sure this is how I display it when it ant in the function

<td>
<?php echo $row['user_firstname']?>
</td>
<td>
<?php echo $row['user_weight']?>
</td>

this is the code

    $u_email = $_SESSION['email'];
    //function get_user_information($u_email){
     $result = mysql_query("SELECT * FROM users
WHERE user_email='$u_email'") or die(mysql_error());  
 
$row = mysql_fetch_array( $result );
//}
when not in the function it display the information just fine.

best regards
Wizzuriz.

Re: I need information for WHERE in mysql_query.

Posted: Mon Aug 29, 2011 10:35 am
by jacek
Okay, but where do you use the get_user_information function ?

Re: I need information for WHERE in mysql_query.

Posted: Mon Aug 29, 2011 11:32 am
by wizzuriz
I use it on the protected.php page.

if I don't make it a function it will display just fine..

Best regards
wizzuriz

Re: I need information for WHERE in mysql_query.

Posted: Tue Aug 30, 2011 11:18 am
by jacek
wizzuriz wrote:I use it on the protected.php page.
Then can you post how you are using it ?