Reply to topic
PHP Help
heather


Joined: 25 Apr 2005
Posts: 1
Reply with quote
Can anyone see anything wrong with this code? :

Code:

<?
$user="myusername";
$password="mypassword";
$database="mydatabase";

mysql_connect("10.10.0.23",$user,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT id,pageTitle,pageContent FROM tbl_Content";
$result=mysql_query($query);

mysql_close();


 
$i=0;
while ($i < $num) {

$id=mysql_result($result,$i,"id");
$pageTitle=mysql_result($result,$i,"pageTitle");
$pageContent=mysql_result($result,$i,"pageContent");
?>


<? echo $pageTitle; ?></font><br />

<?
$i++;
}


echo "<hr size=1 width=90%>";



?>


I know there is data in database, as we can retrieve it with ColdFusion. However, when we attempt to retrieve it with PHP, only the horizontal line shows.

Thanks in advance,

Heather
DatabaseDude


Joined: 04 Mar 2005
Posts: 67
Location: Elkins, WV
Reply with quote
I am not familiar with PHP, but I'm wondering if the database connection being closed before you loop through the data has something to do with it ... are the query results being held in memory after mysql_close()? Perhaps move that statement down to the end of the page (always making sure to close your connection, by all means).

Bryant
cheryl


Joined: 17 Apr 2004
Posts: 84
Location: Newark, DE
Reply with quote
I've looked over your code a bit and didn't really see where you were defining $num. Is that supposed to be defined as the number of rows returned from the query? I recommend checking out http://us4.php.net/mysql, specifically http://us4.php.net/manual/en/function.mysql-affected-rows.php if you wanted to get the number of rows returned and loop through it.
$num
mferguson


Joined: 27 Oct 2004
Posts: 3
Reply with quote
Try adding "$num=mysql_numrows($result);" right before your mysql_close(); statement so that $num gets defined as the number of returned rows.
pmeserve
HostMySite Tech

Joined: 19 Mar 2004
Posts: 178
Reply with quote
Here's how I would try to simplify things a bit. I haven't tested this but it should work:

Code:
<?php

$user="myusername";
$password="mypassword";
$database="mydatabase";

$link = mysql_connect("10.10.0.23",$user,$password);
mysql_select_db($database, $link) or die( "Unable to select database");

$query = "SELECT id,pageTitle,pageContent FROM tbl_Content";
$result = mysql_query($query);

while ($row = mysql_fetch_assoc($result))
   echo "$row['pageTitle'] </font><br />";

echo "<hr size=1 width=90%>";

?>
PHP Help
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
All times are GMT  
Page 1 of 1  

  
  
 Reply to topic