[nycphp-talk] Is there something wrong with this SQL query in PHP?

Anthony Wlodarski aw at
Tue Aug 14 15:44:25 EDT 2007

It does evaluate to true every time, as I wouldn't get to the debug echo
statement in the application.  I put it in its own script and added a few
lines, here is the test script:



// our global database handle

// Always get a database handle

$dbh=mysql_connect ("localhost", "root", "plasticpen5") or die ('I cannot
connect to the database because: ' . mysql_error());

mysql_select_db ("jobsdb");


$query = 'SELECT * FROM `jobsdb` WHERE `id` =1';

$result = mysql_query($query) or die("Error editing data.");

$row = mysql_fetch_assoc($result, MYSQL_ASSOC);


echo $result."</br>";

echo "Debug data</br>";

echo "id ".$row['id']."";


// close our database handle



I gave it the id of 1 because I know it does exist in the database, this is
the output:


Resource id #3
Debug data


Nothing in $row and since I have never echoed a raw result ($result) from
the query can anyone explain what it might possibly be?






From: talk-bounces at [mailto:talk-bounces at] On
Behalf Of Brian O'Connor
Sent: Tuesday, August 14, 2007 3:26 PM
To: NYPHP Talk
Subject: Re: [nycphp-talk] Is there something wrong with this SQL query in


Hmm, have you tried outputting the results of $_POST['editjob']?  It might
not be set to something that evaluates to true, which would cause none of
the information you're looking for to be processed.

On 8/14/07, Anthony Wlodarski <aw at> wrote:

I have been staring at this for about a week and it is turning into a mind
bender.  Here is the code excerpt:


// pre: none

// post: displays our controls for .

function displayControls()




                $query = "SELECT * FROM `jobsdb` WHERE `id`

                $result = mysql_query($query) or die("Error editing data.");

                $row = mysql_fetch_assoc($result, MYSQL_ASSOC);


                echo "Debug: ".$_POST['id']." - ".$row['id']."\n";


                // display all our form data

                echo "<fieldset style='color: #ffffff;'><legend>Edit a

                                "<form action='".$_SERVER['PHP_SELF']."'

                                "Recruiter: <input name='recruiter'
type='text' value='".$row['recruiter']."'></br>".

                                " Job ID: <input name='jobid' type='text'

                                " Position: <input name='position'
type='text' value='".$row['position']."'></br>".

                                " Position Link: <input name='link'
type='text' value='".$row['link']."'></br>".

                                " Additional Info: <input
name='additionalinfo' type='text' value='".$row['additionalinfo']."'></br>".

                                " Salary: <input name='salary' type='text'

                                " Hiring Manager: <input
name='hiringmanager' type='text' value='".$row['hiringmanager']."''></br>".

                                " Company: <input name='company' type='text'

                                " Status: <input name='status' type='text'

                                " Postings: <input name='postings'
type='text' value='".$row['postings']."'></br>".

                                " Job Color: \n".

                                "<select name='color'>\n".

                                "<option value='#ff0000'>Red</option>\n".

                                "<option value='#ffa500'>Orange</option>\n".

                                "<option value='#ffff00'>Yellow</option>\n".

                                "<option value='#00ff7f'>Spring

                                "<option value='#32cd32'>Lime

                                "<option value='#c0c0c0'>Silver</option>\n".

                                "<option value='#0000cd'>Medium

                                "<option value='#191970'>Midnight

                                "<option value='#4169e1'>Royal

                                "<option value='#c71585'>Medium Violet



                                "<input name='saveedit' type='submit'
value='Save Edit'>".

                                "<input name='cancel' type='submit'





... Rest of application.


Now the big problem are the three PHP statements after the "if" statement.
When I click edit job nothing appears even in the debug statement, I am not
returning a valid id from $row['id'] nor does any of the data appear in the
form(which is just an integer).  I have scrutinized the SQL query a million
times, even tried to qualify it with the database name using
`foobar`.`jobsdb` and that still not do anything.  I know the $_POST['id']
variable is passing the right id based on the echo command.  Is this a SQL
issue or a PHP issue?  I have looked at my databases in phpMyAdmin and even
checked them using the command line on our box and they have the right
permissions for the users that can access them.  If this is  a SQL issue can
you guys point me in the right direction?  I was thinking of pulling my data
base handle code and the SQL queries and just throwing them into a bare
script to see what happens.  If anyone needs to see the HTML output I can
get that as well.


Any help is appreciated.





New York PHP Community Talk Mailing List

NYPHPCon 2006 Presentations Online  <> 

Show Your Participation in New York PHP 

Brian O'Connor 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the talk mailing list