Query keeps failing

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
phpretard
Forum Commoner
Posts: 26
Joined: Wed Aug 01, 2007 8:03 pm
Location: Orlando Florida

Query keeps failing

Post by phpretard »

I know I am connecting to the database just fine but my query is failing.

Can someone figure out why based on the code below?


$con=mysql_connect('host',username,password);

@mysql_select_db($database) or die("<b>Unable to specified database</b>");


$query=mysql_query("SELECT email FROM testemail WHERE league= 'coach' ");
$result=mysql_query($query) or die('Error, query failed');

mysql_close($con);


...of course i have a table name "testemail" and a row in that table named "league" and another row named "email".

I am trying to send email to all the people with "coach" in the "league" $row.

[u]P[/u]ulling [u]H[/u]air [u]P[/u]ofusely
thiscatis
Forum Contributor
Posts: 434
Joined: Thu Jul 20, 2006 11:00 am

Post by thiscatis »

You should add [or die(mysql_error());] to your query, will give you a hint on what's wrong
phpretard
Forum Commoner
Posts: 26
Joined: Wed Aug 01, 2007 8:03 pm
Location: Orlando Florida

Here is what it read

Post by phpretard »

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 'Resource id #2' at line 1

But "coach is at the row called "id" #1 not #2

I am not sure if what I just wrote makes sence.
User avatar
robshanks
Forum Commoner
Posts: 32
Joined: Sun Aug 05, 2007 9:27 pm
Location: Hull, UK

Post by robshanks »

What is the error you get?
You can try
Are username and pasword variables(1) or do you want to send username and password literally(2)?
If 1 then they need to be entered as $username and $password
Try:

Code: Select all

$con=mysql_connect("host","username","password");  //(1)send strings directly  OR
  $con=mysql_connect($host,$username,$password);  //(2)send  variables 
  if (!$con)
  {
    die('Could not connect :' . mysql_error());
  }
   $db=mysql_select_db($database,$con);
   if (!$db)
   {
    die('Could not open databse:' . mysql_error());
  }
  $query=mysql_query("SELECT email FROM testemail WHERE league= 'coach' ");
  $result=mysql_query($query);
  if (!$result)
  {
    die('Error, query failed' . mysql_error());
  }
  mysql_close($con);
thiscatis
Forum Contributor
Posts: 434
Joined: Thu Jul 20, 2006 11:00 am

Post by thiscatis »

You're not getting the results but just the resource id.
Please provide us the code where you get 'coach'
Which should also be '$coach' for a start
phpretard
Forum Commoner
Posts: 26
Joined: Wed Aug 01, 2007 8:03 pm
Location: Orlando Florida

Post by phpretard »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Is this what you're lookin for?

Code: Select all

$league=$_POST['league'];

$query=mysql_query("SELECT email FROM testemail WHERE league='$league' ");
$result=mysql_query($query) or die(mysql_error());

mysql_close($con);

<---------------------<html form>

Code: Select all

<form action=this.php method=POST>

<select name="league" size="1">
	<option>select...</option>
	<option value="instructional">Instructional</option>
	<option value="rookie">Rookie</option>
	<option value="parent">Parent</option>
	<option value="coach">Coach</option>
</select>

</form>

</form>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
phpretard
Forum Commoner
Posts: 26
Joined: Wed Aug 01, 2007 8:03 pm
Location: Orlando Florida

Post by phpretard »

"SELECT email FROM testemail" <====This sends an email to everyone succesfully.



"SELECT email FROM testemail WHERE league=$league" <====This fails the query


I want to select just the coaches
phpretard
Forum Commoner
Posts: 26
Joined: Wed Aug 01, 2007 8:03 pm
Location: Orlando Florida

Post by phpretard »

Thank you for all of your help!!!!

It's working now.

-Anthony
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Did no one notice that he was trying to run the result of a query as a query...?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

superdezign wrote:Did no one notice that he was trying to run the result of a query as a query...?
I was just going to ask that :?
phpretard
Forum Commoner
Posts: 26
Joined: Wed Aug 01, 2007 8:03 pm
Location: Orlando Florida

Post by phpretard »

This actually works now (happy me)

Is there any way you folks might look at the topic I posted regarding check boxes?

It's called "Check the box"

Thanky
Post Reply