$row=mysql_fetch_row

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

bionicdonkey
Forum Contributor
Posts: 132
Joined: Fri Jan 31, 2003 2:28 am
Location: Sydney, Australia
Contact:

Post by bionicdonkey »

this is a select statement i use and it works...

Code: Select all

<?php
<select name="catID">
					<option value="" disabled selected>Select Category</option>
					<option value="" disabled>---------------</option><?
					$result = mysql_query("SELECT * FROM category", $db);
					while($row = mysql_fetch_array($result)) {?>
					<option value="<? echo $row['catID']; ?>"><? echo $row['catName']; ?></option>
					<? } ?>
		  		</select>

?>
maybe u need th change

while ($row = mysql_fetch_row($result)) {

to

while ($row = mysql_fetch_array($result)) {
meandrew
Forum Commoner
Posts: 49
Joined: Mon Feb 24, 2003 1:03 pm

Post by meandrew »

I tried:

<select name="Category">
<option value="" disabled selected>Select Category</option>
<option value="" disabled>---------------</option><?
$result = mysql_query("SELECT * FROM category", $db);
while($row = mysql_fetch_array($result)) {?>
<option value="<? echo $row['CategoryName']; ?>"><? echo $row['Category']; ?></option>
<? } ?>
</select>

and it didn't work also I have changed the:

<?

echo "<select name=\"Category\" size=\"1\" class='menuForm'>";

$result=mysql_query("$DBName","SELECT Category, CategoryID FROM category ORDER BY Category");
while ($row = mysql_fetch_array($result)) {
echo "<option value=\"$row[1]\"> $row[0] </option>";
}
echo "</select>";
?>

so it is array and this isbn't working either.

Is this question becoming confusing?

Andrew
bionicdonkey
Forum Contributor
Posts: 132
Joined: Fri Jan 31, 2003 2:28 am
Location: Sydney, Australia
Contact:

Post by bionicdonkey »

this means there is something wrong with the db querys. add or die(mysql_error()); after all the database statements. a common error with queries is using '' in the right places...make sure you only use these with fields with non INT data types
bionicdonkey
Forum Contributor
Posts: 132
Joined: Fri Jan 31, 2003 2:28 am
Location: Sydney, Australia
Contact:

Post by bionicdonkey »

have you selected the db??....mysql_select_db("dbname");
meandrew
Forum Commoner
Posts: 49
Joined: Mon Feb 24, 2003 1:03 pm

mysql_query DB

Post by meandrew »

This is the form code: $DBName is selceted and is set in the require("connection.php"); along with the other $DBHost","$DBUser","$DBPass:

The form:

<table width="146" border="0" cellpadding ="0" cellspacing="0">

<tr class="navTable"><td class="bgColorDark" height="18"><span class="Header">

Search</span></td></tr><tr><td>


<form action='/companies.php' method='POST' name='CompanySearch'>
<input type='hidden' name='Search' value='YES' />
<input type='hidden' name='CompanySearch' value='Simple' />
<input type='text' class='menuForm' name='ANY' value='Keywords'
maxlength='40' size='13' />

<?
require("connection.php");

mysql_connect("$DBHost","$DBUser","$DBPass");

echo "<select name=\"CityID\" size=\"1\" class='menuForm'>";

$result=mysql_query("$DBName","SELECT City, CityID FROM city ORDER BY City");
while ($row = mysql_fetch_row($result)) {
echo "<option value=\"$row[1]\"> $row[0] </option>";
}
echo "</select>";

?>


<?

echo "<select name=\"Category\" size=\"1\" class='menuForm'>";

$result=mysql_query("$DBName","SELECT Category, CategoryID FROM category ORDER BY Category");
while ($row = mysql_fetch_array($result)) {
echo "<option value=\"$row[1]\"> $row[0] </option>";
}
echo "</select>";
?>

</td>
</tr>
<tr class="navTable">
<td class="bgColorMid">
<table cellpadding="0" cellspacing="0" border="0" width="100%"><tr><td valign="bottom"></td>
<td align="right">
<a href="companies.php" class="submitButton">Search</a>
<a href="companies.php" class="submitButton">
<img src="search.gif" border="0" align="absmiddle" vspace="1" hspace="1"></a>
</tr></table>
</td>
</tr>
</form>
</table>
meandrew
Forum Commoner
Posts: 49
Joined: Mon Feb 24, 2003 1:03 pm

Post by meandrew »

just to add to this form part of my question. The select is (was) working from the index.php it is the displying of results.

I am going to post all the code so if any of you guys can spot where I have gone wrond it will really help me :)

index.php<?
require ("header.php");
?>

<div id="diagonal">
<a href="index.php">
<img src="images/bb1.gif" width="70" height="69" alt="Some Meaning">
</a>
</div>


<TABLE>
<TR>
<TD align="left" valign="top" class="leftfill">
<div class="image">

<!-- Include Left Menu Options Here -->


<table width="146" border="0" cellpadding ="0" cellspacing="0">

<tr class="navTable"><td class="bgColorDark" height="18"><span class="Header">

Search</span></td></tr><tr><td>


<form action='/companies.php' method='POST' name='CompanySearch'>
<input type='hidden' name='Search' value='YES' />
<input type='hidden' name='CompanySearch' value='Simple' />
<input type='text' class='menuForm' name='ANY' value='Keywords'
maxlength='40' size='13' />

<?
require("connection.php");

mysql_connect("$DBHost","$DBUser","$DBPass");

echo "<select name=\"CityID\" size=\"1\" class='menuForm'>";

$result=mysql_query("$DBName","SELECT City, CityID FROM city ORDER BY City");
while ($row = mysql_fetch_row($result)) {
echo "<option value=\"$row[1]\"> $row[0] </option>";
}
echo "</select>";

?>


<?

echo "<select name=\"Category\" size=\"1\" class='menuForm'>";

$result=mysql_query("$DBName","SELECT Category, CategoryID FROM category ORDER BY Category");
while ($row = mysql_fetch_array($result)) {
echo "<option value=\"$row[1]\"> $row[0] </option>";
}
echo "</select>";
?>

</td>
</tr>
<tr class="navTable">
<td class="bgColorMid">
<table cellpadding="0" cellspacing="0" border="0" width="100%"><tr><td valign="bottom"></td>
<td align="right">
<a href="companies.php" class="submitButton">Search</a>
<a href="companies.php" class="submitButton">
<img src="search.gif" border="0" align="absmiddle" vspace="1" hspace="1"></a>
</tr></table>
</td>
</tr>
</form>
</table>


<?

$result=mysql("$DBName","SELECT City, CityID FROM city ORDER BY City");

echo "Select a City:<br><br>";
echo "<ul>";
while ($row = mysql_fetch_row($result)) {
$City=$row[0];
$CityID=$row[1];
echo "<li><a href='$Relative/$City/index.php?CI=$CityID'>$City</a></li>";
}
echo "</ul>";

?>

</div>

</TD>
<!--END OF LEFT HAND SIDE COLUMN -->


<!--BEGINNING OF MIDDLE COLUMN -->
<TD align="left" valign="top" class="grayfill">


<!-- BEGINNING MIDDLE TABLE -->

<div class="subheader">&nbsp;Punters Power they're your rights...
</div>

<div class="frontPageContentBox">

<div class="textNormal">

Perhaps the most important aspect of the way we live is the quality of life. The quality of our lives can be influenced by complex factors and simple ones too.

<br>

For instance a Shop Assistant by being courteous can put a smile on your face and make you feel good for the rest of the day.</div>
<br />
</div>


<!--STARTS MAIN MIDDLE CONTENT PAGE STARTS STARTS HERE-->








<!--ENDS MAIN MIDDLE CONTENT PAGE ENDS ENDS HERE-->
<!-- Middle TD ends here -->
</TD>


<TD align="left" valign="top" class="rightfill">

<!-- BEGINNING RIGHT HAND SIDE TABLE WITH LINKS IN AND STYLE iT CSS -->

<span class="subheader">&nbsp;

</span>

<div class="atext">

</div>

<br clear="ALL">


<!-- END RIGHT HAND SIDE TABLE WITH LINKS IN AND STYLE iT CSS :) -->
<div class="ffheader">
<b>Useful Links</b>
</div>

<div class="resource"><a href="/#.php"></a></DIV>
<div class="resource"><a href="/#.php"></a></DIV>


</TD>
</TR>
</TABLE>


<?
require ("footer.php");
?>


companies.php

<?
require ("header.php");
?>

<div id="diagonal">
<a href="index.php">
<img src="images/bb1.gif" width="60" height="60" alt="Some Meaning">
</a>
</div>


<TABLE>
<TR>
<TD align="left" valign="top" class="leftfill">
<div class="image">

<?
require("Cart.php");
mysql_connect("$DBHost","$DBUser","$DBPass");

$result=mysql("$DBName","SELECT Category, CategoryID FROM category ORDER BY Category");

echo "<ul>";
while ($row = mysql_fetch_row($result)) {
$Cat=$row[0];
$CatID=$row[1];
echo"<li><a href='$Relative/items.php?CA=$CatID'>$Cat</a></li>";
}
echo "</ul>";

?>

</div>

</TD>
<!--END OF LEFT HAND SIDE COLUMN -->


<!--BEGINNING OF MIDDLE COLUMN -->
<TD align="left" valign="top" class="grayfill">


<!-- BEGINNING MIDDLE TABLE -->

<?
mysql_connect("$DBHost","$DBUser","$DBPass");

$result=mysql_query("SELECT items.ItemSKU, items.ItemName,
items.ItemDescription, items.PostCode, items.Category, items.CityID, items.CTelephone, items.ItemID FROM items WHERE Category='$CA' ORDER BY itemName");
if (!$result) echo mysql_error();
else {
}
while ($row=mysql_fetch_row($result)) {
$IS=$row['ItemSKU'];
$IN=$row['ItemName'];
$ID=$row['ItemDescription'];
$IC=$row['PostCode'];
$Ca=$row['Category'];
$City=$row['CityID'];
$SC=$row['CTelephone'];
$II=$row['ItemID'];

//echo "<b>Here is a description of $IN...<br><br>";

echo "<div class='subheader'>&nbsp;$IN ";
echo "</div>";

echo "<div class='frontPageContentBox'>";

echo "<TABLE cellpadding=5 cellspacing=3 width=\"100%\" border=2>";
echo "<TR>";

/* Image will be here */
echo "<TD valign=\"top\" align=\"left\" width='30' class=\"grayborder\">";
echo "<img src=\"$Relative/images/s".$II."\">";
echo "</TD>";
/* Image will END here */


echo "<TD valign=\"top\" align=\"left\" class=\"company\">";
echo "<B></B><BR>";

echo "$IC<BR>";
echo "t: $SC<BR>";
echo "f: $CFax<BR>";
echo "e: $CompanyEmail<BR>";
echo "w: $WebAddress";



echo "</TD>";

echo "<TD valign=\"top\" align=\"left\" class=\"companydesc\">";
echo "<BR>";
echo "<BR>";
echo "<A href=\"show_image.php?II=$II&CA=$CA\">more info ...</A>";
echo "</TD>";


echo "</TR>";
echo "</TABLE>";
echo "<BR>\n\n";



echo "</div>";

}

print_r($CityID);
?>

<!-- Middle TD ends here -->
</TD>


<TD align="left" valign="top" class="rightfill">

<!-- BEGINNING RIGHT HAND SIDE TABLE WITH LINKS IN AND STYLE iT CSS -->

<span class="subheader">&nbsp;

</span>


<br><br>

<div class="atext">




</div>

<br clear="ALL">



<br>

<!-- END RIGHT HAND SIDE TABLE WITH LINKS IN AND STYLE iT CSS :) -->


<br>

<div class="ffheader">

<b>Useful Links</b>

</div>


</TD>
</TR>
</TABLE>


<?
require ("footer.php");
?>
gennusa
Forum Newbie
Posts: 9
Joined: Thu Feb 20, 2003 4:27 pm
Location: Delaware, USA
Contact:

Re: mysql_query

Post by gennusa »

meandrew wrote:I now have this:

$result=mysql_query("SELECT items.ItemSKU, items.ItemName,
items.ItemDescription, items.PostCode, items.Category, items.CityID, items.CTelephone, items.ItemID FROM items WHERE Category='$CA' ORDER BY itemName");
if (!$result) echo mysql_error();
else {
}
while ($row=mysql_fetch_row($result)) {
$IS=$row['ItemSKU'];
$IN=$row['ItemName'];
$ID=$row['ItemDescription'];
$IC=$row['PostCode'];
$Ca=$row['Category'];
$City=$row['CityID'];
$SC=$row['CTelephone'];
$II=$row['ItemID'];
I believe that this block of code should be changed to the following:

$result=mysql_query("SELECT items.ItemSKU, items.ItemName,
items.ItemDescription, items.PostCode, items.Category, items.CityID, items.CTelephone, items.ItemID FROM items WHERE Category='$CA' ORDER BY itemName");
if (!$result)
{ echo mysql_error();
}
else {
while ($row=mysql_fetch_row($result)) {
$IS=$row['ItemSKU'];
$IN=$row['ItemName'];
$ID=$row['ItemDescription'];
$IC=$row['PostCode'];
$Ca=$row['Category'];
$City=$row['CityID'];
$SC=$row['CTelephone'];
$II=$row['ItemID'];
}

Maybe I'm wrong but I always thought you had to put If/Else blocks in the curly braces like this.

Another thing here is that you have it where it should display an error if there are no results. If it returns a result, but that result is simply no information, then it is working correctly, but you need to put information into the table that it is drawing from. You may have some empty fields that it is finding.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

I started reformatting your code to get a glimpse of what it is doing...

Code: Select all

<?php require ("header.php"); ?>

<div id="diagonal">
	<a href="index.php">
		<img src="images/bb1.gif" width="70" height="69" alt="Some Meaning" />
	</a>
</div>

<TABLE>
	<TR>
		<TD align="left" valign="top" class="leftfill">
			<div class="image">
				<!-- Include Left Menu Options Here -->
				<table width="146" border="0" cellpadding ="0" cellspacing="0">
					<tr class="navTable"><td class="bgColorDark" height="18">
						<span class="Header">Search</span>
					</td></tr><tr><td>
...
as you can see there are several structural html-errors in your document. Probably they do not cause too much trouble but you might consider fixing them.
Using some indents and the

Code: Select all

-tag when posting here makes it much easier to read other peoples code.

http://validator.w3.org/
meandrew
Forum Commoner
Posts: 49
Joined: Mon Feb 24, 2003 1:03 pm

Post by meandrew »

Changes are beiong made to the html, thank you highlightling that.

Have you anything to add for the problem and question I asked?

Andrew
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Have to admit I stopped at </td></tr><tr><td> :wink:
There's maybe something else wrong but you should take a look at the manual page ofThere's no need to quote variables to use them (only if you want to assure it's a string for some reason). Using double quotes enables the variable-substitution in a string literal; if there's no need for that you may use single quotes. Then you can use double-quotes within the literal without escaping them, e.g. echo 'value="123"';

Code: Select all

<?php
require("connection.php");
$dbConn = mysql_connect($DBHost, $DBUser, $DBPass) or die(mysql_error());
mysql_select_db($DBName, $dbConn) or die(mysql_error());
echo '<select name="CityID" size="1" class="menuForm">';
$result=mysql_query('SELECT City, CityID FROM city ORDER BY City', $dbConn) or die(mysql_error());
while ($row = mysql_fetch_row($result))
	echo "<option value="$row[1]"> $row[0] </option>";
echo '</select>';
?>
meandrew
Forum Commoner
Posts: 49
Joined: Mon Feb 24, 2003 1:03 pm

Post by meandrew »

I had better bring you guys up to speed with this one :)

I am getting the drop down contents but there is no recosets showing from the selcted drop down query :( but the query in the result page does pull records from the DB albieit all the records so the query is not working :(

Andrew
bionicdonkey
Forum Contributor
Posts: 132
Joined: Fri Jan 31, 2003 2:28 am
Location: Sydney, Australia
Contact:

Post by bionicdonkey »

Code: Select all

<?
require("connection.php"); 
require("header.php"); 
?> 

<div id="diagonal"> 
	<a href="index.php"><img src="images/bb1.gif" width="70" height="69" alt="Some Meaning"></a> 
</div> 

<table>
	<tr>
		<td width="20%">
			<form action="/companies.php" method="post" name="CompanySearch">
			<table> 
				<tr class="navTable">
					<td class="bgColorDark" height="18">
						<span class="Header">Search</span>
					</td>
				</tr>
				<tr>
					<td> 
						<input type="hidden" name="Search" value="YES"> 
						<input type="hidden" name="CompanySearch" value="Simple"> 
						<input type="text" class="menuForm" name="ANY" value="Keywords" maxlength="40" size="13"> 
						<select name="CityID" size="1" class="menuForm"><?
						$result = mysql_query("SELECT City, CityID FROM city ORDER BY City") or die(mysql_error()); 
							while ($row = mysql_fetch_array($result)) { 
								echo "<option value="$row[1]"> $row[0] </option>"; 
							} ?> 
						</select>
						<select name="Category" size="1" class="menuForm"><? 
							$result = mysql_query("SELECT Category, CategoryID FROM category ORDER BY Category") or die(mysql_error()); 
							while ($row = mysql_fetch_array($result)) { 
								echo "<option value="$row[1]"> $row[0] </option>"; 
							} ?>
						</select>
					</td>
				</tr>
				<tr class="navTable"> 
					<td class="bgColorMid"> 
						<input type="submit" value="submit" name="Submit">
					</td> 
				</tr>
			</form>
				</tr>
				<tr>
					<td>
						<? 
						$result = mysql_query("SELECT City, CityID FROM city ORDER BY City") 0r die(mysql_error()); 
						echo "Select a City:<br><br>"; 
						echo "<ul>"; 
						while ($row = mysql_fetch_array($result)) { 
							$City = $row[0]; 
							$CityID = $row[1]; 
							echo "<li><a href='$Relative/$City/index.php?CI=$CityID'>$City</a></li>"; 
						} 
						echo "</ul>"; 
						?>
					</td>
				</tr>
			</table>
		</td>

<!--END OF LEFT HAND SIDE COLUMN --> 


<!--BEGINNING OF MIDDLE COLUMN -->
		<td width="60%">
			<table>
				<tr>
					<td align="left" valign="top" class="grayfill"> 
<!-- BEGINNING MIDDLE TABLE --> 
						<div class="subheader">
							Punters Power they're your rights... 
						</div> 
						<div class="frontPageContentBox">
							Perhaps the most important aspect of the way we live is the quality of life. The quality of our lives can be influenced by complex factors and simple ones too. 
							<br> 
							For instance a Shop Assistant by being courteous can put a smile on your face and make you feel good for the rest of the day.</div> 
							<br> 
						</div>
<!--STARTS MAIN MIDDLE CONTENT PAGE STARTS STARTS HERE--> 

<!--ENDS MAIN MIDDLE CONTENT PAGE ENDS ENDS HERE--> 
<!-- Middle TD ends here --> 
					</td> 
					<td align="left" valign="top" class="rightfill"> 
<!-- BEGINNING RIGHT HAND SIDE TABLE WITH LINKS IN AND STYLE iT CSS --> 
<!--						<span class="subheader">  
						</span> 

						<div class="atext"> 
						</div> 

						<br clear="ALL"> 
-->
					</td>
				</tr>
			</table>
		</td>
		<td width="20%">
			<table>
				<tr>
					<td>
<!-- END RIGHT HAND SIDE TABLE WITH LINKS IN AND STYLE iT CSS  --> 
						<div class="ffheader"> 
							<b>Useful Links</b> 
						</div>
						<div class="resource">
							<a href="/#.php">&nbsp;</a>
						</div> 
						<div class="resource">
							<a href="/#.php">&nbsp;</a>
						</div> 
					</td> 
				</tr> 
			</table> 
		</td>
	</tr>
</table>

<? 
require ("footer.php"); 
?>
Last edited by bionicdonkey on Tue Feb 25, 2003 5:01 pm, edited 1 time in total.
bionicdonkey
Forum Contributor
Posts: 132
Joined: Fri Jan 31, 2003 2:28 am
Location: Sydney, Australia
Contact:

Post by bionicdonkey »

Code: Select all

<? 
require ("header.php");
require("Cart.php");
require("connection.php"); 
?> 

<div id="diagonal"> 
	<a href="index.php"> 
		<img src="images/bb1.gif" width="60" height="60" alt="Some Meaning"> 
	</a> 
</div>
<table>
	<tr>
		<td> 
			<table> 
				<tr> 
					<td align="left" valign="top" class="leftfill"> 
						<div class="image">
							<? 
							$result = mysql_query("SELECT Category, CategoryID FROM category ORDER BY Category") or die(mysql_error()); 
							echo "<ul>"; 
							while ($row = mysql_fetch_array($result)) { 
								$Cat = $row[0]; 
								$CatID = $row[1]; 
								echo"<li><a href='$Relative/items.php?CA=$CatID'>$Cat</a></li>"; 
							} 
							echo "</ul>"; 
							?> 
						</div> 
					</td>
				</tr>
			</table>
		</td> 
<!--END OF LEFT HAND SIDE COLUMN --> 


<!--BEGINNING OF MIDDLE COLUMN --> 
		<td align="left" valign="top" class="grayfill"> 
			<? 
			$result = mysql_query("SELECT items.ItemSKU, items.ItemName, items.ItemDescription, items.PostCode, items.Category, items.CityID, items.CTelephone, items.ItemID FROM items WHERE Category='$CA' ORDER BY itemName") or die(mysql_error()); 
			while ($row=mysql_fetch_row($result)) { 
				$IS = $row['ItemSKU']; 
				$IN = $row['ItemName']; 
				$ID = $row['ItemDescription']; 
				$IC = $row['PostCode']; 
				$Ca = $row['Category']; 
				$City = $row['CityID']; 
				$SC = $row['CTelephone']; 
				$II = $row['ItemID']; 
				//echo "<b>Here is a description of $IN...<br><br>";
				echo "<div class="subheader">". $IN ; 
				echo "</div>"; 
				echo "<div class="frontPageContentBox">"; 
				echo "<table cellpadding="5" cellspacing="3" width="100%" border="2">"; 
				echo "<tr>"; 
	
				/* Image will be here */ 
				echo "<td valign="top" align="left" width='30' class="grayborder">"; 
				echo "<img src="$Relative/images/s".$II."">"; 
				echo "</td>"; 
				/* Image will END here */ 
	
				echo "<td valign="top" align="left" class="company">"; 
				echo "<BR>"; 

				echo $IC."<BR>"; 
				echo "t: ".$SC."<BR>"; 
				echo "f: ".$CFax."<BR>"; 
				echo "e: ".$CompanyEmail."<BR>"; 
				echo "w: ".$WebAddress; 

				echo "</td>"; 
	
				echo "<td valign="top" align="left" class="companydesc">"; 
				echo "<br>"; 
				echo "<br>"; 
				echo "<a href="show_image.php?II=$II&CA=$CA">more info ...</A>"; 
				echo "</td>"; 
				echo "</tr>"; 
				echo "</table>"; 
				echo "<br>\n\n"; 
				echo "</div>"; 
			} 
	
			print_r($CityID); 
			?> 

<!-- Middle TD ends here --> 
		</TD> 

		<TD align="left" valign="top" class="rightfill"> 

<!-- BEGINNING RIGHT HAND SIDE TABLE WITH LINKS IN AND STYLE iT CSS --> 
<!--
			<span class="subheader"></span> 
			<br><br> 
			<div class="atext"></div> 
			<br clear="ALL"><br> 
-->
<!-- END RIGHT HAND SIDE TABLE WITH LINKS IN AND STYLE iT CSS  --> 

			<div class="ffheader">
				<b>Useful Links</b> 
			</div> 
		</td> 
	</tr> 
</table> 


<? 
require ("footer.php"); 
?>
bionicdonkey
Forum Contributor
Posts: 132
Joined: Fri Jan 31, 2003 2:28 am
Location: Sydney, Australia
Contact:

Post by bionicdonkey »

put all connection stuff into connection.php e.g.
mysql_pconnect(....);
mysql_select_db("dbname");
meandrew
Forum Commoner
Posts: 49
Joined: Mon Feb 24, 2003 1:03 pm

Post by meandrew »

excellent thank you Bionic

all is up and running now.

Now I just need to build on the structure etc any ideas how to pull an ID from mysql and display an image that is assigned to the ID?

Andrew
Post Reply