Re: SQL Select?
| From: | Jason Stechschulte | Date: | Tue, 27 Mar 2001 12:59:53 +0000 |
| Subject: | Re: SQL Select? | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-7870@lists.php.net to get a copy of this message | ||
On Sat, Mar 24, 2001 at 08:55:13PM -0000, Grant wrote:
> Is there any way of using a string in an SQL query instead of using the
> table name. Something along the lines of
>
> $result=mysql_query("SELECT * FROM $tablename",$db);
>
> this doesnt work it come up with a parse error
Your problem is probably on a line above this one. Because this line
should be valid. I do stuff like this all the time and it works
without any problems so far.
<?php
// First of all, I use a variable to hold my sql statement:
$sql = "select * from $tablename";
// Then perform the query:
$result = mysql_query($sql, $db) or die("$sql<br>".mysql_error($db));
?>
This allows you to see the sql that is actually being executed, and you
also get to see the actual mysql error.
--
Jason Stechschulte
jpstech@unoh.edu
--
It is, of course, written in Perl. Translation to C is left as an
exercise for the reader. :-) -- Larry Wall in <7448@jpl-devvax.JPL.NASA.GOV>