0

can some one point out the problem with this code? It supposed to fetch data from mysql but it returns blank. Here is the full code.

<ul class="list">

<?php

require("object/db.class.php");

error_reporting(0);

function entry_tickets() {

if($_SESSION['dojopress_global:root'] == false) {

$entry_ticket .= <<<ENTRY_TICKET
<li><p><img src="http://cdn1.iconfinder.com/data/icons/humano2/32x32/apps/gnome-keyring-manager.png" />Access denied</p></li>
ENTRY_TICKET;

} elseif($_SESSION['dojopress_global:root'] == true) {

$q = "SELECT * FROM `notice` ORDER BY nid LIMIT 12 DESC";
$r = mysql_query($q);

if ( mysql_num_rows($r) == 0 ) {

$entry_ticket .= <<<ENTRY_TICKET
<li><p><img src="http://cdn1.iconfinder.com/data/icons/humano2/32x32/status/dialog-information.png" /> Nothing to display</p></li>
ENTRY_TICKET;

} elseif ( $r !== false && mysql_num_rows($r) > 0 ) {
    while ( $a = mysql_fetch_array($r) ) {

        $nid = stripslashes($a['nid']);
        $note = stripslashes($a['note']);
        $type = stripslashes($a['type']);
        $private = stripslashes($a['private']);
        $date = stripslashes($a['date']);
        $author = stripslashes($a['author']);


    function note_type($type) {

       if($type == 1) { $type = "posted a comment!"; } elseif($type == 2) { $type = "raised a support ticket!"; } else { }
       return ($type);
    }

$entry_ticket .= <<<ENTRY_TICKET
<li><p><a href="viewer.php?nid=$nid" id="record-$nid"> $athor, note_type($type)</a></p></li>
ENTRY_TICKET;

    return $entry_ticket;

    }
}


}
}

echo entry_tickets();

?>

</ul>

<div style="clear:both;height:10px;"></div>

sorry forgot db.class.php

<?php
session_start();
//connect.php
$host = "localhost";
$db_user = "root";
$db_pass = "";
$db_name = "au";

$connectClass = mysql_connect("$host", "$db_user", "$db_pass") or die ("Couldn't establish connection to database server.");
$dbObject = mysql_select_db("$db_name", $connectClass) or die ("Couldn't select database.");
?>

error reporting disabled error code

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in D:\wamp\www\ageis\note.php on line 22
2
  • 2
    Returning a blank page could mean that PHP has been set to not display the error (kinda sensitive and useless for the enduser). Try setting it to display all errors. Commented May 11, 2011 at 18:32
  • 1
    Then change line 20 to: $r = mysql_query($q) or die(mysql_query()); Commented May 11, 2011 at 18:36

2 Answers 2

4

Your mysql syntax looks bad. You have:

 SELECT * FROM `notice` ORDER BY nid LIMIT 12 DESC

try

 SELECT * FROM `notice` ORDER BY nid DESC LIMIT 12

Erik's comments should have pointed you in the right direction, however:

  1. Figure out where PHP logs errors. Either turn up php's error reporting level so you see more errors, or start tailing your apache error_log
  2. You should always check for errors after running mysql_query and do some sort of logging on failure. This is probably best accomplished by writing a wrapper function for mysql_query that does this, or using a 3rd party db wrapper that has already solved this problem.
Sign up to request clarification or add additional context in comments.

1 Comment

And I should add, that: or die(mysql_query()) should not be in production code. Errors should be caught and logged so the end user won't see it, just like Frank says.
2

You're redefining function note_type every time through your while loop. You have a return call outside the function, below it. Really I can't see how this is even syntactically correct. It looks like you have a large problem with mismatched brackets.

As for an actual database issue, you should check mysql_error() if no rows return from the call because it's likely something went wrong.

Also I recommend using PDO which is a true database class instead of the native function based ones.

4 Comments

Also if you have access to a command line you can do php -l your_filename.php and it will give you the errors. It's probably in your webserver log as well.
wow, you're right, the WTF-per-line count here is high. $athor is spelled wrong in one place, and there's an attempt to call note_type($type) from inside of a docblock
FYI: Returning no rows is not an error, and mysql_error() will not tell you anything.
a yes manage to fail that part was in a bit of a rush I rewrote the entire code now. So it works the only part is note_type function which I can get to work...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.