Hyperon
asked on
Why doesnt my script work?
this is my entire page for a programming site i am building. This is my first time using PHP and MySQL and i don't know where ive gone wrong. The aim is to enter a database, select 1 row from it that matches the 'id' in the query string, then select data from a different table where the row depends upon data from the previous table
its well commented
<?php echo "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?". ">"; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style type="text/css">
body {
font-family:arial,helvetic a,verdana, sans-serif ,serif;
color:#000;
background-color:#fff;
}
td a, td a:active, td a:link, td a:visited {color:#fc0; display:block; text-align:center; text-decoration:none; width:135px;}
td a:hover {color:#f00; text-decoration:none; background-color:#fff;}
h2 {color:#fff;}
td {vertical-align:middle;}
</style>
<?PHP
//The aim of this section is to retrieve data from a mySQL database
//1. connect to mySQL
$db = mysql_connect("localhost", "client") or die("Couldnt Connect to mysql");
//2. select the db. NOTE: I select the db via SQL as I use PHP4. mysql_select_db won't work
mysql_query("use prog",$db) or die("couldnt connect to the database");
//3. store id for tidier query
$qid=$HTTP_GET_VARS[id];
//4. get data about the article
$result=mysql_query("selec t title,author,body,attach from articles where id='$qid'",$db);
//5. NEED TO SPLIT DATA INTO ARRAY - mysql_get_row() ???????? array name will be : dbdata
//----------------------ST UCK ABOVE--------------------- ----
//QUESTION : do you use '@' to denote an array as in PERL? I know that regardless
//you'd still use $ (scalar) to access each piece
//6. get data about the author - different table - id is taken from previous query
$result=mysql_query("selec t name,pic,picdesc,email,for umname where id='$dbdata[1]'",$db);
//7. echoes the title of the article to be the document title --- we are still in header section
echo "<title>Calgen Studios Programming Archives - " & $dbdata[0] & "</title>";
//8. tidy up!
mysql_close($db);
?>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<div align="center"><img align="center" src="banner.gif" /></div>
<br />
<table width="750" cellspacing="0" cellpadding="0" align="center">
<tr>
<td height="75" width="600" bgcolor="#ff0000">
<h2 align="center">Calgen Studios - Programming Archives</h2>
</td>
<td width="150" rowspan="2"><img src="cslogo.gif" /></td>
</tr>
<tr>
<td height="75" width="600" bgcolor="#ffcc00">
<?PHP //echo the article title into the yellow/orange bar
echo "<h2 align=\"center\" style=\"color:#000;\">";
echo $dbdata[0];
echo"</h2>";
?>
</td>
</tr>
<tr>
<td colspan="2">
<table width="750" cellspacing="0" cellpadding="0" align="center">
<tr><th style="background-color:#0 69; height:25px; color:#fff;" colspan="2">
<?PHP //Authorname
/*
echo "Article submitted by ";
echo $authordata[0];
*/?>
</th></tr>
<tr>
<?PHP //left ?>
<td bgcolor="#006699" width="135">
<table cellspacing="0" cellpadding="0" border="0">
<tr><td><a href="prog.php?page=home"> Home</a></ td></tr>
<tr><td><a href="prog.php?page=asm">A ssembler (ASM)</a></td></tr>
<tr><td><a href="prog.php?page=c">C/C ++</a></td ></tr>
<tr><td><a href="prog.php?page=c_shar p">C#</a>< /td></tr>
<tr><td><a href="prog.php?page=fortra n">Fortran </a></td>< /tr>
<tr><td><a href="prog.php?page=java"> Java</a></ td></tr>
<tr><td><a href="prog.php?page=perl"> Perl</a></ td></tr>
<tr><td><a href="prog.php?page=php">P HP</a></td ></tr>
<tr><td><a href="prog.php?page=python ">Python</ a></td></t r>
<tr><td><a href="prog.php?page=vb">Vi sual Basic</a></td></tr>
<tr><td valign="middle" height="9"><img src="menusep.gif" width="130" height="3" /></td></tr>
</table>
</td>
<?PHP //main ?>
<td width="615">
<table cellspacing="0" cellpadding="0" border="0">
<tr>
<td>
<img src="nopic.gif" width="90" height="120" />
</td>
<td><br />
<a href="mailto:nowhere"><img src="email.gif" width="5" height="5" />Email the author</a><br />
<?PHP //profile.php params : ?>
<a><img src="forumpic.gif" width="5" height="5" />PM the Author's forum account</a><br />
<?PHP //privmsg.php params : ?>
<a><img src="forumprofile.gif" width="5" height="5" />View the Author's forum profile</a><br />
</td>
</tr>
</table>
</td>
</tr>
</table>
</td></tr>
<tr><td height="10"></td></tr><tr>
<td colspan="2">
<table cellspacing="0" cellpadding="0" border="0">
<tr>
<td><img src="sidebanner.gif" height="400" width="100" /></td>
<td width="550" style="padding:10px; text-align:center; vertical-align:top;">
Blahblahblahblahblahblahbl ahblahblah blahblahbl ahblahblah blahblahbl ah
blahblahblahblahblahblahbl ahblahblah blahblahbl ahblahblah blahblahbl ahblah
blahblahblahblahblahblahbl ahblahblah blahblahbl ahblahblah blahblahbl ahblah
blahblahblahblahblahblahbl ahblahblah blahblahbl ahblahblah blahblahbl ahblah
</td>
<td><img src="sidebanner.gif" height="400" width="100" /></td>
</tr>
</table>
</td>
</tr>
<tr><hr size="5" /></tr>
<?PHP /*
<?PHP $db = mysql_connect("localhost", "client", "");
mysql_select_db("hitcounte r",$db);
mysql_query("UPDATE hits SET hits=hits+1 WHERE page = '$PHP_SELF'",$db);
if (mysql_affected_rows($db) < 1) { //if this page hasnt been visited yet
$result = mysql_query("INSERT INTO hits VALUES ('$PHP_SELF', 1)", $db); //create row for this page
}
$hitcount = mysql_query("select hits from hits where page='$PHP_SELF'",$db);
//$allhits = mysql_query("select hits from hits");
echo "<tr><td height=\"10\">";
echo $hitcount;
echo "</td></tr>";
mysql_close($db); */?>
</table>
</body>
</html>
thanks in advance, Hyperon
its well commented
<?php echo "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?".
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style type="text/css">
body {
font-family:arial,helvetic
color:#000;
background-color:#fff;
}
td a, td a:active, td a:link, td a:visited {color:#fc0; display:block; text-align:center; text-decoration:none; width:135px;}
td a:hover {color:#f00; text-decoration:none; background-color:#fff;}
h2 {color:#fff;}
td {vertical-align:middle;}
</style>
<?PHP
//The aim of this section is to retrieve data from a mySQL database
//1. connect to mySQL
$db = mysql_connect("localhost",
//2. select the db. NOTE: I select the db via SQL as I use PHP4. mysql_select_db won't work
mysql_query("use prog",$db) or die("couldnt connect to the database");
//3. store id for tidier query
$qid=$HTTP_GET_VARS[id];
//4. get data about the article
$result=mysql_query("selec
//5. NEED TO SPLIT DATA INTO ARRAY - mysql_get_row() ???????? array name will be : dbdata
//----------------------ST
//QUESTION : do you use '@' to denote an array as in PERL? I know that regardless
//you'd still use $ (scalar) to access each piece
//6. get data about the author - different table - id is taken from previous query
$result=mysql_query("selec
//7. echoes the title of the article to be the document title --- we are still in header section
echo "<title>Calgen Studios Programming Archives - " & $dbdata[0] & "</title>";
//8. tidy up!
mysql_close($db);
?>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<div align="center"><img align="center" src="banner.gif" /></div>
<br />
<table width="750" cellspacing="0" cellpadding="0" align="center">
<tr>
<td height="75" width="600" bgcolor="#ff0000">
<h2 align="center">Calgen Studios - Programming Archives</h2>
</td>
<td width="150" rowspan="2"><img src="cslogo.gif" /></td>
</tr>
<tr>
<td height="75" width="600" bgcolor="#ffcc00">
<?PHP //echo the article title into the yellow/orange bar
echo "<h2 align=\"center\" style=\"color:#000;\">";
echo $dbdata[0];
echo"</h2>";
?>
</td>
</tr>
<tr>
<td colspan="2">
<table width="750" cellspacing="0" cellpadding="0" align="center">
<tr><th style="background-color:#0
<?PHP //Authorname
/*
echo "Article submitted by ";
echo $authordata[0];
*/?>
</th></tr>
<tr>
<?PHP //left ?>
<td bgcolor="#006699" width="135">
<table cellspacing="0" cellpadding="0" border="0">
<tr><td><a href="prog.php?page=home">
<tr><td><a href="prog.php?page=asm">A
<tr><td><a href="prog.php?page=c">C/C
<tr><td><a href="prog.php?page=c_shar
<tr><td><a href="prog.php?page=fortra
<tr><td><a href="prog.php?page=java">
<tr><td><a href="prog.php?page=perl">
<tr><td><a href="prog.php?page=php">P
<tr><td><a href="prog.php?page=python
<tr><td><a href="prog.php?page=vb">Vi
<tr><td valign="middle" height="9"><img src="menusep.gif" width="130" height="3" /></td></tr>
</table>
</td>
<?PHP //main ?>
<td width="615">
<table cellspacing="0" cellpadding="0" border="0">
<tr>
<td>
<img src="nopic.gif" width="90" height="120" />
</td>
<td><br />
<a href="mailto:nowhere"><img
<?PHP //profile.php params : ?>
<a><img src="forumpic.gif" width="5" height="5" />PM the Author's forum account</a><br />
<?PHP //privmsg.php params : ?>
<a><img src="forumprofile.gif" width="5" height="5" />View the Author's forum profile</a><br />
</td>
</tr>
</table>
</td>
</tr>
</table>
</td></tr>
<tr><td height="10"></td></tr><tr>
<td colspan="2">
<table cellspacing="0" cellpadding="0" border="0">
<tr>
<td><img src="sidebanner.gif" height="400" width="100" /></td>
<td width="550" style="padding:10px; text-align:center; vertical-align:top;">
Blahblahblahblahblahblahbl
blahblahblahblahblahblahbl
blahblahblahblahblahblahbl
blahblahblahblahblahblahbl
</td>
<td><img src="sidebanner.gif" height="400" width="100" /></td>
</tr>
</table>
</td>
</tr>
<tr><hr size="5" /></tr>
<?PHP /*
<?PHP $db = mysql_connect("localhost",
mysql_select_db("hitcounte
mysql_query("UPDATE hits SET hits=hits+1 WHERE page = '$PHP_SELF'",$db);
if (mysql_affected_rows($db) < 1) { //if this page hasnt been visited yet
$result = mysql_query("INSERT INTO hits VALUES ('$PHP_SELF', 1)", $db); //create row for this page
}
$hitcount = mysql_query("select hits from hits where page='$PHP_SELF'",$db);
//$allhits = mysql_query("select hits from hits");
echo "<tr><td height=\"10\">";
echo $hitcount;
echo "</td></tr>";
mysql_close($db); */?>
</table>
</body>
</html>
thanks in advance, Hyperon
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
ah, i see now
<?php echo "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?". ">"; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style type="text/css">
body {
font-family:arial,helvetic a,verdana, sans-serif ,serif;
color:#000;
background-color:#fff;
}
td a, td a:active, td a:link, td a:visited {color:#fc0; display:block; text-align:center; text-decoration:none; width:135px;}
td a:hover {color:#f00; text-decoration:none; background-color:#fff;}
h2 {color:#fff;}
td {vertical-align:middle;}
</style>
<?PHP
//The aim of this section is to retrieve data from a mySQL database
//1. connect to mySQL
$db = mysql_connect("localhost", "client") or die("Couldnt Connect to mysql");
//2. select the db. NOTE: I select the db via SQL as I use PHP4. mysql_select_db won't work
mysql_query("use prog",$db) or die("couldnt connect to the database");
//3. store id for tidier query
$qid=$HTTP_GET_VARS[id];
//4. get data about the article
$result=mysql_query("selec t title,author,body,attach from articles where id='$qid'",$db);
//5. Split data into array
$dbdata=mysql_fetch_row($r esult);
//6. get data about the author - different table - id is taken from previous query
$result=mysql_query("selec t name,pic,picdesc,email,for umname from authors where id='$dbdata[1]'",$db);
//7. echoes the title of the article to be the document title --- we are still in header section
echo "<title>Calgen Studios Programming Archives - '$dbdata[0]'</title>";
//8. tidy up!
mysql_close($db);
?>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<div align="center"><img align="center" src="banner.gif" /></div>
<br />
<table width="750" cellspacing="0" cellpadding="0" align="center">
<tr>
<td height="75" width="600" bgcolor="#ff0000">
<h2 align="center">Calgen Studios - Programming Archives</h2>
</td>
<td width="150" rowspan="2"><img src="cslogo.gif" /></td>
</tr>
<tr>
<td height="75" width="600" bgcolor="#ffcc00">
<?PHP //echo the article title into the yellow/orange bar
echo ("<h2 align="center" style="color:#000;">");
echo $dbdata[0];
echo"</h2>";
?>
</td>
</tr>
<tr>
<td colspan="2">
<table width="750" cellspacing="0" cellpadding="0" align="center">
<tr><th style="background-color:#0 69; height:25px; color:#fff;" colspan="2">
<?PHP //Authorname
echo "Article submitted by '$authordata[0]'";
?>
</th></tr>
<tr>
<?PHP //left ?>
<td bgcolor="#006699" width="135">
<table cellspacing="0" cellpadding="0" border="0">
<tr><td><a href="prog.php?page=home"> Home</a></ td></tr>
<tr><td><a href="prog.php?page=asm">A ssembler (ASM)</a></td></tr>
<tr><td><a href="prog.php?page=c">C/C ++</a></td ></tr>
<tr><td><a href="prog.php?page=c_shar p">C#</a>< /td></tr>
<tr><td><a href="prog.php?page=fortra n">Fortran </a></td>< /tr>
<tr><td><a href="prog.php?page=java"> Java</a></ td></tr>
<tr><td><a href="prog.php?page=perl"> Perl</a></ td></tr>
<tr><td><a href="prog.php?page=php">P HP</a></td ></tr>
<tr><td><a href="prog.php?page=python ">Python</ a></td></t r>
<tr><td><a href="prog.php?page=vb">Vi sual Basic</a></td></tr>
<tr><td valign="middle" height="9"><img src="menusep.gif" width="130" height="3" /></td></tr>
</table>
</td>
<?PHP //main ?>
<td width="615">
<table cellspacing="0" cellpadding="0" border="0">
<tr>
<td>
<img src="nopic.gif" width="90" height="120" />
</td>
<td><br />
<a href="mailto:nowhere"><img src="email.gif" width="5" height="5" />Email the author</a><br />
<?PHP //profile.php params : ?>
<a><img src="forumpic.gif" width="5" height="5" />PM the Author's forum account</a><br />
<?PHP //privmsg.php params : ?>
<a><img src="forumprofile.gif" width="5" height="5" />View the Author's forum profile</a><br />
</td>
</tr>
</table>
</td>
</tr>
</table>
</td></tr>
<tr><td height="10"></td></tr><tr>
<td colspan="2">
<table cellspacing="0" cellpadding="0" border="0">
<tr>
<td><img src="sidebanner.gif" height="400" width="100" /></td>
<td width="550" style="padding:10px; text-align:center; vertical-align:top;">
Blahblahblahblahblahblahbl ahblahblah blahblahbl ahblahblah blahblahbl ah
blahblahblahblahblahblahbl ahblahblah blahblahbl ahblahblah blahblahbl ahblah
blahblahblahblahblahblahbl ahblahblah blahblahbl ahblahblah blahblahbl ahblah
blahblahblahblahblahblahbl ahblahblah blahblahbl ahblahblah blahblahbl ahblah
</td>
<td><img src="sidebanner.gif" height="400" width="100" /></td>
</tr>
</table>
</td>
</tr>
<tr><hr size="5" /></tr>
<?PHP /*
<?PHP $db = mysql_connect("localhost", "client", "");
mysql_select_db("hitcounte r",$db);
mysql_query("UPDATE hits SET hits=hits+1 WHERE page = '$PHP_SELF'",$db);
if (mysql_affected_rows($db) < 1) { //if this page hasnt been visited yet
$result = mysql_query("INSERT INTO hits VALUES ('$PHP_SELF', 1)", $db); //create row for this page
}
$hitcount = mysql_query("select hits from hits where page='$PHP_SELF'",$db);
//$allhits = mysql_query("select hits from hits");
echo "<tr><td height=\"10\">";
echo $hitcount;
echo "</td></tr>";
mysql_close($db); */?>
</table>
</body>
</html>
should this now work?
<?php echo "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?".
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style type="text/css">
body {
font-family:arial,helvetic
color:#000;
background-color:#fff;
}
td a, td a:active, td a:link, td a:visited {color:#fc0; display:block; text-align:center; text-decoration:none; width:135px;}
td a:hover {color:#f00; text-decoration:none; background-color:#fff;}
h2 {color:#fff;}
td {vertical-align:middle;}
</style>
<?PHP
//The aim of this section is to retrieve data from a mySQL database
//1. connect to mySQL
$db = mysql_connect("localhost",
//2. select the db. NOTE: I select the db via SQL as I use PHP4. mysql_select_db won't work
mysql_query("use prog",$db) or die("couldnt connect to the database");
//3. store id for tidier query
$qid=$HTTP_GET_VARS[id];
//4. get data about the article
$result=mysql_query("selec
//5. Split data into array
$dbdata=mysql_fetch_row($r
//6. get data about the author - different table - id is taken from previous query
$result=mysql_query("selec
//7. echoes the title of the article to be the document title --- we are still in header section
echo "<title>Calgen Studios Programming Archives - '$dbdata[0]'</title>";
//8. tidy up!
mysql_close($db);
?>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<div align="center"><img align="center" src="banner.gif" /></div>
<br />
<table width="750" cellspacing="0" cellpadding="0" align="center">
<tr>
<td height="75" width="600" bgcolor="#ff0000">
<h2 align="center">Calgen Studios - Programming Archives</h2>
</td>
<td width="150" rowspan="2"><img src="cslogo.gif" /></td>
</tr>
<tr>
<td height="75" width="600" bgcolor="#ffcc00">
<?PHP //echo the article title into the yellow/orange bar
echo ("<h2 align="center" style="color:#000;">");
echo $dbdata[0];
echo"</h2>";
?>
</td>
</tr>
<tr>
<td colspan="2">
<table width="750" cellspacing="0" cellpadding="0" align="center">
<tr><th style="background-color:#0
<?PHP //Authorname
echo "Article submitted by '$authordata[0]'";
?>
</th></tr>
<tr>
<?PHP //left ?>
<td bgcolor="#006699" width="135">
<table cellspacing="0" cellpadding="0" border="0">
<tr><td><a href="prog.php?page=home">
<tr><td><a href="prog.php?page=asm">A
<tr><td><a href="prog.php?page=c">C/C
<tr><td><a href="prog.php?page=c_shar
<tr><td><a href="prog.php?page=fortra
<tr><td><a href="prog.php?page=java">
<tr><td><a href="prog.php?page=perl">
<tr><td><a href="prog.php?page=php">P
<tr><td><a href="prog.php?page=python
<tr><td><a href="prog.php?page=vb">Vi
<tr><td valign="middle" height="9"><img src="menusep.gif" width="130" height="3" /></td></tr>
</table>
</td>
<?PHP //main ?>
<td width="615">
<table cellspacing="0" cellpadding="0" border="0">
<tr>
<td>
<img src="nopic.gif" width="90" height="120" />
</td>
<td><br />
<a href="mailto:nowhere"><img
<?PHP //profile.php params : ?>
<a><img src="forumpic.gif" width="5" height="5" />PM the Author's forum account</a><br />
<?PHP //privmsg.php params : ?>
<a><img src="forumprofile.gif" width="5" height="5" />View the Author's forum profile</a><br />
</td>
</tr>
</table>
</td>
</tr>
</table>
</td></tr>
<tr><td height="10"></td></tr><tr>
<td colspan="2">
<table cellspacing="0" cellpadding="0" border="0">
<tr>
<td><img src="sidebanner.gif" height="400" width="100" /></td>
<td width="550" style="padding:10px; text-align:center; vertical-align:top;">
Blahblahblahblahblahblahbl
blahblahblahblahblahblahbl
blahblahblahblahblahblahbl
blahblahblahblahblahblahbl
</td>
<td><img src="sidebanner.gif" height="400" width="100" /></td>
</tr>
</table>
</td>
</tr>
<tr><hr size="5" /></tr>
<?PHP /*
<?PHP $db = mysql_connect("localhost",
mysql_select_db("hitcounte
mysql_query("UPDATE hits SET hits=hits+1 WHERE page = '$PHP_SELF'",$db);
if (mysql_affected_rows($db) < 1) { //if this page hasnt been visited yet
$result = mysql_query("INSERT INTO hits VALUES ('$PHP_SELF', 1)", $db); //create row for this page
}
$hitcount = mysql_query("select hits from hits where page='$PHP_SELF'",$db);
//$allhits = mysql_query("select hits from hits");
echo "<tr><td height=\"10\">";
echo $hitcount;
echo "</td></tr>";
mysql_close($db); */?>
</table>
</body>
</html>
should this now work?
>>The aim is to enter a database, select 1 row from it that matches the 'id'
Since that is the case you need to also modify your SQL query like so:
$result=mysql_query('SELEC T `title`,`author`,`body`,`a ttach` FROM `articles` WHERE `id`="'.$qid.'" LIMIT 1;',$db)
Since that is the case you need to also modify your SQL query like so:
$result=mysql_query('SELEC
just a little side note here...
>> //3. store id for tidier query
>> $qid=$HTTP_GET_VARS[id];
You should use single quotes around id:
$qid=$HTTP_GET_VARS['id'];
Without the quotes, php looks first for a defined() constant named id
>> //3. store id for tidier query
>> $qid=$HTTP_GET_VARS[id];
You should use single quotes around id:
$qid=$HTTP_GET_VARS['id'];
Without the quotes, php looks first for a defined() constant named id
ASKER
(i tried to split the points but my browser wouldn't play [IE is broken, netscape don't work on XP, opera and mozilla wont split points])
sorry
Hyperon
and thank you very much
sorry
Hyperon
and thank you very much
Glad I could help, and thank-you for the A!
ASKER
no problem, thanks for the help!
$dbdata = array();
or just start pluging in values:
$dbdata[] = "index one";
or perhaps best for what you are trying to do, just assign an array to the variable:
$dbdata = mysql_fetch_row($result);