Jump to content

Why won't this script work appropriately?


yuckysocks

Recommended Posts

Any help would be appreciated. Specifically, the <h4> tag doesn't contain anything when this is run.

 

Thanks!

 

<?php
$id = intval($_GET['id']);

echo "<div id=\"content\">";
echo "<h3 id=\"title\">Case Study Details</h3>";

$link = mysql_connect('localhost', 'name', 'pw')
    or die('Could not connect: ' . mysql_error());
mysql_select_db('case_studies') or die('Could not select database');
$query = "SELECT schoolname FROM casestudies
		WHERE id = $id";
$result = $schoolname;
echo "<h4>$schoolname</h4>";
$query = "SELECT * FROM casestudies
            WHERE id = $id";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
$row = mysql_fetch_assoc($result);


    foreach ($row as $fld => $val)
    {
        echo "<p class=\"caseresult\"><b>$fld</b> : $val </p>";
    }

?>

Link to comment
https://forums.phpfreaks.com/topic/99580-why-wont-this-script-work-appropriately/
Share on other sites

You need to do a query and a fetch before you can use the values.

 

This

<?php
$query = "SELECT schoolname FROM casestudies
		WHERE id = $id";
$result = $schoolname;
?>

should probably be

<?php
$query = "SELECT schoolname FROM casestudies WHERE id = $id";
$rs = mysql_query($query) or die("Problem with the query: $query<br>" . mysql_error());
$rw = mysql_fetch_assoc($rs);
$result = $rw['schoolname'];
?>

 

Also, why don't you just use your second query first, so you don't have to issue the same query twice?

 

Ken

Well, <looking at toes> the real answer is I don't know enough about PHP to do otherwise. This is an amalgam of copied and pasted code with my own database names thrown in. Thanks for the tips

 

 

I guess my -REAL- answer is that I don't know how to pull just the piece of data I want out of the array that's produce by the first query.

 

Back to the books I suppose.

Try:

<?php
$id = intval($_GET['id']);

echo '<div id="content">';
echo '<h3 id="title">Case Study Details</h3>';

$link = mysql_connect('localhost', 'name', 'pw') or die('Could not connect: ' . mysql_error());
mysql_select_db('case_studies') or die('Could not select database');
$query = "SELECT * FROM casestudies WHERE id = $id";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
$row = mysql_fetch_assoc($result);
echo "<h4>{$row['schoolname']}</h4>";
foreach ($row as $fld => $val)
    {
        echo '<p class="caseresult"><b>'  . $fld . '</b> : ' . $val . '</p>';
    }

?>

 

Ken

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.