Ok so I am a rookie in php and working on populating drop down lists from a mysql database. I have a drop down list working that contains words, but my drop down list for my date refuses to work and I don't know what the difference between them should be. Working code

$querystyle="SELECT DISTINCT style FROM $table";
$styleresult=mysql_query($querystyle);

$numstyle=mysql_numrows($styleresult);
if(!$styleresult)
	die(mysql_error());
$i=0;
echo "<select name=style=''><option value = 1>Game Style</option>";

while ($i < $numstyle) {


$style=mysql_result($styleresult,$i,"style");

echo "<option value = '$style'>$style</option>";
$i++;
}
echo '</select';

Non-working code

$querydate="SELECT DISTINCT release FROM $table";
$dateresult = mysql_query ($querydate);

$numdate=mysql_numrows($dateresult);
if(!$dateresult)
	die(mysql_error());
$i=0;
echo "<select name=Release date=''>
<option value = 1>Release Date</option>";

while ($i < $numdate) {


$date=mysql_result($dateresult,$i,"release");

echo "<option value = '$date'>$date</option>";
$i++;
}
echo '</select';

Recommended Answers

All 10 Replies

Oh and these are the errors I am getting,

Warning: mysql_numrows(): supplied argument is not a valid MySQL result resource in /home/paragoul/public_html/upcoming/upcoming.php on line 25


You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'release FROM upcoming' at line 1

Your $querydate SQL syntax is probably wrong, I don't really know how your database is built so I can only assume.

try doing an echo on your $querydate and copying the syntax displayed on the browser and trying that syntax in MYSQL or a "query browser" if you have one.

try this

$querydate="SELECT DISTINCT release FROM $table";
$dateresult = mysql_query ($querydate);

$numdate=mysql_numrows($dateresult);
if(!$dateresult)
{
die(mysql_error());
}
$i=0;

/*echo "<select name=Release_date=''>
<option value = 1>Release Date</option>";*/

echo "<select name = 'Release date'>
<option value = 1>Release Date</option>";

//i don't really know what you're trying to display here. 
while ($i < $numdate) {

$date=mysql_result($dateresult,$i,"release");

echo "<option value = '$date'>$date</option>";
$i++;
}
echo '</select>';

Thanks for the attempt, tried it but still the same results, the database is built with phpmyadmin. It has to connect since I can create drop down lists on the same page from text types, but with phpmyadmin(not sure of other ways of working with databases) the dates are input into actual date types. Should the data type matter when trying to pull it from the database and input it into a variable?

From what i could tell, the data you are trying to retrieve from the database is a date type, if that's the case, the value from the database will display as a 'YYYY-MM-DD' format.

try this

$querydate="SELECT DISTINCT release FROM $table";
$dateresult = mysql_query ($querydate);

$numdate=mysql_numrows($dateresult);
if(!$dateresult)
{
die(mysql_error());
}
$i=0;

/*echo "<select name=Release_date=''>
<option value = 1>Release Date</option>";*/

echo "<select name = 'Release date'>
<option value = 1>Release Date</option>";

//i don't really know what you're trying to display here. 
/*
while ($i < $numdate) {

$date=mysql_result($dateresult,$i,"release");

echo "<option value = '$date'>$date</option>";
$i++;
}*/

//try this instead

while(row=mysql_fetch_array($dateresult, MYSQL_BOTH)){
echo "<option value = '".$row['release']."'>
          ".$row['release']."</option>";
}
echo '</select>';
Member Avatar for rajarajan2017
$dateresult = mysql_query($querydate) or die ("Error in query: $querydate. ".mysql_error());
$numdate=[B]mysql_num_rows[/B]($dateresult);

Check with the statement or else first echo your result of $querydate to check whether it is received or not

From what i could tell, the data you are trying to retrieve from the database is a date type, if that's the case, the value from the database will display as a 'YYYY-MM-DD' format.

try this

$querydate="SELECT DISTINCT release FROM $table";
$dateresult = mysql_query ($querydate);

$numdate=mysql_numrows($dateresult);
if(!$dateresult)
{
die(mysql_error());
}
$i=0;

/*echo "<select name=Release_date=''>
<option value = 1>Release Date</option>";*/

echo "<select name = 'Release date'>
<option value = 1>Release Date</option>";

//i don't really know what you're trying to display here. 
/*
while ($i < $numdate) {

$date=mysql_result($dateresult,$i,"release");

echo "<option value = '$date'>$date</option>";
$i++;
}*/

//try this instead

while(row=mysql_fetch_array($dateresult, MYSQL_BOTH)){
echo "<option value = '".$row['release']."'>
          ".$row['release']."</option>";
}
echo '</select>'; 

end quote.

Thanks for another great attempt, and yes the data I am trying to get is of date type and is in the format you described, but still the same results when I tried your suggestion. The problem lies more in

$querydate="SELECT DISTINCT release FROM $table";
$dateresult = mysql_query ($querydate);

$numdate=mysql_numrows($dateresult);
if(!$dateresult)
{
die(mysql_error());
}

then in the while loop. It never gets passed mysql_numrows() while using a date type. I have fought with this problem for about a month of sundays so I am just gonna do what I should of done a long time ago and just change it to a text type and put the date in my own format and hope it will work that way. Thanks for all the help people and I will let you know how it goes, I'm sure I will be on this site alot until I relearn the php I haven't used in 4 years.

I have fought with this problem for about a month of sundays so I am just gonna do what I should of done a long time ago and just change it to a text type and put the date in my own format and hope it will work that way.

This plan is still a failure... not sure why the code works for $style on the same page but fails for $date even if I only put 1 number as the date in text format in the database.

So after more trials of stuff I found out the problem lies somewhere in this code.

$querystyle="SELECT DISTINCT style FROM $table";
echo "$querystyle";
//works correctly

$styleresult=mysql_query($querystyle);
echo "$styleresult";
//works correctly

$numstyle=mysql_numrows($styleresult);
echo "$numstyle";
//works correctly

if(!$styleresult)
	die(mysql_error());
//works correctly

$querydate="SELECT DISTINCT release FROM $table";
echo "$querydate";
//works correctly

$dateresult=mysql_query($querydate);
echo "$dateresult";
//is blank

$numresult=mysql_numrows($dateresult);
echo "$numresult";
//fails because $dateresult is blank

I have checked multiple times to make sure release is spelled right, I have even changed the database row name and the name on the query to try a different word in case release was a word I wasn't allowed to use. Should there be a problem with doing multiple but seperate queries on the same page?

So after more trials of stuff I found out the problem lies somewhere in this code.

$querystyle="SELECT DISTINCT style FROM $table";
echo "$querystyle";
//works correctly

$styleresult=mysql_query($querystyle);
echo "$styleresult";
//works correctly

$numstyle=mysql_numrows($styleresult);
echo "$numstyle";
//works correctly

if(!$styleresult)
	die(mysql_error());
//works correctly

$querydate="SELECT DISTINCT release FROM $table";
echo "$querydate";
//works correctly

$dateresult=mysql_query($querydate);
echo "$dateresult";
//is blank

$numresult=mysql_numrows($dateresult);
echo "$numresult";
//fails because $dateresult is blank

Also I have changed the query to any of the other 3 fields, and it works with all of them and they were all of text type, but still doesn't work for the date even though I have now changed it to text type.

Ok, on a final note, thanks to everyone for their help, my final solution, deleted the date field, made a new one with the date type, and it works perfectly, so maybe that field was just bugged.. Hope to see you around peeps.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.