Page 1 of 1

mysql_query doesnt work in PHP

Posted: Thu Apr 30, 2009 4:39 am
by jade5357
hello. i was wondering if you could help me with this code.
this searches for the name of the person using the keyword

Code: Select all

 
<?php
                
                $sql = mysql_query("SELECT DISTINCT personID, personFirstName, personMiddleInitial, personLastName
                                FROM person join student
                                ON person.personid = student.studentID
                                WHERE personFirstName LIKE '%keyword%' OR personMiddleInitial LIKE '%keyword%' OR personLastName LIKE '%keyword%'");
                        
                if(mysql_num_rows($sql)==0)
                {echo "<tr> <td colspan=\'2'\ style=\"width: 40em;\"> No records found! </td> </tr>";}
            
                else
                {
                    while($row=mysql_fetch_array($sql))
                    {
                        $firstname = $row['personFirstName'];
                        $lastname = $row['personLastName'];
                        $middleinitial = $row['personMiddleInitial'];
                        $pid = $row['personID'];
                        echo "<tr>";
                        echo "<td> ". $firstname . " " . $middleinitial . " " . $lastname . "</td>";
                        echo "<td> <a href=\"viewstudentaccountabilities.php?pid=$pid\" style=\"color:#0066cc;\"> View Accountabilities </td>";
                        echo "</tr>";
                    }
                }
            ?>
 
ive checked the keyword entered by the user and its seems fine.. the only problem i that whe I execute this code

Code: Select all

SELECT DISTINCT personID, personFirstName, personMiddleInitial, personLastName
                                FROM person JOIN student
                                ON person.personid = student.studentID
                                WHERE personFirstName LIKE '%keyword%' OR personMiddleInitial LIKE '%keyword%' OR personLastName LIKE '%keyword%'
 
in the MYSQL console, it works..
But when I run it in the php file, it always returns 0 results..
I removed once the mysql_num_rows to see if it causes the error but no, it still returns 0 results... what could be the problem with this code..
btw, im using wampserver 2.0e.. (php 5.2.7 and mysql 5.1.30)

Re: mysql_query doesnt work in PHP

Posted: Thu Apr 30, 2009 4:42 am
by requinix

Code: Select all

LIKE '%keyword%'
Is "keyword" supposed to be a PHP variable?

Re: mysql_query doesnt work in PHP

Posted: Thu Apr 30, 2009 4:44 am
by jade5357
yes.. its a variable.. OMG.. im so sorry.. i forgot to put the '$' sign.. *sigh* after watching the code for one hour... i didn't even notice that

Re: mysql_query doesnt work in PHP

Posted: Thu Apr 30, 2009 4:45 am
by jade5357
thanks you very much.. its my first time here.. thank you!!