Selom
Selom

Reputation: 735

Mysql query returns only the first letter of strings

Can someone please tell me what I am missing. the following sql query returns only the first letter of strings it fetched from the my database:

 $result = mysql_query("SELECT * FROM users) or die("error in the query");

thanks.

Update


$result = mysql_query("SELECT * 
                         FROM properties 
                         JOIN users USING(UserId) 
                        WHERE Level <> 'Admin' $pages->limit") or die("could not load all the properties"); 

$i=0; 
while($row = mysql_fetch_array($result)) { 
  $fn[$i] = $row ['FN']; 
  $ln[$i] = $row ['LN']; 
  $phone[$i] = $row ['Phone']; 
  $Email[$i] = $row ['Email']; 
} 

...the part of the code that giving me head ache since yesterday. $fn, $ln, $email and$phone` all contain only one character. I think the error is on my own page but i just can't see it. anything concerning property contains the rite value

Upvotes: 3

Views: 6508

Answers (7)

Gary Hayes
Gary Hayes

Reputation: 1788

I just had this problem today. ( results only showing first letter of each variable ).

The situation was that my form data ( $_GET ) was using the same variable name as the array that was used later on after the query.

Once I changed the form data variable name, every thing was fine.

So, as a reminder to lazy people, when you paste old code into your page, double check variable names! :)

Upvotes: 0

CheeseConQueso
CheeseConQueso

Reputation: 6041

i had this similar problem once.... defining the arrays before assigning values to them helped me

try

$fn = array();

before going into the while loop

Upvotes: 0

Mike
Mike

Reputation: 151

I can only presume that somewhere in the code previous to this you have set up $ln, $ln, $phone and $Email to some default string value. Then, because you aren't changing the value of $i from 0 in your loop you are setting the first letter of these strings to be equal to the string value returned from mysql_fetch_array() which has the effect of only changing the first letter. Difficult to explain so allow me to provide an example:

$tmp = 'A';
$tmp[0] = 'It does not matter how long this string is!';
echo $tmp;

The result of that snippet is that the single character "I" gets echoed.

Your code will work as:

$fn = $ln = $phone = $Email = array();
$i=0; 
while($row = mysql_fetch_array($result)) { 
    $fn[$i] = $row ['FN']; 
    $ln[$i] = $row ['LN']; 
    $phone[$i] = $row ['Phone']; 
    $Email[$i] = $row ['Email']; 
    ++$i;
} 

Alternatively find where you have set the variables as strings.

Consider having an array of "user" objects because splitting up associated information like this is rarely useful.

e.g.

class User {
    public $first_name = '';
    public $last_name = '';
    public $phone_number = '';
    public $email = '';

    public function __construct($first, $last, $phone, $email) {
       $this->first_name = $first;
       $this->last_name = $last;
       $this->phone_number = $phone;
       $this->email = $email;
    }

    public function full_name() {
       return $first_name . ' ' . $last_name;
    }
}

while ($row = mysql_fetch_array($result)) { 
    $users[] = new User($row['FN'], $row['LN'], $row['Phone'], $row['Email']);
}

Objects, IMHO, shouldn't have public properties but this is for example purposes.

Upvotes: 1

David Thomas
David Thomas

Reputation: 253318

If you're using the while() to allocate variables to an array, I don't think you need to use the $i, if you change the code to:

while($row = mysql_fetch_array($result)) { 
  $fn[] = $row ['FN']; 
  $ln[] = $row ['LN']; 
  $phone[] = $row ['Phone']; 
  $Email[] = $row ['Email']; 
}

The $row['whatever'] variable should be appended to the array. This was posted as an answer because it's too long to post as a comment (at least legibly); I don't, however, think it's affecting your problem.

It is, though, worth trying:

`echo "<pre>" . print_r(get_defined_vars(),true) . "</pre>";

to see what the contents of the variables are, and whether they're being returned from the db as one-character strings, or if it's happening in the code somewhere.

Upvotes: 3

Steven
Steven

Reputation: 19425

You need to fetch the array which is returned. Add something like this:

$row = mysql_fetch_array($result);
echo $row[0];

Upvotes: 0

Sergej Andrejev
Sergej Andrejev

Reputation: 9413

Maybe your foreach loop is incorrect. SQL looks good. Maybe you are doing something wrong later

Upvotes: 0

David Hedlund
David Hedlund

Reputation: 129792

Either:

a. the fields in your database are declared with a one-character length, so that the data is actually being truncated

b. you're getting the right data, but something in the code displaying the data (which you haven't posted) is wrong, leading up to this result

Upvotes: 1

Related Questions