PDA

View Full Version : [PHP/mySQL] mysql_num_rows() doesn't work?



Trigs
15-03-2009, 09:02 PM
$msgquery = mysql_query("SELECT * FROM pms WHERE to = '{$_SESSION['username']}' ORDER BY id desc",$conn);
$msgnum = mysql_num_rows($msgquery);

I keep getting:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource blah blah blah

Protege
15-03-2009, 09:20 PM
SELECT * FROM `pms` WHERE `to` = '' ORDER BY `id` DESC


include 'mysql_config.php'; // mysql db config file etc.

$MySQLQuery = mysql_query( 'SELECT * FROM `pms` WHERE `to` = "' . $_SESSION[ 'username' ] . '" ORDER BY `id` DESC' );
$MySQLRows = mysql_num_rows( $MySQLQuery );

echo $MySQLRows;

Trigs
16-03-2009, 09:39 PM
So the problem was that I didn't use ` right?

Protege
16-03-2009, 09:47 PM
I believe so.

Trigs
16-03-2009, 09:50 PM
Aha thanks, it worked. +rep

Trigs
16-03-2009, 09:56 PM
I'm also having a problem with this...

Fatal error: Cannot redeclare clean() (previously declared in ---/config.php:60) in -------/config.php on line 66

I have register.php which uses clean() several times. It requires config.php which has this from line 60-66:

function clean($str) {
if(!get_magic_quotes_gpc()) {
$str = addslashes($str);
}
$str = strip_tags(htmlspecialchars($str));
return $str;
}

*** is wrong with it?

Dentafrice
16-03-2009, 09:59 PM
you're declaring the function clean() more then once.

Protege
16-03-2009, 09:59 PM
I believe that means you have the function "Clean" declared twice 8-)

Trigs
16-03-2009, 10:01 PM
Pretty obvious lol.

I figured it out. I had a function in register.php that also required config.php. I just globalized the vars I needed from config.php instead.

Trigs
16-03-2009, 10:43 PM
Okay nevermind, it still doesn't work...

Now it happens with every function I have stored in config.php. For example:


Fatal error: Cannot redeclare clean() (previously declared in ---/config.php:60) in ---/config.php on line 66

Lines 60-66:


function clean($str) {
if(!get_magic_quotes_gpc()) {
$str = addslashes($str);
}
$str = strip_tags(htmlspecialchars($str));
return $str;
}

I don't see what the problem is.

Dentafrice
16-03-2009, 10:54 PM
You're redeclaring it again somewhere else.. what part of that don't you get?

You can only declare clean() one time.. in the entire program.

Trigs
16-03-2009, 10:56 PM
What do you mean only one time? You mean I can only do function clean() {} once? not clean($abc) once right?

Dentafrice
16-03-2009, 11:00 PM
Yes, you can only declare the function (function clean() {) once.

Trigs
16-03-2009, 11:01 PM
I only declared it once... Should I post all of config.php?

Dentafrice
16-03-2009, 11:09 PM
#1, why are you declaring your functions in config.php, and #2: it wouldn't say that if you haven't declared it elsewhere.

Trigs
16-03-2009, 11:11 PM
Declaring as in setting it (function name() { blah blah blah }) right? Well why not?

Dentafrice
16-03-2009, 11:14 PM
Declaring as in setting it (function name() { blah blah blah }) right? Well why not?
Because the configuration file is not the spot for declaring functions.

Trigs
16-03-2009, 11:16 PM
And this causes the error how...

Dentafrice
16-03-2009, 11:19 PM
It doesn't, and I never once said you did, I was just asking why the hell you were declaring them there.

And then I said that the error wouldn't be caused, unless you were declaring the functions somewhere else.. so somewhere you're re-declaring the clean() function.

Protege
16-03-2009, 11:22 PM
BASICALLY
HES SAYING THIS

In config you got

function Clean( $str )
{

}

and in another location you got a function called clean

and its like having a fight and php doesnt like that.

Trigs
16-03-2009, 11:23 PM
@dent: Where should I declare them... lmao
@protege: i know that...

Up to line 66:

<?php
#######################
# Configuration #######
#######################
// Database Connection
removed
// Gets an array with the active user's information
$query = mysql_query("SELECT * FROM users WHERE username = '{$_SESSION['username']}'",$conn);
$userinfo = mysql_fetch_array($query);
// Bans
if($userinfo['banned'] == 'yes') {
include('banned.php');
die();
}
// Messages
$msgquery = mysql_query("SELECT * FROM `pms` WHERE `to` = '{$_SESSION['username']}' ORDER BY `id` DESC",$conn);
$msgnum = mysql_num_rows($msgquery);
// Version No.
$version = 'r1 BETA';
// Admin/System Email
$adminemail = '';
// Errors
$err_login = 'You must be logged in to view this page. Please <a href="login.php">login</a> or <a href="register.php">register</a>.';
$err_auth = 'Only authorized staff can view this page!';
$err_notfound = 'The page you were looking for could not be found. Click <a href="index.php">here</a> to go back to the main page.';
// IP
$ip = $_SERVER['REMOTE_ADDR'];
// Date
$day = date('l');
$month = date('F');
$daynum = date('j');
$year = date('Y');
$date = $day.', '.$month.' '.$daynum.', '.$year;
#######################
# System Functions ####
#######################
// Cleaning Input
function clean($str) {
if(!get_magic_quotes_gpc()) {
$str = addslashes($str);
}
$str = strip_tags(htmlspecialchars($str));
return $str;
}

I had the exact same thing on a different host and it worked but when I switched it gave me these errors...

Dentafrice
16-03-2009, 11:25 PM
I'm not even going to comment on how horrible that code is.

Post the code for the page that presents the error, or give me the link to config.php page.

Protege
16-03-2009, 11:30 PM
Shouldnt declare so many variables unless you're going to use them each time you load your config page.

Trigs
16-03-2009, 11:35 PM
nvm, fixed it. also a question,

If I do;

require 'config.php';
require 'home.php';

and I had a function in config.php, would I be able to use it in home.php without requiring config.php in home.php?

Protege
16-03-2009, 11:38 PM
"and I had a function in config.php, would I be able to use it in home.php without requiring config.php in home.php?"

Can I call my Dad on my home phone which isn't connected to a telephone line?

No you can't, you need to require/include it in the file inorder to use it.

Dentafrice
16-03-2009, 11:58 PM
Quit. Putting. *******. Functions. In Your Config File.

---------

Lets say you have this.

config.php - includes database details/connection, etc.
functions.php - includes your clean function and some other things.
home.php - home file.

here's the code on config.php:



<?php
require_once "functions.php";
?>


here's the code on home.php:



<?php
include "config.php"; // includes the config file.

$string = $_GET["string"];
$string = clean($string);

Blinger1
17-03-2009, 04:33 AM
Quit. Putting. *******. Functions. In Your Config File.

---------

Lets say you have this.

config.php - includes database details/connection, etc.
functions.php - includes your clean function and some other things.
home.php - home file.

here's the code on config.php:



<?php
require_once "functions.php";
?>


here's the code on home.php:



<?php
include "config.php"; // includes the config file.

$string = $_GET["string"];
$string = clean($string);

or just download a premade user ******* system.

Dentafrice
17-03-2009, 11:17 AM
or just download a premade user ******* system.
How does that help anyone learning anything? I'd rather make my own then download a pile of **** that I'm going to spend more time customizing to make it how I want, then actually building it.

Jxhn
19-03-2009, 04:51 PM
nvm, fixed it. also a question,

If I do;

require 'config.php';
require 'home.php';

and I had a function in config.php, would I be able to use it in home.php without requiring config.php in home.php?

Yes you can, if you require it in home.php then you will get the error you got before. You can use require_once to stop this happening. Protege you read his post wrong.

Trigs
19-03-2009, 04:53 PM
yeah, i already figured that out.

Want to hide these adverts? Register an account for free!