null given?

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
ScottCFR
Forum Commoner
Posts: 33
Joined: Sat Jun 19, 2010 7:36 pm

null given?

Post by ScottCFR »

I am working on the admin panel I plan to put on my site, I am trying to make it so the contact form posts into a mysql db and it displays in the admin panel. For some reason, I can't figure out what I've done wrong.

Errors:

Code: Select all

Warning: mysql_num_rows() expects parameter 1 to be resource, null given in /home/scottcfr/public_html/admin/index.php on line 39
Warning: mysql_fetch_assoc() expects parameter 1 to be resource, null given in /home/scottcfr/public_html/admin/index.php on line 152
Lines:

Code: Select all

39: <p><a href="index.php?page=blog">Blog</a> -=- <a href="index.php?page=announcements">Announcements</a> -=- Contacted (<?php include "config.php"; $return = mysql_query("SELECT * FROM contact WHERE read='no'"); mysql_num_rows($return1) ?>)-=- <a href="http://hellbeast.net/">Site Home</a> -=- <a href="http://hellbeast.net/forums/">Site Forums</a>

152: while($myrow = mysql_fetch_assoc($result))
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: null given?

Post by Jonah Bron »

Intuitively, I would expect this:

Code: Select all

$return = mysql_query("SELECT * FROM contact WHERE read='no'"); mysql_num_rows($return1)
To be:

Code: Select all

$return = mysql_query("SELECT * FROM contact WHERE read='no'"); mysql_num_rows($return)
$return != $return1

Just a wild guess, but the second error might be the same problem. $result != $return != $return1.
ScottCFR
Forum Commoner
Posts: 33
Joined: Sat Jun 19, 2010 7:36 pm

Re: null given?

Post by ScottCFR »

Well, I changed them all so they aren't close to the same thing. I still get the same error..
s992
Forum Contributor
Posts: 124
Joined: Wed Oct 27, 2010 3:06 pm

Re: null given?

Post by s992 »

You want them to be the same thing.

You're assigning your query to $return, but then you don't do anything with it(that we can see). You check for the number of rows in $return1, and then you try to fetch an associative array of $result - neither of those variables have been declared in the snippet you gave us.
ScottCFR
Forum Commoner
Posts: 33
Joined: Sat Jun 19, 2010 7:36 pm

Re: null given?

Post by ScottCFR »

Sorry, but I'm not understanding.

In the one where I am counting the rows where read=no, if I remove "WHERE read='no'" it's fine, but if it's there it doesn't work... explanation?
s992
Forum Contributor
Posts: 124
Joined: Wed Oct 27, 2010 3:06 pm

Re: null given?

Post by s992 »

What I was getting at is that it appears that you are trying to reference the results with three different variables.

On line 39, you have two variables($return and $return1):

Code: Select all

<?php include "config.php"; $return = mysql_query("SELECT * FROM contact WHERE read='no'"); 
mysql_num_rows($return1) ?>
The results of your query are stored in $return, not $return1. Judging from the two lines you gave us, $return1 appears to be blank. Likewise on line 152, you reference $result which appears to have not been assigned a value.

However, now that you say that it works fine if you remove the WHERE clause, I'd venture a guess that it has something to do with the data in your database. Your mysql_query() isn't returning any data(hence the null), which means it can't find any records matching the query. Ensure that you actually have records in the DB where read = no. To see if MySQL is throwing an error when you are attempting the query, try this code:

Code: Select all

<?php
include "config.php";
$return = mysql_query("SELECT * FROM contact WHERE read='no'") OR die(mysql_error());
// Plug in the rest of your code...
Let me know if it returns an error message, that will help us figure it out a little bit more.
Post Reply