Hi everyone, I 'm having trouble when getting the value from the MySQL DB, databse as shown:
table name: animal
name |type |number
------------------------
John | cat | 5
John | dog | 6
John | fish | 3
Adam | bird | 4
I want to show all types(under John) from an array by:
$query = "SELECT * FROM `animal` WHERE `name`='John' ";
$result = mysql_query( $query) or die ("didn't query");
while ($row = mysql_fetch_array($result)) {
print $row[1];}
But I'm getting catdogfish, is there any way I can get it seperate so i can assign them in different variable? many thx
Getting values from MySQL array
Moderator: General Moderators
-
moodbuilder
- Forum Newbie
- Posts: 3
- Joined: Sat Jan 30, 2010 6:51 am
- social_experiment
- DevNet Master
- Posts: 2793
- Joined: Sun Feb 15, 2009 11:08 am
- Location: .za
Re: Getting values from MySQL array
Try :
Code: Select all
<?php
while ($row = mysql_fetch_array($result)) {
print $row['type']."<br />";
}
?>“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
-
moodbuilder
- Forum Newbie
- Posts: 3
- Joined: Sat Jan 30, 2010 6:51 am
Re: Getting values from MySQL array
It shows the results! thx but if I want to assign each each value to variable like
$type1 = cat;
$type2 = dog;
$type3 = fish;
I tried
$type1 = $row['type'][0];
$type2 = $row['type'][1];
$type3 = $row['type'][2];
but it doesn;t seem working, any thoughts? many thx again!
$type1 = cat;
$type2 = dog;
$type3 = fish;
I tried
$type1 = $row['type'][0];
$type2 = $row['type'][1];
$type3 = $row['type'][2];
but it doesn;t seem working, any thoughts? many thx again!
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: Getting values from MySQL array
Code: Select all
while ($row = mysql_fetch_array($result)) {
$types[] = $row['type'];
}mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
-
moodbuilder
- Forum Newbie
- Posts: 3
- Joined: Sat Jan 30, 2010 6:51 am
Re: Getting values from MySQL array
Thx, I tried:
$types[] = $row['type'];
print $types[0];
and I get:
//catcatcat
print $types[1];
//dogdog
print $types[2];
//fish
Don;t know why [0] and [1] it gives trible and double value? thx again!
$types[] = $row['type'];
print $types[0];
and I get:
//catcatcat
print $types[1];
//dogdog
print $types[2];
//fish
Don;t know why [0] and [1] it gives trible and double value? thx again!
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: Getting values from MySQL array
Code: Select all
while ($row = mysql_fetch_array($result)) {
$types[] = $row['type'];
}
echo $types[0].'<br>';
echo $types[1].'<br>';
echo $types[2].'<br>';
foreach($types as $type) {
echo $type.'<br>';
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.