hello,

I'm new to the PHP-scene and I'm having difficulties with creating a log in system.
The login system writes to a postgresql database.
when I try to run the page I receive this error:

Warning: pg_num_rows(): supplied argument is not a valid PostgreSQL result resource in /var/ftpdirs/512544/PHP/login/adduser.php on line 13
MDB2 Error: no such field

the error relates to this function:

http://www.php.net/manual/en/function.pg-num-rows.php

this is my source code of adduser.php:

<?php
        session_start();
        include "connectie_db.php";
        $User_name = $_POST['user_name'] ;
        $User_pass = $_POST['user_pass'] ;
	$User_pass2 = $_POST['user_pass2'] ;
	$checkUsername= 
                     "SELECT user_name 
                       from tovanu.users 
                        where user_name = '$User_name';";

        $q = $db->exec($checkUsername);
		if(MDB2::isError($q)){
					echo "code: ".$q->getUserInfo();
					exit();
					}
        if(pg_numrows($q) == 1){
                echo "<p>Someone took that username</p>";
                include "signup.php";
                exit;
        }
        If(strlen($User_name > 32)){
                echo "<p>The username is too long</p>";
                include "signup.php";
                exit;
        }
        if($User_pass != $User_pass2){
                echo "<p>Both passwords must be the same</p>";
                include "signup.php";
                exit;
        }
        $password = md5($User_pass);
        $add = "INSERT INTO tovanu.users (user_name,user_pass) 
                VALUES ($User_name,$password)";

        $execute = $db->exec($add) ;
        
        	if(MDB2::isError($execute)){
					echo "code: ".$q->getUserInfo();
					exit();
					}
					
        $_SESSION['user'] = $username;
        header("Location: index.php");
		include "index.php";
        ?>

On line 12 you have $q = $db->exec($checkUsername); . That tells me that you have a custom class (you are possibly executing something like $db=new MDB2(....); somewhere) that has its own execute method (named exec). Within that method try putting/attaching or die( pg_last_error($conn) ) (refer to http://www.php.net/manual/en/function.pg-last-error.php) to your actual execute statement (so that you can find out the reason for the failed query) OR make sure that your exec method returns null when an error occurs.

If the problem persists, you will need to post the code to your MDB2 class.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.