PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old February 22nd, 2004, 10:31 AM
aalowe9 aalowe9 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 5 aalowe9 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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

Reply With Quote
  #2  
Old February 23rd, 2004, 02:28 AM
Spongy's Avatar
Spongy Spongy is offline
Alternately High
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Location: Hilversum, Netherlands
Posts: 223 Spongy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 h 56 m 41 sec
Reputation Power: 15
Send a message via MSN to Spongy
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

Reply With Quote
  #3  
Old February 23rd, 2004, 05:50 AM
aalowe9 aalowe9 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 5 aalowe9 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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?

Reply With Quote
  #4  
Old February 23rd, 2004, 06:24 AM
Spongy's Avatar
Spongy Spongy is offline
Alternately High
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Location: Hilversum, Netherlands
Posts: 223 Spongy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 h 56 m 41 sec
Reputation Power: 15
Send a message via MSN to Spongy
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?

Reply With Quote
  #5  
Old February 23rd, 2004, 11:54 AM
aalowe9 aalowe9 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 5 aalowe9 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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

Reply With Quote
  #6  
Old February 23rd, 2004, 11:59 AM
Spongy's Avatar
Spongy Spongy is offline
Alternately High
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Location: Hilversum, Netherlands
Posts: 223 Spongy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 h 56 m 41 sec
Reputation Power: 15
Send a message via MSN to Spongy
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,

Reply With Quote
  #7  
Old February 23rd, 2004, 01:11 PM
Buster77 Buster77 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 28 Buster77 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
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?


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....

Reply With Quote
  #8  
Old February 23rd, 2004, 02:44 PM
aalowe9 aalowe9 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 5 aalowe9 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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?

Reply With Quote
  #9  
Old February 23rd, 2004, 03:19 PM
Buster77 Buster77 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 28 Buster77 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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!

Reply With Quote
  #10  
Old February 24th, 2004, 07:11 AM
ramji ramji is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 2 ramji User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Smile Hey try this code ....

PHP Code:
<html
<
head>
<
title>  </title>
</
head
<
body>
<?
php

$tableName 
$_GET["tableName"];
if(
$tableName != "")
{    
    
$dbcnx = @mysql_connect("localhost""root"""); 
    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="test.php?tableName=test">link</a>
</body> 
</html> 

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingPHP Development > php nightmare, plz help


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap