why only first letter of column coming from mysql database in php?
my database is
CREATE TABLE `mytable` (
`id` int(10) AUTO_INCREMENT,
`name` varchar(50),
`description` varchar(255),
`visible` varchar(10),
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET utf8;
and php code is
$display = query("SELECT * FROM mytable ORDER BY id ASC");
foreach($display as $row) {
echo $row['id'];
echo $row['name'];
echo $row['description'];
开发者_如何学JAVA}
what is wrong in my code ? data is not displayed and when it display only first letter of field is displayed. All the configuration and connection settings are fine. Pls help
var_dump($row);
Looks like it's the same pitfall I fell into once :)
Don't you have only one row in your table?
If this query() function is kinda too smart one, determining return type by returned data, it can be a reason.
Make it return nested array, not one row.
And define result type explicitly, not automatically based on returned data. Add a parameter to indicate what kind of result you want.
However, such a function is very good approach. Only a few people have an idea of devising such a function instead of constant hassle with numerous API functions.
but if you expect just one row, then
$row = query("SELECT * FROM mytable ORDER BY id ASC");
echo $row['id'];
echo $row['name'];
echo $row['description'];
I had the this same problem using CodeIgniter.
The problem for me was that I was trying to echo out the array value using an associative key when it was just a numerically indexed array. I'm surprised it worked at all.
So my array was built like so:
$result = $this->ci->db->get('role');
if($result->num_rows() > 0)
{
foreach($result->result_array() as $row)
{
$roles[$row['id']] = $row['name'];
}
}
When I tried to use the array like so:
foreach($roles as $role)
{
echo $role['name'];
}
It would only print out the first letter of each value.
When I changed the array to be built like this:
$roles[$row['id']] = array('name' => $row['name']);
Everything worked as expected.
Hope this helps someone.
精彩评论