|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
Free Web 2.0 Code Generator! Generate data entry and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!
|
|
#1
|
|||
|
|||
|
php nightmare, plz help
Hello, i am trying to do something that i think is probly quite simple but i dont know php so well,
i want to refresh a php page but sending a different variable when a button is clicked, here is the code Code:
<html>
<head>
<title> </title>
</head>
<body>
<?php
$tableName = $_GET["tableName"];
$dbcnx = @mysql_connect("localhost", "xxxxxx", "xxxxxx");
if (!$dbcnx) {
echo( "<p>Unable to connect to the database server at this time.</p>"
);
exit();
}
if (! @mysql_select_db("aalowe93_test") ) {
echo( "<p>Unable to locate the database at this time.</p>" );
exit();
}
$result = @mysql_query("SELECT JokeText FROM $tableName");
if (!$result) {
echo("<p>Error performing query: " . mysql_error() . "</p>");
exit();
}
while ( $row = mysql_fetch_array($result) ) {
echo("<p>" . $row["JokeText"] . "</p>");
}
?>
<a href="index.php?tableName='test'">link</a>
</body>
</html>
This page is saved as index.php, With the link at the bottom i am trying to recall the page and send the variable tableName by the GET method, however this code doesnt work. When the page is run without using a variable for the table name it works with no problems, the only problem is trying to pass a variable as the name of the table. Please help if you can because this is driving me mad |
|
#2
|
||||
|
||||
|
Try taking off the quotes in your link, like this:
<a href="index.php?tableName=test">link</a> Think that does the trick...
__________________
Work to live, don't live to work |
|
#3
|
|||
|
|||
|
re
Thanks for the reply, i tried taking the quotes off the link but i still get the message
"Error performing query: 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 '' at line 1" If i change the line "$result = @mysql_query("SELECT JokeText FROM $tableName");" and remove the variable $tableName and just put back the name of the table 'test' it works fine, any idea what the error could be? |
|
#4
|
||||
|
||||
|
What happens when you do this:
$result = mysql_query("SELECT JokeText FROM '$tableName';") or die(mysql_error()); // Without @ and with quotes around $tableName What version of MySQL are you working with? ![]() |
|
#5
|
|||
|
|||
|
re
Im using MySQL 4.0.15 and when i tried different variations of using quotes and the @ i still keep getting a syntax error
message. Is what im doing legal? can u use variables in query strings like ive done? i just cant see whats up with it, it works fine until u put a $ into the query. Any1 have any ideas? would really really appreciate some help |
|
#6
|
||||
|
||||
|
Strange...
Yes it is legal, I do it all the time! Hmm, I hope some1 else got an idea... I don't know anymore I think... Cheers, |
|
#7
|
|||
|
|||
|
Quote:
I'm not an expert on PHP, although I'm trying to learn it right now. The fact that when you replace the absolute name (ie. "test") with a variable the script no longer works is a bit suspicious, though. I have done extensive work with MySQL and C++ lately, and I found that the best way to use a variable in a query was to set the query up separately. Again, I'm not an expert with PHP, but this? $query = "SELECT JokeText FROM "; $query .= $tableName; $result = mysql_query($query) or die (mysql_error()); Not really sure if my syntax within the query is correct. I've found with C++ I often have to use the single quotes within a query but NEVER the semi-colon. As far as I know, though, this should also work... $query = "SELECT JokeText FROM '$tableName'"; $result = mysql_query($query) or die (mysql_error()); Note, single quotes around the variable, but no semi-colon in the query string.... |
|
#8
|
|||
|
|||
|
re
Thanks for the reply, i tried both methods but still getting exactly the saame problem, could the error be in the way im
passing the variable $tableName back to the page? i have no idea why this thing wont work... any1? |
|
#9
|
|||
|
|||
|
At this point, I would output the value of $tableName and make sure it is what you want it to be. Since the error seems to be syntax rather than "this table does not exist", maybe the variable is not getting stored in the form you expect. Maybe, for example, it is stored as 'test', with the single quotes. That would account for your error. You also might try printing out the value of $query, and checking that as well.
Good Luck! |
|
#10
|
|||
|
|||
|
PHP Code:
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > PHP Development > php nightmare, plz help |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|