...

View Full Version : error in php code



ROYW1000
01-25-2010, 12:39 PM
Hi

I have been given this code and not sure thy when I run this script I get no output.

Can anyone see whats wrong and how to correct please. Its the php code as I know the query works.




<?php
$hostname = "localhost";
$database = "sextoys";
$username = "sextoys";
$password = "sextoys";
mysql_connect($hostname, $username, $password) or die(mysql_error());
mysql_select_db($database) or die(mysql_error());
$query="SELECT t1.title AS lev1, t1.sectionID AS id1,
t2.title as lev2, t2.sectionID AS id2,
t3.title as lev3, t3.sectionID AS id3,
t4.title as lev4, t4.sectionID AS id4
FROM jss_sections AS t1
LEFT JOIN jss_sections AS t2 ON t2.parent = t1.sectionID
LEFT JOIN jss_sections AS t3 ON t3.parent = t2.sectionID
LEFT JOIN jss_sections AS t4 ON t4.parent = t3.sectionID
WHERE t1.title = 'Main Section'";
$id1 = -1;
while ( get a row into $row )
{
if ( $row['id1'] != $id1 )
{
echo $row['lev1'] . "<br/>\n";
$id2 = -1; $id3 = -1; $id4 = -1;
$id1 = $row['id1'];
}
if ( is_set($row['id2'] && $row['id2'] != $id2 )
{
echo $row['lev1'] . " >> " . $row['lev2'] . "<br/>\n";
$id3 = -1; $id4 = -1;
$id2 = $row['id2'];
}
if ( is_set($row['id3'] && $row['id3'] != $id3 )
{
echo $row['lev1'] . " >> " . $row['lev2'] . " >> " . $row['lev3'] . "<br/>\n";
$id4 = -1;
$id3 = $row['id3'];
}
if ( is_set($row['id4'] && $row['id4'] != $id4 )
{
echo $row['lev1'] . " >> " . $row['lev2'] . " >> " . $row['lev3'] . " >> " . $row['lev4'] . "<br/>\n";
$id4 = $row['id4'];
}
}
mysql_close();
?>


Thanks
Roy

Dormilich
01-25-2010, 12:43 PM
while ( get a row into $row )
this certainly is not valid PHP.

ROYW1000
01-25-2010, 01:02 PM
Hi

Have tried removing that with no joy. Not sure if it needs the following or not

$result=mysql_query($query);

$num=mysql_numrows($result);

and something like this:-

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

$T1=mysql_result($result,$T,"T1");
$T2=mysql_result($result,$T,"T2");
$T3=mysql_result($result,$T,"T3");
$T4=mysql_result($result,$T,"T4");
?>

As had something like this in another query but its been ammended and not sure what its missing now.

Thanks
Roy

Dormilich
01-25-2010, 01:06 PM
rather use mysql_fetch_row() (http://php.net/mysql-fetch-row) and related, thatís the standard way to handle the result. (Although the PDO Guy would first fetch the primary keys and then get each row separately)

ROYW1000
01-25-2010, 02:38 PM
Hi

I have tried your suggestion and run the script and all I am getting is a totally blank screen with no error messages.



<?php
$hostname = "localhost";
$database = "sextoys";
$username = "sextoys";
$password = "sextoys";
mysql_connect($hostname, $username, $password) or die(mysql_error());
mysql_select_db($database) or die(mysql_error());
$query="SELECT t1.title AS lev1, t1.sectionID AS id1,
t2.title as lev2, t2.sectionID AS id2,
t3.title as lev3, t3.sectionID AS id3,
t4.title as lev4, t4.sectionID AS id4
FROM jss_sections AS t1
LEFT JOIN jss_sections AS t2 ON t2.parent = t1.sectionID
LEFT JOIN jss_sections AS t3 ON t3.parent = t2.sectionID
LEFT JOIN jss_sections AS t4 ON t4.parent = t3.sectionID";
$result=mysql_query($query);
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
$row = mysql_fetch_row($result);
mysql_close();
$id1 = -1;
while
{
if ( $row['id1'] != $id1 )
{
echo $row['lev1'] . "<br/>\n";
$id2 = -1; $id3 = -1; $id4 = -1;
$id1 = $row['id1'];
}
if ( is_set($row['id2'] && $row['id2'] != $id2 )
{
echo $row['lev1'] . " >> " . $row['lev2'] . "<br/>\n";
$id3 = -1; $id4 = -1;
$id2 = $row['id2'];
}
if ( is_set($row['id3'] && $row['id3'] != $id3 )
{
echo $row['lev1'] . " >> " . $row['lev2'] . " >> " . $row['lev3'] . "<br/>\n";
$id4 = -1;
$id3 = $row['id3'];
}
if ( is_set($row['id4'] && $row['id4'] != $id4 )
{
echo $row['lev1'] . " >> " . $row['lev2'] . " >> " . $row['lev3'] . " >> " . $row['lev4'] . "<br/>\n";
$id4 = $row['id4'];
}
}
mysql_close();
?>

Dormilich
01-25-2010, 03:33 PM
may I point out that the manual page has sufficient examples as well as links worth further reading, which also have sufficient examples?

I wonder that your code executes at all. (while usually has a loop condition, etc.)

ROYW1000
01-25-2010, 09:31 PM
Hi

I have tried everything tonight to try and fix this all I know at the moment is that the issue is in the 2nd part of the script the 2nd if statement.



$query="SELECT t1.title AS lev1, t1.sectionID AS id1,
t2.title as lev2, t2.sectionID AS id2,
t3.title as lev3, t3.sectionID AS id3,
t4.title as lev4, t4.sectionID AS id4
FROM jss_sections AS t1
LEFT JOIN jss_sections AS t2 ON t2.parent = t1.sectionID
LEFT JOIN jss_sections AS t3 ON t3.parent = t2.sectionID
LEFT JOIN jss_sections AS t4 ON t4.parent = t3.sectionID
where t1.title='Main Section'";
$id1 = -1;
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
if ( $row['id1'] != $id1 )
{
echo $row['lev1'] . "<br/>\n";
$id2 = -1; $id3 = -1; $id4 = -1; $id1 = $row['id1'];
}
if (isset($row['id2'] && $row['id2'] != $id2 ))
{
echo $row['lev1'] . " >> " . $row['lev2'] . "<br/>\n";
$id3 = -1; $id4 = -1;
$id2 = $row['id2'];
}
}

mysql_close();
?>


If I stop the code at the 2nd if I get one result through which is correct for that level. The 2nd part of the script should bring out a further 80+ result but I just get a blank screen.

I have tried DEBUG and get no errors, I have PHP error reporting on and it shows nothing wrong but all I get is this blank page.

Any help please.

Roy

Dormilich
01-25-2010, 10:06 PM
not sure if this is intended

if (isset($row['id2'] && $row['id2'] != $id2 ))

I’d expected rather

if (isset($row['id2']) && $row['id2'] != $id2 )
although I’d say the isset() part is not necessary, since you put "id2" in through your SQL.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum