Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 132,463 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,386 people online right now. Registration is fast and FREE... Join Now!




php/mysql SELECT help

2 Pages V  1 2 >  
Reply to this topicStart new topic

php/mysql SELECT help, Help please

Adsa
post 21 Apr, 2005 - 08:59 PM
Post #1


New D.I.C Head

*
Joined: 21 Apr, 2005
Posts: 27


My Contributions


I am an absolute beginner at PHP and MySql so please be kind.

My problem is I am trying to pass on some information using a form and use this information to isolate the details required using the SELECT command. I know that I am getting the information from my form but am unsure on how to retrieve the necessary data from the database.

CODE
$sql = 'SELECT * '
       . ' FROM `clan_name` '
       . ' WHERE 1 AND `clan_name` '
       . ' LIKE \'Dr\' LIMIT 0, 30';


Where I have "Dr" i wish to replace this with the selected information from my form.

Help please.

Thanks
User is offlineProfile CardPM

Go to the top of the page

DanceInstructor
post 21 Apr, 2005 - 10:16 PM
Post #2


New D.I.C Head

Group Icon
Joined: 18 Mar, 2005
Posts: 41



Dream Kudos: 25
My Contributions


Hmm lets say in your form you have a select box with clan names, something like:

CODE

<select name="clan_name">
<option value="Agent">Agents
<option value="Fear">Fear This
</select>


Then in your script you might do this:

CODE

$clan_name = $_POST['clan_name'];
$sql = "SELECT *
       FROM `clan_name`
       WHERE 1 AND `clan_name`
       LIKE $clan_name LIMIT 0, 30";


Note the use of ". You must use double quotes when using variables in strings, if you use single quotes php assumes its all text and does not look for variables.
User is offlineProfile CardPM

Go to the top of the page

cyberscribe
post 21 Apr, 2005 - 10:25 PM
Post #3


humble.genius

Group Icon
Joined: 5 May, 2002
Posts: 1,062



Thanked 2 times

Dream Kudos: 154
My Contributions


ditch the
CODE

1 AND

...it's useless. smile.gif
User is offlineProfile CardPM

Go to the top of the page

Adsa
post 21 Apr, 2005 - 10:52 PM
Post #4


New D.I.C Head

*
Joined: 21 Apr, 2005
Posts: 27


My Contributions


Thanks for that, but for some reason I get this now

CODE
Unknown column 'Dr' in 'where clause'


At least now I am getting the variable into the statment. smile.gif
User is offlineProfile CardPM

Go to the top of the page

DanceInstructor
post 21 Apr, 2005 - 11:28 PM
Post #5


New D.I.C Head

Group Icon
Joined: 18 Mar, 2005
Posts: 41



Dream Kudos: 25
My Contributions


Post the code you used please. We may need a peek at your form code as well, not sure. Lets also clarify the structure of your database. It sounds like the sql statement has a value in the wrong place though.
User is offlineProfile CardPM

Go to the top of the page

Adsa
post 22 Apr, 2005 - 02:02 AM
Post #6


New D.I.C Head

*
Joined: 21 Apr, 2005
Posts: 27


My Contributions


Here is the code I used for MySql minus the mysql_connect stuff smile.gif

CODE

<?php
$clan_select = $_POST['clan_name'];
$sql = "SELECT *
      FROM `clan_name`
      WHERE `clan_name`
      = $clan_select";
while ($newArray = mysql_fetch_array($result)) {
    $id  = $newArray['id'];
    $clan_name = $newArray['clan_name'];
               $challenging_clan = $newArray['challenging_clan'];
    echo "$clan_name<br>"
               echo "$challenging_clan <br>";
}
?>


and here is an extract of the code I used for the form.

CODE

<p><strong>Select a clan</strong></p>
<select name="clan_name" >
<option value="Dr" >Dr</option>
<option value="Blackwidow" >Blackwidow</option>
<option value="Redback" >Redback</option>
<option value="THE" >THE</option>
</select>
<p><input type="submit" value="send"></p>


and finally here is the structure of the database.

CODE

id   int(40)
clan_name   varchar(100)
challenging_clan   varchar(100 )
challenge_score   int(40)
defender_score   int(40)  
date   datetime No  0000-00-00 00:00:00  
map   varchar(100)
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 22 Apr, 2005 - 05:18 AM
Post #7


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,173



Thanked 33 times

Dream Kudos: 25
My Contributions


try
CODE

$sql = "SELECT *
     FROM `clan_name`
     WHERE `clan_name`
     = '".$clan_select."'";

Please note that after the equal sign, there is a single quote, then double quotes. Similarly, after the final concatenator, there is a set of double quotes, a single quote, and then double quotes again.
User is offlineProfile CardPM

Go to the top of the page

DanceInstructor
post 22 Apr, 2005 - 12:38 PM
Post #8


New D.I.C Head

Group Icon
Joined: 18 Mar, 2005
Posts: 41



Dream Kudos: 25
My Contributions


Actually he shouldn't have to concatenate the string. That is the whole point of using double quotes in php, so you can insert variables straight into a string without extra syntax. So this should work: (but then again you never know tongue.gif )

CODE
$sql = "SELECT * FROM `clan_name` WHERE `clan_name` = $clan_select";


My next question is: You gave us the field names of a table in your database. What is the table name? If it is the same as a field in the table I suggest you change it to something else to eliminate possible confusion. Now lets look at the sql statement again.

CODE
$sql = "SELECT * FROM `clan_name` WHERE `clan_name` = $clan_select";


means (this is not code, just for explanation):

$sql = "SELECT allvalues FROM tablename WHERE fieldname isexactlythesameas $clan_select";

So you might wanna go back to using LIKE in case the value in the database is not exactly the same as the value assigned in the form. Also lets make sure about the table name.

PS READ THIS:
I just noticed that I don't see where you actually made your query in the code you supplied. Do you actually have:

CODE
$result = mysql_query($sql);


in your code? Because it definitly won't work if you don't execute the query.

This post has been edited by DanceInstructor: 22 Apr, 2005 - 12:45 PM
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 22 Apr, 2005 - 12:42 PM
Post #9


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,173



Thanked 33 times

Dream Kudos: 25
My Contributions


Yeah, the only reason I suggested concatenating the strings was due to his error message. It seems clear that 'Dr' is being interpreted as a column name, not as a string variable.
QUOTE

PS READ THIS:
I just noticed that I don't see where you actually made your query in the code you supplied. Do you actually have:

CODE
$result = mysql_query($sql);


in your code? Because it definitly won't work if you don't execute the query.

Good spot, never even noticed myself... smile.gif
User is offlineProfile CardPM

Go to the top of the page

Adsa
post 22 Apr, 2005 - 03:09 PM
Post #10


New D.I.C Head

*
Joined: 21 Apr, 2005
Posts: 27


My Contributions


QUOTE(Amadeus @ Apr 22 2005, 06:18 AM)
try
CODE

$sql = "SELECT *
     FROM `clan_name`
     WHERE `clan_name`
     = '".$clan_select."'";

Please note that after the equal sign, there is a single quote, then double quotes. Similarly, after the final concatenator, there is a set of double quotes, a single quote, and then double quotes again.


Thank you very much that works a treat. biggrin.gif

QUOTE(DanceInstructor @ Apr 22 2005, 01:38pm)
PS READ THIS:
I just noticed that I don't see where you actually made your query in the code you supplied. Do you actually have:


CODE
 
$result = mysql_query($sql);




in your code? Because it definitly won't work if you don't execute the query.


Ops My mistake, in copying and pasting I accidently left it out.

Thanks Amadeus and DanceInstructor for your help. biggrin.gif

This post has been edited by Adsa: 22 Apr, 2005 - 03:10 PM
User is offlineProfile CardPM

Go to the top of the page

skyhawk133
post 22 Apr, 2005 - 03:10 PM
Post #11


Head DIC Head

Group Icon
Joined: 17 Mar, 2001
Posts: 14,844



Thanked 45 times

Dream Kudos: 1650

Expert In: Web Development

My Contributions


Thanks guys for helping out, and welcome to dream.in.code Adsa!
User is offlineProfile CardPM

Go to the top of the page

cyberscribe
post 22 Apr, 2005 - 07:41 PM
Post #12


humble.genius

Group Icon
Joined: 5 May, 2002
Posts: 1,062



Thanked 2 times

Dream Kudos: 154
My Contributions


QUOTE(Adsa @ Apr 22 2005, 04:09 PM)
Thanks Amadeus and DanceInstructor for your help. biggrin.gif

And me. I optimized your query by 1/100000ms at least. smile.gif
User is offlineProfile CardPM

Go to the top of the page

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 11/22/08 01:27PM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month