...

View Full Version : script help



SDP2006
01-02-2004, 05:09 AM
<?php
include('configed.php');

class user
{
function warnUser($user,$wpts){
$this->user = $user;
$this->wp = $wpts;

$link = @mysql_connect($host,$user,$pass) or die("Could not connect to database. Please try again later!");
@mysql_select_db($db,$link) or die("Could not select database. Please try again or try again later.");
$sql_insert = "UPDATE `users` SET `wpts`='{$this->wp}' WHERE `user`='{$this->user}'";
mysql_query($sql_insert,$link);
$success = "Successful";
return $success;
}
}

$exe = new user();
$exe->warnUser('SDP2006','2');
?>
My error is
Could not select database. Please try again or try again later.I don't know why. btw here is configed.php
<?php
$host = "localhost";
$dbase = "testing";
$user = "root";
$pass = "rootpass";
?>
My database information is correct, but It must be something else. Thanks for any help.

firepages
01-02-2004, 07:08 AM
a) your class can not 'see' your connection variables as they are outside of its scope
b) $db should be $dbase

either import using global ...


function warnUser($user,$wpts){
global $host,$dbase,$user,$pass;

or directly

function warnUser($user,$wpts){
include('configed.php');


(both of which are bad ideas though they work)

of globalise your variables in the first place ...

<?php
$GLOBALS['db']['host'] = "localhost";
$GLOBALS['db']['dbase'] = "testing";
$GLOBALS['db']['user'] = "root";
$GLOBALS['db']['pass'] = "rootpass";
?>


& then
$link = @mysql_connect($GLOBALS['db']['host'] , ... etc

..........................................................................

BUT , doing your db connection in each and every class method is not a good plan , perhaps ..



<?php
include('configed.php');

class user{
var $db ;//db connection

function user( $db ){
//store the db connection//
$this->db=$db;
}

function warnUser($user,$wpts){
$this->user = $user;
$this->wp = $wpts;

$sql_insert = "UPDATE `users` SET `wpts`='{$this->wp}' WHERE `user`='{$this->user}'";
mysql_query( $sql_insert , $this->db ) ;
$success = "Successful";
return $success;
}
}


$link = @mysql_connect( $host , $user , $pass ) or die("Could not connect to database. Please try again later!");
@mysql_select_db( $dbase , $link ) or die("Could not select database. Please try again or try again later.");

$exe = new user( $link ) ;
$exe->warnUser('SDP2006','2') ;

//see the advantage ? you can reuse that link anywhere//
$other = new other_class( $link );
?>


or a vriation on such , thats just one idea , there are many other ways , but the general idea is not to have database connections happening left right and center.

................................................................................

A handy note .. if you are only accessing one database host (and mostly that will be the case) , you dont even need to pass all the database connections around , once php has made a MySQL connection , it will reuse that connection wherever it can , unless you close it its always available...



<?
mysql_connect($host,$user,$pass);
mysql_select_db($dbase);

class whatever{
function whatever(){
//this query will work asis//
echo 'resource id ...' . mysql_query("SELECT * FROM table");
}
}

whatever::whatever();
?>



there are many good reasons for not having ANY sql calls within the classes themselves and abstracting the database work elsewhere , but thats another story.

SDP2006
01-02-2004, 02:21 PM
Thanks, my script works now.

SDP2006
01-02-2004, 02:27 PM
One more question. How do I change my MySQL Query to where it adds to the current total in the field. Like if there were 2 warning points and you wanted to add 2 more that would give a total of 4. How might I do that?

Thanks

ReadMe.txt
01-02-2004, 07:53 PM
UPDATE `users` SET `wpts`= `wpts` + '{$this->wp}' WHERE `user`='{$this->user}'

SDP2006
01-02-2004, 08:26 PM
Thanks, ReadMe.

One last problem.
<?php
include('configed.php');

class user
{
var $db ;

function user( $db ){
$this->db=$db;
}

function warnUser($user,$wpts)
{
$this->user = $user;
$this->wp = $wpts;

$sql_insert = "UPDATE `users` SET `wpts`=`wpts` + '{$this->wp}' WHERE `user`='{$this->user}'";
mysql_query( $sql_insert , $this->db ) ;
$success = "{$this->user} recieved {$this->wp} warning points!";
print $success;
}
function printPts($user)
{
$this->user = $user;
$sql_select = "SELECT user,wpts FROM users WHERE `user`='{$this->user}'";
mysql_query($sql_select, $this->db);
$sql = mysql_fetch_array($sql_select);
$this->user = $sql[user];
$this->wpts = $sql[wpts];
$final = "{$this->user} has {$this->wpts} warning points!";
print $final;
}
}
$link = @mysql_connect($host, $user, $pass) or die("Could not connect to database. Please try again later!");
@mysql_select_db($dbase, $link) or die("Could not select database. Please try again or try again later.");

$exe = new user($link);
$exe->warnUser('SDP2006','2');
$exe->printPts('SDP2006');

?>

Fuction warnUser() is okay, but I don't believe function printPts is. My errors are
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in c:\server\htdocs\warn.php on line 27

Notice: Use of undefined constant user - assumed 'user' in c:\server\htdocs\warn.php on line 28

Notice: Use of undefined constant wpts - assumed 'wpts' in c:\server\htdocs\warn.php on line 29

Anyone see what is wrong? Thanks alot.

- Stevie

SDP2006
01-02-2004, 11:05 PM
Okay, I got rid of all errors so
<?php
include('configed.php');

class user
{
var $db ;

function user( $db ){
$this->db=$db;
}

function warnUser($user,$wpts)
{
$this->user = $user;
$this->wp = $wpts;

$sql_insert = "UPDATE `users` SET `wpts`=`wpts` + '{$this->wp}' WHERE `user`='{$this->user}'";
mysql_query( $sql_insert , $this->db ) ;
$success = "{$this->user} recieved {$this->wp} warning points!<br />";
print $success;
}
function printPts($user)
{
$sql_select = "SELECT `user`,`wpts` FROM `users` WHERE `user`='$user'";
$result = mysql_query($sql_select, $this->db);
$sql= mysql_fetch_array($result);
$user = $sql['user'];
$wpts= $sql['wpts'];
$final = "$user has $wpts warning points!";
print $final;
}
}
$link = @mysql_connect($host, $user, $pass) or die("Could not connect to database. Please try again later!");
@mysql_select_db($dbase, $link) or die("Could not select database. Please try again or try again later.");

$exe = new user($link);
$exe->warnUser('SDP2006','2');
$exe->printPts('SDP2006');

?>
My problem now is in function printPts all that is printed is 'has warning points!' I dunno why. All help is welcome....

-Thanks

raf
01-03-2004, 01:02 AM
Firepages :
A handy note .. if you are only accessing one database host (and mostly that will be the case) , you dont even need to pass all the database connections around , once php has made a MySQL connection , it will reuse that connection wherever it can , unless you close it its always available...

I think that if you use mysql_connect(), that the connection is then automatically closed when the script is processed.

You need to use mysql_pconnect() if you want persistent connections and optimize your serverperformance with connectionpooling.

Note : using persistent connections will result in always having a number of 'sleeping connection' and requires more RAM.

SDP2006 : you should be extremely carefull when you use variables to store your accountdetails in like
<?php
$host = "localhost";
$dbase = "testing";
$user = "root";
$pass = "rootpass";
?>
Always set these variables explicitely to '' as soon as you no longer need them or unset() then !!

SDP2006
01-03-2004, 01:56 AM
Okay, so I changed it to mysq_pconnect() and I'm still having the problem above ^.

Thanks for all the help. Is is greatly appreciated.

mordred
01-03-2004, 02:16 AM
Have you made sure the SQL query works as expected? Try these steps:

1.) add "if (!$result) print mysql_error();" to see any potential MySQL error messages.
2.) print out the SQL query used
3.) run the query (as it was printed out!) in phpMyAdmin and check that it returns the expected result sets.

firepages
01-03-2004, 03:04 AM
Originally posted by raf
I think that if you use mysql_connect(), that the connection is then automatically closed when the script is processed.


yes , so in the scope of a script (eg page) unless you explicity close and reopen your connections the connection is not 'lost' until the script finishes.

eg one connection in 'config.inc' is valid thoughout the lifetime of the script that called it.

when you use mysql_pconnect the connection stays (or attempts to) stay in memory across requests hence the overhead an dis mostly best avoided unless you have a `difficult` connection or say a seperate data-tier.

SDP2006
01-03-2004, 01:37 PM
Originally posted by mordred
Have you made sure the SQL query works as expected? Try these steps:

1.) add "if (!$result) print mysql_error();" to see any potential MySQL error messages.
2.) print out the SQL query used
3.) run the query (as it was printed out!) in phpMyAdmin and check that it returns the expected result sets. Yes, I ran the code in PHPMyAdmin and it returned results as I expected.

I found the problem, it was my query. Not the query, but the WHERE user='{this->user}'. I had a user that didn't exist in the $user in the function.

Thanks for the help guys and the notes on mysql_pconnect.

Thanks



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum