Error in Query?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
ip2g
Forum Newbie
Posts: 4
Joined: Mon Feb 09, 2009 6:28 pm

Error in Query?

Post by ip2g »

I honestly cannot find out what is wrong with this code. I'm fairly new at PHP so the variable $i call may be wrong, I'm not sure.

Code: Select all

 
<?
            $dbhost = 'localhost';
            $dbuser = 'ip2g_*****';
            $dbpass = '*****';
            $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
            $dbname = 'ip2g_*****';
            mysql_select_db($dbname) or die('Could not connect to Database');
            for ($i=1;$i<=3;$i++)
            {
                $query = "SELECT * FROM Posts WHERE postID='$i'";
                $result = mysql_query($conn,$query)
                     or die ("Couldn't Execute Query.");
                     $row = mysql_fetch_assoc($result);
                     $postTitle = $row['postTitle'];
                     $postDate = $row['postDate'];
                     echo " <li class='menu'>
                            <a href='about.html'>                         
                                <span class='name'>" . $postTitle . "</span>
                                <span class='comment'>" . $postDate . "</span>
                                <span class='arrow'></span>
                            </a>
                        </li>";
            }
        ?>
 
The error I get is
Couldn't Execute Query
... which obviously means the query is wrong. I have 3 entries in the table Posts, and I have made sure the table is spelled correctly in both the mySQL and on this code. Am I calling the variable "$i" incorrectly? How can I fix this?

A quick help would be much appreciated ;), and remember, I'm new to PHP :P

Oh and this is my first post, hopefully not my last. So hi!
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Error in Query?

Post by John Cartwright »

Try changing

Code: Select all

 
$result = mysql_query($conn,$query) or die ("Couldn't Execute Query.");
to

Code: Select all

 
$result = mysql_query($conn,$query);
if (!$result) {
   echo 'Failed on: '. $query .'<br />';
   echo 'Error: '. mysql_error();
   exit();
}
for a more descriptive idea of whats going on. It is generally good practice to echo the query out, and always include mysql erorr reporting.
ip2g
Forum Newbie
Posts: 4
Joined: Mon Feb 09, 2009 6:28 pm

Re: Error in Query?

Post by ip2g »

Alright good to know.

This is the error now:

Failed on: SELECT * FROM Posts WHERE postID='1'
Error:
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Error in Query?

Post by John Cartwright »

The query looks fine (although it is not neccesary to quote integers).

The odd thing about it is that the mysql_query() will only return false on an error, however, mysql_error() is not reporting an error.

Just as a sanity check, try changing it to this:

Code: Select all

$result = mysql_query($conn,$query) or die (mysql_error());
Last edited by John Cartwright on Mon Feb 09, 2009 7:04 pm, edited 1 time in total.
Reason: sp
ip2g
Forum Newbie
Posts: 4
Joined: Mon Feb 09, 2009 6:28 pm

Re: Error in Query?

Post by ip2g »

I thought it was weird too. Maybe for some reason it's my host, I don't know why though.

Anyway I changed it to that and again no error code... this is quite the pickle, isn't it :P.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Error in Query?

Post by John Cartwright »

I just realized you had your parameters backwards.

Code: Select all

$result = mysql_query($query,$conn) or die(mysql_error());
 
instead.

Also, do you have mroe than 1 mysql connection open during this script's execution?
ip2g
Forum Newbie
Posts: 4
Joined: Mon Feb 09, 2009 6:28 pm

Re: Error in Query?

Post by ip2g »

Yes it works!

And yes I did have more than one mySQL connection, I didn't think that would be a problem. Thanks a billion, you were a huge help!
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Error in Query?

Post by John Cartwright »

Ah yes that would explain why mysql_error() wasn't returning anything. If you do not pass the appropriate connection resource, mysql_error() will use the last opened connection.

Should have been mysql_error($conn)
Post Reply