mysql_query doesnt work in PHP

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
jade5357
Forum Newbie
Posts: 5
Joined: Thu Apr 30, 2009 4:25 am

mysql_query doesnt work in PHP

Post 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)
Last edited by Benjamin on Thu Apr 30, 2009 12:16 pm, edited 1 time in total.
Reason: Changed code type from text to php, sql.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: mysql_query doesnt work in PHP

Post by requinix »

Code: Select all

LIKE '%keyword%'
Is "keyword" supposed to be a PHP variable?
jade5357
Forum Newbie
Posts: 5
Joined: Thu Apr 30, 2009 4:25 am

Re: mysql_query doesnt work in PHP

Post 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
jade5357
Forum Newbie
Posts: 5
Joined: Thu Apr 30, 2009 4:25 am

Re: mysql_query doesnt work in PHP

Post by jade5357 »

thanks you very much.. its my first time here.. thank you!!
Post Reply