Welcome to Dream.In.Code
Become a PHP Expert!

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




sql query problem

 
Reply to this topicStart new topic

sql query problem, query doesnt work

xasthur
9 Jan, 2008 - 04:54 AM
Post #1

New D.I.C Head
*

Joined: 3 Jan, 2008
Posts: 5


My Contributions
hello all.
Im really new to SQL but have some knowledge of PHP.
I was writing some very simple code to pick some records from a DB. I use phpMyAdmin.
here is my code:
CODE

<?php
    $searchTerm=(int)trim($_POST['st2']);              //searchTerm and searchType are coming from a
    $searchType=trim($_POST['st']);                  // basic html form where for example
                                                                                  //searchType is id field (or name) and searchTerm
                                                                                  // is corresponding name or id      
    
            @$db=mysql_connect("localhost","root","");        
    if(!$db) {
        echo "Error";
        exit;
    }
    else {
        mysql_select_db("adventuregame",$db);            
        $query="SELECT * FROM heroes WHERE ".$searchType." LIKE ".$searchTerm;    
        $result=mysql_query($query,$db);            
        
        echo "<table border=1>";    
        //get row data as an associative array
        while($row=mysql_fetch_assoc($result))
        {
            echo "<tr>";
            foreach($row as $col=>$value)
                echo "<td>$value</td>";
            echo "</tr>";
        }
        echo "</table>";    
    }

?>

it works OK for id field ( when i write id and 2 for example) but not for name. btw id is stored as integer in DB and name is as string. I guess the problem is in query.
I also tried to erase the cast in searchTerm,it still works for id..but still doesn't work for name. Gives a warning saying "Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in ...\search.php on line 18" and doesn't write anything on the browser. Any ideas and corrections will be appreciated. Thanks.

User is offlineProfile CardPM
+Quote Post

no2pencil
RE: Sql Query Problem
9 Jan, 2008 - 05:09 AM
Post #2

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 6,550



Thanked: 67 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
For each item returned from $query="SELECT * FROM heroes WHERE ".$searchType." LIKE ".$searchTerm; will be in your array $row[]. Rather than pulling *, try pulling simply the results you want.
CODE

$query="SELECT column1, column2 FROM heroes WHERE ".$searchType." LIKE ".$searchTerm;
        while($row=mysql_fetch_assoc($result)) {
            echo "<tr>";
            echo "<td>$row[0]</td>"; // Column1
            echo "<td>$row[1]</td>"; // Column2
            echo "</tr>";
        }
        echo "</table>";    

I would only use a for loop if you know how many * is going to return. If you can somehow count ahead of the search how many items are in the database, when you SELECT * you'll know how many times to run your for loop.
User is offlineProfile CardPM
+Quote Post

xasthur
RE: Sql Query Problem
9 Jan, 2008 - 05:25 AM
Post #3

New D.I.C Head
*

Joined: 3 Jan, 2008
Posts: 5


My Contributions
QUOTE(no2pencil @ 9 Jan, 2008 - 03:09 PM) *

For each item returned from $query="SELECT * FROM heroes WHERE ".$searchType." LIKE ".$searchTerm; will be in your array $row[]. Rather than pulling *, try pulling simply the results you want.
CODE

$query="SELECT column1, column2 FROM heroes WHERE ".$searchType." LIKE ".$searchTerm;
        while($row=mysql_fetch_assoc($result)) {
            echo "<tr>";
            echo "<td>$row[0]</td>"; // Column1
            echo "<td>$row[1]</td>"; // Column2
            echo "</tr>";
        }
        echo "</table>";    

I would only use a for loop if you know how many * is going to return. If you can somehow count ahead of the search how many items are in the database, when you SELECT * you'll know how many times to run your for loop.


That didn't work. Now im getting that warning even when im using id field to search. Actually i want to get the whole columns from the DB, thats why i used a foreach loop. I need all the properties of what im searching for and print the result as a table of one row and columns.
User is offlineProfile CardPM
+Quote Post

darklighter
RE: Sql Query Problem
9 Jan, 2008 - 11:34 AM
Post #4

New D.I.C Head
*

Joined: 5 Jan, 2008
Posts: 18


My Contributions
Sometimes the fieldname "name" causes problems with queries. It's not a reserved word, but i've encountered problems with it before. Try renaming the fieldname to something else like "itemname"


User is offlineProfile CardPM
+Quote Post

xasthur
RE: Sql Query Problem
9 Jan, 2008 - 11:56 AM
Post #5

New D.I.C Head
*

Joined: 3 Jan, 2008
Posts: 5


My Contributions
ok i also have a field named "power" i've tried that instead of "name"..still get the same error...i guess its about the query..i've manipulated the query tho many times but still didn't work. actually im clueless at this point.

User is offlineProfile CardPM
+Quote Post

no2pencil
RE: Sql Query Problem
9 Jan, 2008 - 12:30 PM
Post #6

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 6,550



Thanked: 67 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
QUOTE(xasthur @ 9 Jan, 2008 - 12:56 PM) *

ok i also have a field named "power" i've tried that instead of "name"..still get the same error...i guess its about the query..i've manipulated the query tho many times but still didn't work. actually im clueless at this point.

Can you echo out the query to visually verify that it is what you intend to send to mysql? Also try testing (cut & paste) that query on the command line.

It is possible that the query simply isn't returning anything.
User is offlineProfile CardPM
+Quote Post

ahmad_511
RE: Sql Query Problem
9 Jan, 2008 - 01:45 PM
Post #7

D.I.C Regular
Group Icon

Joined: 28 Apr, 2007
Posts: 351



Thanked: 8 times
Dream Kudos: 400
My Contributions
QUOTE

$query="SELECT column1, column2 FROM heroes WHERE ".$searchType." LIKE ".$searchTerm;
while($row=mysql_fetch_assoc($result)) {
echo "<tr>";
echo "<td>$row[0]</td>"; // Column1
echo "<td>$row[1]</td>"; // Column2
echo "</tr>";
}
echo "</table>";


I don't know if this is what causing the problem but.
don't we using the field name when using fetch_assoc ?, something like this:
CODE

echo "<td>$row['column1']</td>"; // Column1

User is offlineProfile CardPM
+Quote Post

xasthur
RE: Sql Query Problem
9 Jan, 2008 - 02:24 PM
Post #8

New D.I.C Head
*

Joined: 3 Jan, 2008
Posts: 5


My Contributions
Ok, problem solved!
actually...the common error was a echoing the $value variable in the foreach loop, i should try '<td>$value</td>' or "<td>.$value.</td>" instead. and i should write the query as $query="SELECT * FROM heroes WHERE ".$searchType." LIKE '".$searchTerm."'" i.e put the single quotes around $searchTerm because when i was querying name(or power like i said) it was searching for a string( the values are stored as string besides id field in DB) it also works for id tho, i guess mySQL does the conversion implicitly?
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/5/08 04:44AM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month