$_GET used in URL to select MySQL table - fetch array error
Posted: Mon Sep 26, 2011 5:26 pm
Good evening,
I'm using PHP and MySQL to code up my new site, however difficulties beyond my knowledge or something small I have missed are causing nightmares!
I have used the $_GET function to catch the name of the specified MySQL table in the URL.... For example: index.php?table=home. The code below is what i'm using:
In order to troubleshoot this I inserted an echo $content function which simpley kept displaying "404" whatever the passed value. This makes me think there is a problem with the above code not capturing the correct value. Just to be sure I've inserted the code below in which I'm using to display the contents of the specified table.
Note: If I remove the $content variable from the SQL line and add the actual table name the text is displayed as stored in the table correctly.
Ignoring the echo $content function the page gives me the "Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /wwwroot/...../index.php on line 59" error.
Can anyone point me in the right direction here?
Many thanks,
Jim
I'm using PHP and MySQL to code up my new site, however difficulties beyond my knowledge or something small I have missed are causing nightmares!
I have used the $_GET function to catch the name of the specified MySQL table in the URL.... For example: index.php?table=home. The code below is what i'm using:
Code: Select all
<?php
if (!$_GET["table"]) {
$_GET["table"] = "home";
}
$content = $_GET["table"];
if (!file_exists($content)) {
$content = "404";
}
?>Code: Select all
<?php
echo '<font face="Verdana, Arial, Helvetica, sans-serif">' . $content .'</font>';
require_once("config/db.php");
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");
mysql_select_db($db) or die ("Unable to select database!");
$sql = "select `id`, `context` from $content";
$rslt = mysql_query($sql);
while ($result = mysql_fetch_array($rslt) )
{
$output = wordwrap($result['context'], 100);
echo '<font face="Verdana, Arial, Helvetica, sans-serif">' . $output .'</font>';
}
?>Ignoring the echo $content function the page gives me the "Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /wwwroot/...../index.php on line 59" error.
Can anyone point me in the right direction here?
Many thanks,
Jim