Jump to content

Not Valid Mysql Query


Distortion
 Share

Recommended Posts

So I'm confused with all the different ways in which queries are written, and I'm not seeing why this code gives me the following error.Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource on line 7

<?php include("includes/messages.php"); $username=$_SESSION['username'];$query = "SELECT * FROM messages WHERE receiver='$username' AND read='0'"; $result = mysql_query($query); $new = mysql_num_rows($result);?>
session has already been started in the page where this script is included. Also I'd like some information about things like $_POST['username'] because sometimes I see it with the ' and sometimes without in queries. And how do I implement a function like crypt($pw) in a query. I hope you understand what I want because I'm having some difficulty explaining :|
Link to comment
Share on other sites

http://php.net/manual/en/function.mysql-num-rows.php
<?php$link = mysql_connect("localhost", "mysql_user", "mysql_password");mysql_select_db("database", $link);$result = mysql_query("SELECT * FROM table1", $link);$num_rows = mysql_num_rows($result);echo "$num_rows Rows\n";?>
that's an example from the PHP manual. I think you need to connect to the DB with your mysql_query, and/or put it in parentheses. As far as quotations, I think when you have double quotes, you need to use single quotes within them, or escape them. Edited by thescientist
Link to comment
Share on other sites

oh yeah I forgot to tell you. I connect to the database by including messages.php which does not give me any warnings, so I think that connects propperly.I meant something else with the double quotes, I know about escaping them but I just don't get how to use session variables or php functions in MySql.I'll try to find a clear example...I don't really know what you meant with putting 'it' in parenthesis.

Link to comment
Share on other sites

The query is failing, so $result is not a resource. A failed query does not return a PHP error. It just returns false.Obviously, as the scientist says, if you haven't connected, your query will fail.Echo the query to make sure it is correct. You never know what's going to turn up in a variable till you look at it.Is the read field a string or number? I ask because you have a zero in quote marks. Could be fine. I don't know your setup.Is this your first time accessing this database? My implementation requires `backticks` around field names. Yours might also.Old-style PHP allowed associative array indexes to be used without quotation marks. I suppose you've seen it done that way in queries to minimize nested quotation mark problems. But it is bad practice, and I believe PHP 6 may not allow it at all. Assigning an array value to a scalar variable as you are doing is the most surefire way of eliminating probalems like that. It costs an extra programming line, but it creates clarity that makes the cost worthwhile. And it's a very small cost anyway. So keep doing it.

Link to comment
Share on other sites

I narrowed down the problem. Database is working fine, i tried every possible variation with quotes.The problem seems to be the word read.If i put any other row there e.g. time or sender it'll work. But I need to know if it is read or not. time is an int just like read, so that isn't the difference. What i'm thinking is that maybe read is a reserved term or something?Wait, why don't i rename it...EDIT: Yes that is it, I renamed 'read' to 'viewed' and it worked. Does anyone understand this?

Edited by Distortion
Link to comment
Share on other sites

Wait, why don't i rename it...EDIT: Yes that is it, I renamed 'read' to 'viewed' and it worked. Does anyone understand this?
READ is a reserved word in MySQL - It's often best just to avoid them as you figured out, this should shed some light for you.http://dev.mysql.com/doc/refman/5.1/en/reserved-words.html Edited by scott100
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...