Forum Moderators: coopster

Message Too Old, No Replies

Display content by using ID with PHP

Cannot make it work!

         

henry0

6:58 pm on May 28, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello
From my db I try do display a text content that is represented by $id (here id=2)
Php tries to tell me that I have an error
At : echo "$page_id";

I cannot figure where my error is

Greatly appreciate any help

Regards

<?
include("dbinfo.inc.php");

mysql_connect ($host,$username,$password);
@mysql_select_db($database) or die("unable to select database");
$query = "SELECT page_content from web_pages WHERE $page_id=2";
$result=mysql_query($query);

$num=mysql_numrows($result);
mysql_close();
?>

<table border="2">

<?
$i=0;
while ($i<$num)
{
$page_id=mysql_result($result,$i,$page_id)";
?>

<tr>
<td>

<?
echo "$page_id";
?>
</tr></td>
<?
++$i;
}
echo "</table>";
?>

jatar_k

7:06 pm on May 28, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I think if you do

echo $query;

before you do your mysql_query you may notice that your query is wrong. This may be better

$query = "SELECT page_content from web_pages WHERE page_id=2";

you had a dollar sign before page_id which will prompt php to put the var value in that spot.

you also use mysql_close before you do mysql_result which will cause mysql_result to not have access to the db, therefore not returning the desired result.

jatar_k

7:11 pm on May 28, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you're reusing the $page_id var too many times and for too many things. I have a sneaking suspicion that you data is completely corrupted.

I also think
$page_id=mysql_result($result,$i,$page_id)";

should be
$page_id=mysql_result($result,$i,"page_content.page_id");

I removed the double quote and the dollar sign.

Also as mentioned here
[php.net...]

When working on large result sets, you should consider using one of the functions that fetch an entire row (specified below). As these functions return the contents of multiple cells in one function call, they're MUCH quicker than mysql_result(). Also, note that specifying a numeric offset for the field argument is much quicker than specifying a fieldname or tablename.fieldname argument.

henry0

7:25 pm on May 28, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thank you
I still get an error at:
echo "$query";

here is the modified script
<?
include("dbinfo.inc.php");

mysql_connect ($host,$username,$password);
@mysql_select_db($database) or die("unable to select database");
$query = "SELECT page_content from web_pages WHERE page_id=2";
$result=mysql_query($query);

$num=mysql_numrows($result);

?>

<table border="2">

<?
$i=0;
while ($i<$num)
{
$page_id=mysql_result($result,$i)";
mysql_close();
?>

<tr>
<td>

<?
echo "$query";
?>
</tr></td>
<?
++$i;
}
echo "</table>";
?>

jatar_k

7:33 pm on May 28, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



try this, all relevant functions can be looked up at www.php.net.

include("dbinfo.inc.php");

mysql_connect ($host,$username,$password);
mysql_select_db($database) or die("unable to select database");
$query = "SELECT page_content from web_pages WHERE page_id=2";
$result=mysql_query($query);

echo "<table border=\"2\">";
while ($row = mysql_fetch_array($result)) {
echo "<tr><td>",$row['page_content'],"</tr></td>";
}
echo "</table>";

henry0

7:38 pm on May 28, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Jatar K
I indeed learned a great deal

thak you very much
It works fine
thanks for the links