Jump to content

Recommended Posts

guys this is my table

 

--

-- Table structure for table `tbl_country`

--

 

CREATE TABLE IF NOT EXISTS `tbl_country` (

  `id` int(11) NOT NULL,

  `country_name` varchar(50) NOT NULL

) ENGINE=MyISAM DEFAULT CHARSET=latin1;

 

--

-- Dumping data for table `tbl_country`

--

 

INSERT INTO `tbl_country` (`id`, `country_name`) VALUES

(1, 'Afghanistan'),

(2, 'Albania'),

(3, 'Algeria'),

(4, 'American Samoa'),

(5, 'Andorra'),

(6, 'Angola'),

(7, 'Anguilla');

 

i am to fetch them with loop help please

 

<div id="logo_ads_from">Display Classifieds From:  
<select name="country">
<?php

//use php loop to print all county name in selectbox

?>
</select>
</div>

 

 

see i done like this but then i get blank page

 

<?php
//use php loop to print all county name in selectbox

$result = mysql_query("SELECT * FROM tbl_country") 
or die(mysql_error());  
while($row = mysql_fetch_array( $result )) {
echo "<select name='country'>";
echo "<option value='$row['id'];'>$row['country_name'];</option>";
}
echo "</select>";
?>

When echoing an array reference within double quotes, this will give an error:

<?php
echo "<option>$row['country_name'];</option>";
?>

You can do it one of these ways:

<?php
echo "<option>{$row['country_name']}</option>";
//
// or
//
echo "<option>" . $row['country_name'] . "</option>";
//
//  or
//
echo "<option>$row[country_name]</option>";
?>

 

But those still won't work correctly since you're not setting the value of each option. This will:

<?php
echo "<option value="{$row['country_name'])">{$row['country_name']}</option>";
?>

 

Ken

Okay, try:

 

echo "<select name=\"country\">";
$result = mysql_query("SELECT * FROM tbl_country");

while($row = mysql_fetch_assoc($result))
{
   echo "<option value=\"{$row['country_name']}\">{$row['country_name']}</option>";
}

echo "</select>";

 

Since you're trying to output an array value within a string (within quotes), you need to use {} around them.

done this still page turns blank

 

<?php

//use php loop to print all county name in selectbox
echo "<select name='country'>";
$result = mysql_query("SELECT * FROM tbl_country") 
or die(mysql_error());  
while($row = mysql_fetch_array( $result )) {
echo "<option value="{$row['id'])">{$row['country_name']}</option>";
}
echo "</select>";
?>

 

Okay, try:

 

echo "<select name=\"country\">";
$result = mysql_query("SELECT * FROM tbl_country");

while($row = mysql_fetch_assoc($result))
{
   echo "<option value=\"{$row['country_name']}\">{$row['country_name']}</option>";
}

echo "</select>";

 

Since you're trying to output an array value within a string (within quotes), you need to use {} around them.

 

works bro thanks soo much  :shrug:

Quotes were unescaped, and the closing curly brace was a parenthese . . .

echo "<option value=\"{$row['id']}\">{$row['country_name']}</option>";

true but then my teacher tells me not to use back slashes he insist me to use single quotes instead inside of double quotes

Okay, try:

 

echo "<select name=\"country\">";
$result = mysql_query("SELECT * FROM tbl_country");

while($row = mysql_fetch_assoc($result))
{
   echo "<option value=\"{$row['country_name']}\">{$row['country_name']}</option>";
}

echo "</select>";

 

Since you're trying to output an array value within a string (within quotes), you need to use {} around them.

 

bro can i not use while($row = mysql_fetch_array($result)) insteat of while($row = mysql_fetch_assoc($result))?

 

whats the difference between assoc and array? why did we have to use assoc but not array?

Yes, it does output the same results. The issue is efficiency. In your code above, you should be using mysql_fetch_row(), or possibly mysql_fetch_array( 'query string', MYSQL_NUM) to limit the results to an enumerated array, since that is how you're accessing it.

 

mysql_fetch_row(); returns an enumerated array.

mysql_fetch_assoc(); returns an associative array.

mysql_fetch_array(), when used without modifying parameters, returns both an associative array and an enumerated array, which has higher overhead, and is slower.

 

You should explicitly list the fields you need in the query string (SELECT `field1`, `field2` FROM . . . ) instead of wildcard selecting them (SELECT * FROM . . . ), unless, of course, you actually need all of the fields in the table.

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.