Jump to content

[SOLVED] adding username to image


runnerjp

Recommended Posts

  • Replies 128
  • Created
  • Last Reply

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

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

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

<?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.';
}
?>

 

$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

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.';

}

 

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" />';
      }
    }
  }

?>

I think his $_SESSION['user_id'] is the username, not the id field we all think it is...

 

runnerjp, everyone has given you everything you need to get this working.  You need to make some effort to connect the dots.

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

 

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.

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???

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!

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.