Jump to content

mysql_num_rows problem, can someone help?


j.smith1981

Recommended Posts

I am having real problems getting mysql_num_rows function working with PHP 5

 

This is the split up script:

 

index.php

<?php
ini_set('display_errors', 1);

require_once 'config.php';

require_once 'func/func.db.php';

$connect = databaseConnect($host, $user, $password, $database);

if($connect == true)
{

//	printf("<table>");

// get it querying results for showing users entries as of now:
$result = query("SELECT * FROM entries");

if($result == true)
{
	printf("<table>
	<tr>
	<td></td>
	</tr>\n");

	while($row = mysql_fetch_array($result))
	{
		printf("
		<tr>
		<td>Result row</td>
		</tr>\n");
	}

	printf("</table>\n\n");
}

// show form for users logged in so they can post entries, ie just one user account for now:
require_once 'auth.php';

}
?>

 

func/func.db.php

<?php
function databaseConnect($host, $user, $password, $database)
{
$connect = mysql_connect($host, $user, $password);

if($connect)
{
	$select_db = mysql_select_db($database, $connect);
	if($select_db)
	{	
		return true;
	} else {
		return false;
	}
} else {	
	return false;
}
}

function query($sql)
{
$sql = stripslashes($sql);
$query = mysql_real_escape_string($sql);
$result = mysql_query($query);

if($result){
	return $result;
}
}

 

Then the auth.php, looks like:

<?php
if(isset($_POST))
{

$message = 'Only registered users can post';

if(array_key_exists('submit',$_POST))
{
	if($_POST['username'] != '' && $_POST['password'] != '')
	{
		$username = $_POST['username'];
		$password = sha1($_POST['password']);

		// try to log user in:
		$result = query("SELECT * FROM users WHERE username = '$username' AND '$password'");

		echo mysql_num_rows($result);

	} else {

		$message = 'You did not enter all required fields, please retry';

	}
}

} else {

$message = 'Only registered users can post';

}
?>

<p><?=$message;?></p>
<table>
<form id="login" name="login" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

<tr>
	<td><label for="username">User: </label></td>
	<td><input type="text" id="username" name="username" value="" /></td>
</tr>

<tr>
	<td><label for="password">Password: </label></td>
	<td><input type="password" id="password" name="password" value="" /></td>
</tr>

<tr>
	<td><input type="submit" id="submit" name="submit" value="Login" /></td>
</tr>
</form>
<table>

 

You dont need to know whats in config.php since thats just full of my database connection variables, like the hostname, user, password and database, but its saying mysql_num_rows is an invalid function, since maybe its because its lost connection to the database, but how would I get around this?

 

I thought about maybe making it connect to the database again, but is there any point in doing this?

 

This is the actual error I get:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /www/jeremysmith.me.uk/html/blog-dev/auth.php on line 17

 

Any help is much appreciated,

Jez.

Link to comment
Share on other sites

that error has nothing to do with the mysql_num_rows function being unrecognised.  That error is thrown because your SQL syntax is malformed in the following:

	$result = query("SELECT * FROM users WHERE username = '$username' AND '$password'");

The fact that you are using SELECT * asside, you are not checking $password against a field name in the database.  Try:

$result = query("SELECT username FROM users WHERE username = '$username' AND password= '$password'");

Link to comment
Share on other sites

Nope its still causing problems even though I have fixed it, didnt think that'd work.

 

It would still come up with that error even when I have actually noticed my own error, but it wouldnt matter anyways, its PHP saying that its an unrecognised resource, so no matter what syntax you use, it would still come up with that error, its bringing that error up even before the sql syntax is executed, I just cant remember how to get around it, thats all.

 

It's breaking before even executing whats in the SQL

 

Could even have "SELECT *" and that would bring up the exact same error.

 

Can someone help please?

Link to comment
Share on other sites

Ah I know now. sorry yes.

 

But if I change the query line to:

 

			$result = mysql_query("SELECT * FROM users WHERE username = '$username' and password = '$password'");
		if(!$result)

		{
			echo mysql_error();
		} else {
			echo $result;
		}

 

It works, just outputted the SQL statement before and its not removing the slashes which is what stripslashes is supposed to do, any suggestions?

Link to comment
Share on other sites

I know what I have done!

 

Put it within an if statement, this is how I did it:

 

			$result = query("SELECT * FROM users WHERE username = '$username' and password = sha1('$password')");
		$count = mysql_num_rows($result);

 

Now it works, don't really see what I have done to get around this (probs due to the number of times I have looked at it), but it works now.

 

Thanks,

Jez.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.