Jump to content

[SOLVED] using PHP to echo SQL query


JsusSalv

Recommended Posts

Hello Everyone:

 

Can someone help me figure some code out?  I am attempting to dynamically generate an SQL query based on the page I am on.  Here's my code so far, which I logically think should work but doesn't:

 

 

// Create database connection by calling function [ dbConnect() ]

$conn = dbConnect($dbname);

 

// Prepare the SQL query.

$echoSQL = basename($_SERVER['SCRIPT_NAME']);

 

if ($echoSQL == 'index2.php') {$tablename = 'index';}

if ($echoSQL == 'information.php') {$tablename = 'information';}

if ($echoSQL == 'ex_libris.php') {$tablename = 'ex_libris';}

if ($echoSQL == 'events.php') {$tablename = 'events';}

if ($echoSQL == 'contact.php') {$tablename = 'contact';}

 

$sql = "'SELECT * FROM ".$tablename.'\';';

 

 

// Submit the query, capture the result, and store its reference within a variable ($result)

$result = mysql_query($sql) or die(mysql_error());

 

 

I've echoed the $sql variable and do recieve the proper syntax but nothing happens.  For example, if I'm on the webpage, information.php the code produces:

"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''SELECT * FROM information'' at line 1"

 

If you notice, 'SELECT * FROM information' is produced but somehow it doesn't work. 

 

Any clues on why it doesn't work or what I'm doing wrong?

 

Thank you everyone.

 

Hugo

Link to comment
https://forums.phpfreaks.com/topic/101933-solved-using-php-to-echo-sql-query/
Share on other sites

Change it to:

 

$sql = "'SELECT * FROM '$tablename'";

 

OR

 

$sql = "'SELECT * FROM \"$tablename\"";

 

If I were you, I'd rename index2.php to index.php, then change your code to something like this:

$tablename = basename($_SERVER['SCRIPT_NAME']);
$tablename = str_replace('.php', '', $echoSQL);  // remove .php from the string
$sql = "'SELECT * FROM \"$tablename\"";

 

 

 

Hello:

 

Yes, the code changes you provided did kick-ass! However, I wanted to keep this line: if ($echoSQL == 'XXXXX.php') {$tablename = 'XXXXX';} flexible enough so that if the table names or page names changed then the XXXXXX's could be replaced by anything a developer wanted.  The page name and table name are not dependent on each other, they can be completely different.  However, the table name would need to exist in the db (of course!).  Thank you for providing the help.  It allowed me to find the solution to this code block.  I've provided my final version (which really isn't all that different except for the way that $sql is prepared):

 

// Create database connection by calling function [ dbConnect() ]

$conn = dbConnect($dbname);

 

// Prepare the SQL query.

$echoSQL = basename($_SERVER['SCRIPT_NAME']);

if ($echoSQL == 'index2.php') {$tablename = 'index2';}

if ($echoSQL == 'information.php') {$tablename = 'information';}

if ($echoSQL == 'ex_libris.php') {$tablename = 'ex_libris';}

if ($echoSQL == 'events.php') {$tablename = 'events';}

if ($echoSQL == 'contact.php') {$tablename = 'contact';}

 

$sql = "SELECT * FROM $tablename";

 

 

// Submit the query, capture the result, and store its reference within a variable ($result)

$result = mysql_query($sql) or die('There was an issue with the database. Please contact the Webmaster');

 

 

If anyone is interested in getting more information you can contact me at [email protected]

 

 

Thank you everyone!

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.