Jump to content

[SOLVED] adding username to image


runnerjp

Recommended Posts

  • Replies 128
  • Created
  • Last Reply

Top Posters In This Topic

would this work>>

 

<?$sql = 'SELECT `ext` FROM `user_image` WHERE `user_id`=' $_SESSION['user_id'];
$q = mysql_query($sql);
if($q){
  $row = mysql_fetch_array($q);
  if(!empty($row)){
    echo '<img src="http://www.yourdomain.com/members/images/'
         $_SESSION['user_id'] '.' . $row['ext']
         . '" />';
  }
}else{
  echo 'Error: Image not found.';
}
?>

 

tried it but getting syntax error, unexpected T_VARIABLE one 1st line

Link to comment
Share on other sites

would this work>>

 

<?$sql = 'SELECT `ext` FROM `user_image` WHERE `user_id`=' $_SESSION['user_id'];
$q = mysql_query($sql);
if($q){
  $row = mysql_fetch_array($q);
  if(!empty($row)){
    echo '<img src="http://www.yourdomain.com/members/images/'
         $_SESSION['user_id'] '.' . $row['ext']
         . '" />';
  }
}else{
  echo 'Error: Image not found.';
}
?>

 

tried it but getting syntax error, unexpected T_VARIABLE one 1st line

 

Use this, instead. It has no syntax errors:

 

<?php 

$sql = "SELECT `ext` FROM `user_image` WHERE `user_id`=".$_SESSION['user_id'];
$q = mysql_query($sql);
if($q){
  $row = mysql_fetch_array($q);
  if(!empty($row)){
    echo "<img src='http://www.yourdomain.com/members/images/'".
         $_SESSION['user_id'] . '.' . $row['ext']
         . " />";
  }
}else{
  echo 'Error: Image not found.';
}
?>

 

Sam

Link to comment
Share on other sites

Sorry, that's because we had an extra ' in the code

 

<?php 

$sql = "SELECT `ext` FROM `user_image` WHERE `user_id`=".$_SESSION['user_id'];
$q = mysql_query($sql);
if($q){
  $row = mysql_fetch_array($q);
  if(!empty($row)){
    echo "<img src='http://www.yourdomain.com/members/images/".
         $_SESSION['user_id'] . "." . $row['ext']
         . "' />";
  }
}else{
  echo 'Error: Image not found.';
}
?>

 

That ought to work now

 

Sam

Link to comment
Share on other sites

<?php 

$sql = "SELECT `ext` FROM `user_image` WHERE `user_id`=".$_SESSION['user_id'];
$q = mysql_query($sql) or die("Error running query:".mysql_error());
if($q){
  $row = mysql_fetch_array($q);
  if(!empty($row)){
    echo "<img src='http://www.yourdomain.com/members/images/".
         $_SESSION['user_id'] . "." . $row['ext']
         . "' />";
  }
}else{
  echo 'Error: Image not found.';

?>

 

Try it with the 'mysql_error' part on the query. Also with the first if statement it's just if($q)... if $q is what? set, empty etc..?

 

The second if statement is if(!empty($row)), which is like a double negative in English, if $row is not not full.. :P So you might want to try

 

if(isset($row)){ }

 

If that doesn't work you might want a while loop.. Something along the lines of

 

<?php 

$sql = "SELECT `ext` FROM `user_image` WHERE `user_id`=".$_SESSION['user_id'];
$q = mysql_query($sql);
if(isset($q)){
while ($row = mysql_fetch_array($q)) {
  
    echo "<img src='http://www.yourdomain.com/members/images/".
         $_SESSION['user_id'] . "." . $row['ext']
         . "' />";
  }
}
else{
  echo 'Error: Image not found.';
}
?>

 

Link to comment
Share on other sites

$sql = "SELECT `ext` FROM `user_image` WHERE `user_id`=".$_SESSION['user_id'];

$q = mysql_query($sql) or die("Error running query:".mysql_error());

if($q){

  $row = mysql_fetch_array($q);

  if(!empty($row)){

    echo "<img src='http://www.yourdomain.com/members/images/".

        $_SESSION['user_id'] . "." . $row['ext']

        . "' />";

  }

}else{

  echo 'Error: Image not found.';

 

gimmies nexpected $end

Link to comment
Share on other sites

Unexpected end means that you forgot to close a statement.  In this case, you didn't close the if statement. 

 

$sql = "SELECT `ext` FROM `user_image` WHERE `user_id`=".$_SESSION['user_id'];

$q = mysql_query($sql) or die("Error running query:".mysql_error());

if($q){

  $row = mysql_fetch_array($q);

  if(!empty($row)){

    echo "<img src='http://www.yourdomain.com/members/images/".

        $_SESSION['user_id'] . "." . $row['ext']

        . "' />";

  }

}else{

  echo 'Error: Image not found.';

}

 

Link to comment
Share on other sites

I don't understand why your querying the database at all, seems a waste of resources.

 

<?php

  if (isset($_SESSION['user_id'])) {
    foreach(array('jpg','gif','png') as $ext) { // add valid file extensions here.
      if (file_exists('members/images/' . $_SESSION['user_id'] . '.' . $ext)) {
        echo '<img src="members/images/{$_SESSION['user_id']}.$ext" />';
      }
    }
  }

?>

Link to comment
Share on other sites

if the url was www.runningprofiles.com/members/user/index.php?username=helraizer (website doesn't actually exist)

 

$usrname = $_GET['username'] - the username would be helraizer. For instance.

 

Then query the database SELECT * FROM `table` WHERE `username`= $username  (or similar)

then use a while loop, like in the last example I gave.

 

Use that to find the id associated with that username.

 

Sam

 

Link to comment
Share on other sites

question ...  can i get the username from whats typed in the url 

 

Yes, and I have previousy provided you with code (for your profile system) that does just that. As I said when I gave you that example, learn from it. Its no good simply having code handed to you unless you understand it. If you understood the code I provided you with over a week ago, you wouldn't be asking this same question.

Link to comment
Share on other sites

i am currently using

$query = "SELECT * FROM users WHERE Username = '$username' LIMIT 1"; 
  if ($result = mysql_query($query)) {
    if (mysql_num_rows($result)) { 
      $array = mysql_fetch_assoc($result);
      $pemail = $array['Email'];
      $puser = $array['Username'];
    } else {
      echo "No users found with id $id<br />";
    }
  } else {
    echo "Query failed<br />$sql<br />" . mysql_error();
  }

 

could i not just array the id???

Link to comment
Share on other sites

OK BEEN MESISNG AROUND WITH IT AND I HAVE NEARLY GOT IT

 

$sql = 'SELECT `ext` FROM `user_image` WHERE `user_id`=' $pID;
$q = mysql_query($sql);
if($q){
  $row = mysql_fetch_array($q);
  if(!empty($row)){
    echo '<img src="http://www.runningprofiles.com/members/images/'
         "$pID" '.' . $row['ext']
         . '" />';
  }
}else{
  echo 'Error: Image not found.';
}

 

BUT THE CODE DOESNT WORK DO TO unexpected T_VARIABLES

 

$pID gets the users id!

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.