Page 1 of 2

Fatal error: Call to a member function query() on a non-obje

Posted: Sun Oct 15, 2006 7:35 pm
by garry27
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Fatal error: Call to a member function query() on a non-object in ........ /pa_home.php on line 59

why do i keep getting the above error when i try to connect to a database using a "self executable" script and sessions? 

Pleeeeeeeeeeeassssseeeeeeee heeeeeelpppppppppp meeeeeeee!!!!!!! im going insane and i WILL fail my university project if i don't get this sorted out soon!

Code: Select all

<?php   //common.php
function db_connect()
{
   $result = new mysqli('www.nl-webspace.co.uk', 'unn_p921847', 'password'); 
   if (!$result)
     throw new Exception('Could not connect to database server');
   else
     return $result;
}




<?php   //logon page
  //Initiates php script containing functions all common functions 
  
  require_once('common.php');  
  
  $dbconn = db_connect();
  session_start();
  
  //outputs header info to brower with the following title
  echo xhtmlheader('Personal Account Registration Form');
  
  //outputs the main navigation bar to the browser
  echo topnav_pa();

  if (isset($_POST['email']) && isset ($_POST['pwd']))
  {
    //if the user has just tried to log in
    $email = $_POST['email'];
    $pwd = $_POST['pwd'];
  
    //$db_conn = new mysqli('localhost', 'webauth', 'webauth', 'auth');
  
    $sql = 'select * from Authourised_User'
    ."where userEmail='$email'"
    ." and userPassword='$pwd';";
  
    $result = $dbconn->query($sql);
    if ($result->num_rows >0)
    {
    $_SESSION['valid_user'] = $email;
    }
    $dbconn->close;
  }
?>
 
<!-- main body of page -->
<div id="main"> 
  <h1>My Pub Crawl Planner</h1>
 



<?php 
  if (isset($_SESSION['valid_user']))
  {
    echo 'You are logged in as: '.$_SESSION['valid_user'].' <br />';
    echo '<a href="logout.php">Log out</a><br />';
  }
  else
  {
    if (isset($email))
    {
      // if they've tried and failed to log in
      echo 'Could not log you in.<br />';
    }
    else 
    {
      // they have not tried to log in yet or have logged out
      echo 'You are not logged in.<br />';
    }
  }
?>

  <p>Member Login </p>
  <form action="index.php<?php echo strip_tags(SID); ?>" method="post" class="login">
    <table>
      <tr> 
        <td>E-mail:</td>
        <td class="table"> <input type="text"
                name="email"
                value="" 
                tabindex="1"/></td>
      </tr>
      <tr> 
        <td>Password:</td>
        <td class="table"> <input type="text"
                name="pwd"
                value="" 
                tabindex="2"/></td>
      </tr>
          </table>

    <!-- submit button  -->
    <input type="submit" 
          name="login"
          value="Login" 
          tabindex="7"/>
  </form>
  
  
	
    <br/>
  <br/>
  <
</div>
</body>
</html>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Re: Fatal error: Call to a member function query() on a non-

Posted: Sun Oct 15, 2006 7:42 pm
by volka
please mark line 59 in your script and use

Code: Select all

[url=http://forums.devnetwork.net/faq.php?mode=bbcode]bbcode tags[/url].

Posted: Sun Oct 15, 2006 7:43 pm
by garry27
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


the previous error was actually for another db_connect() function that i tried:

Code: Select all

function db_connect()
  {

  $conn = MYSQL_CONNECT('www.nl-webspace.co.uk', 'unn_p921847', 'password');
  $db = mysql_select_db('unn_p921847', $conn);
  
 }


the db_connect() in my original messge returns:

Code: Select all

Warning: mysql_query() [function.mysql-query]: Access denied for user 'apache'@'localhost' (using password: NO) in /.........../pa_home.php on line 38

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /........../pa_home.php on line 38
2047
whats up with this? 8O


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Sun Oct 15, 2006 7:44 pm
by volka
missing return statement in db_connect()

Posted: Sun Oct 15, 2006 8:52 pm
by garry27
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


thanks for replying, volka.

i now get the following error regardless of what's in the login form when i submit it (i think there may be something wrong with the session which i'm trying to forward in the url):

Notice: Trying to get property of non-object in /home/unn_p921847/public_html/index.php on line 24

Code: Select all

<?php
  //Initiates php script containing functions all common functions 
  session_start();
  require_once('common.php');
  $dbconnect = db_connect();
  
  
  //outputs header info to brower with the following title
  echo xhtmlheader('Personal Account Registration Form');
  
  //outputs the main navigation bar to the browser
  echo topnav_pa();

  if (isset($_POST['email']) && isset ($_POST['pwd']))
  {
    //if the user has just tried to log in
    $email = $_POST['email'];
    $pwd = $_POST['pwd'];
  
    $sql = "select * from Authourised_User where userEmail='$email' and userPassword='$pwd'";
         
  
    $result=$dbconnect->query($sql);                     /*****line 24 *****/
    if ($result->num_rows >0 )
    {
    $_SESSION['valid_user'] = $email;
    }
    $dbconnect->close();
  }
?>
 
<!-- main body of page -->
<div id="main"> 
  <h1>My Pub Crawl Planner</h1>
 



<?php 
  if (isset($_SESSION['valid_user']))
  {
    echo 'You are logged in as: '.$_SESSION['valid_user'].' <br />';
    echo '<a href="logout.php">Log out</a><br />';
  }
  else
  {
    if (isset($email))
    {
      // if they've tried and failed to log in
      echo 'Could not log you in.<br />';
    }
    else 
    {
      // they have not tried to log in yet or have logged out
      echo 'You are not logged in.<br />';
    }
  }
?>

  <p>Member Login </p>

 <form action="index.php" method="post" class="login">         /****form action *****/

    <table>
      <tr> 
        <td>E-mail:</td>
        <td class="table"> <input type="text"
                name="email"
                value="" 
                tabindex="1"/></td>
      </tr>
      <tr> 
        <td>Password:</td>
        <td class="table"> <input type="text"
                name="pwd"
                value="" 
                tabindex="2"/></td>
      </tr>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Re: Fatal error: Call to a member function query() on a non-

Posted: Sun Oct 15, 2006 9:03 pm
by volka
volka wrote:please mark line 59 in your script and use

Code: Select all

[url=http://forums.devnetwork.net/faq.php?mode=bbcode]bbcode tags[/url].[/quote]now line 24.

Posted: Sun Oct 15, 2006 9:36 pm
by garry27
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


thanks for replying, volka. 

i now get the following error regardless of what's in the login form when i submit it (i think there may be something wrong with the session which i'm trying to forward in the url): 

Notice: Trying to get property of non-object in /home/unn_p921847/public_html/index.php on line 24

Code: Select all

<?php 
//Initiates php script containing functions all common functions 
session_start(); 
require_once('common.php'); 
$dbconnect = db_connect(); 


//outputs header info to brower with the following title 
echo xhtmlheader('Personal Account Registration Form'); 

//outputs the main navigation bar to the browser 
echo topnav_pa(); 

if (isset($_POST['email']) && isset ($_POST['pwd'])) 
{ 
//if the user has just tried to log in 
$email = $_POST['email']; 
$pwd = $_POST['pwd']; 

$sql = "select * from Authourised_User where userEmail='$email' and userPassword='$pwd'"; 


$result=$dbconnect->query($sql); /*****line 24 *****/
if ($result->num_rows >0 ) 
{ 
$_SESSION['valid_user'] = $email; 
} 
$dbconnect->close(); 
} 
?> 

<!-- main body of page --> 
<div id="main"> 
<h1>My Pub Crawl Planner</h1> 




<?php 
if (isset($_SESSION['valid_user'])) 
{ 
echo 'You are logged in as: '.$_SESSION['valid_user'].' <br />'; 
echo '<a href="logout.php">Log out</a><br />'; 
} 
else 
{ 
if (isset($email)) 
{ 
// if they've tried and failed to log in 
echo 'Could not log you in.<br />'; 
} 
else 
{ 
// they have not tried to log in yet or have logged out 
echo 'You are not logged in.<br />'; 
} 
} 
?> 

<p>Member Login </p> 

<form action="index.php" method="post" class="login"> /****form action *****/ 

<table> 
<tr> 
<td>E-mail:</td> 
<td class="table"> <input type="text" 
name="email" 
value="" 
tabindex="1"/></td> 
</tr> 
<tr> 
<td>Password:</td> 
<td class="table"> <input type="text" 
name="pwd" 
value="" 
tabindex="2"/></td> 
</tr>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Sun Oct 15, 2006 9:39 pm
by volka
volka wrote:please mark line 59 in your script and use

Code: Select all

[url=http://forums.devnetwork.net/faq.php?mode=bbcode]bbcode tags[/url][/b].[/quote]
and please post the current code of your function db_connect(), too.

Posted: Mon Oct 16, 2006 7:33 am
by feyd
garry27, I've had to edit every single one of your posts. Please do not make me, or any other moderator, have to do that by starting to use the highlighting tags as volka has so kindly asked.

Posted: Mon Oct 16, 2006 8:02 am
by garry27
hi, sorry about that, guys.

here's the db function which i'm currently using

Code: Select all

function db_connect()
{
   $result = new mysqli('www.nl-webspace.co.uk', 'unn_p921847', 'password'); 
   if (!$result)
     throw new Exception('Could not connect to database server');
   else
     return $result;
}
TIA

Posted: Mon Oct 16, 2006 8:18 am
by volka
Hm, maybe
if ($result->num_rows >0 )
actually is line 24?

Code: Select all

<?php
//Initiates php script containing functions all common functions
session_start();
require_once('common.php');
$dbconnect = db_connect();


//outputs header info to brower with the following title
echo xhtmlheader('Personal Account Registration Form');

//outputs the main navigation bar to the browser
echo topnav_pa();

if (isset($_POST['email']) && isset ($_POST['pwd']))
{
	//if the user has just tried to log in
	$email = $_POST['email'];
	$pwd = $_POST['pwd'];

	$sql = "select * from Authourised_User where userEmail='$email' and userPassword='$pwd'";

	if (!is_object($dbconnect)) {
		die ('dbconnect is not an object. '.var_dump($dbconnect));
	}
	$result=$dbconnect->query($sql);
	if ( false===$result) {
		die($dbconnect->error . ': '.$sql);
	}
	if ( $result->num_rows > 0 )
	{
		$_SESSION['valid_user'] = $email;
	}
	$dbconnect->close();
}
?>
...

Posted: Mon Oct 16, 2006 10:35 am
by garry27
ye that's the one! :oops: hehe

can you think why i can't query my database and will it have anything to do with the way i'm using sessions?

Posted: Mon Oct 16, 2006 10:42 am
by volka
it means
$result=$dbconnect->query($sql);
doesn't return a valid mysql result resource. It returns false if the query fails, i.e. no database connection or permission denied or syntax errors in sql statement.

doesn't the "new"
$result=$dbconnect->query($sql);
if ( false===$result) {
die($dbconnect->error . ': '.$sql);
}
part print a "new" error message?

Posted: Mon Oct 16, 2006 11:04 am
by garry27
i don't know what's going on. when i submit it sometimes shows page not found and sometimes it shows only:
Notice: Trying to get property of non-object in /home/unn_p921847/public_html/index.php on line 24
the only changes i've made are adding :

Code: Select all

if ( false===$result) { 
die($dbconnect->error . ': '.$sql); 
}
and removing the css style functions:

http://www.nl-webspace.co.uk/~unn_p921847

Posted: Mon Oct 16, 2006 11:21 am
by RobertGonzalez
Try the connection in the function, but the exception checking in the code:

Code: Select all

<?php
function db_connect()
{
   return new mysqli('www.nl-webspace.co.uk', 'unn_p921847', 'password');
}

try {
    $return = db_connect();
} catch (Exception $e) {
    echo "Failed to get DB handle: " . $e->getMessage() . "\n";
    exit;
}
?>
See if this does anything different.