I saw the previous thread on this topic but the resolution failed to solve my problem. I am and definately feel, like a total noob on php. I was toying around with user authentication and i can't seem to get queries to work. At all. It's depressing, I've tried super global, mysql_query auto connect functions, and changing up my tables. I still seem to have the same error. It blows on a mastodon scale. The code in question is: <?php require_once( "db_connect.php" ); $conn = db_connect(); $query= "select * from user where email='".$email."'"; function register($email, $password, $fname, $lname) { $result = $conn-> mysql_query ($query); if (!$result) { throw new exception('Query error'); } if ($result->num_rows>0) { throw new exception ('that email is in use, find another.'); } $result = $conn->query("insert into user values ( '".$email."', sha1('".$password."'), '".$fname."', '".$lname."')"); if (!$result) { throw new exception ('could not register you, database error,failsauce.'); } return true; } ?> The code is intended to be a registering function in a larger try construc register code. thnx alsso srory fro teh badd sppellin
Also the error was Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\data_valid_fns.php on line 7 Fatal error: Call to a member function query() on a non-object in C:\xampp\htdocs\function_register.php on line 25
the data base is email varchar(100) no pri null passwrd char(40) no null fname varchar(30) no null lname varchar(30) no null not sure if you guys need that but i thott i'd include it.
also the connect is good I just checked, so is the db user and password, as well as the syntax on the query i just ran it through the shell
The function is used within this block of code <?php Require_once( 'login_con2.php' ); $fname=$_POST['firstname']; $lname=$_POST['lastname']; $email=$_POST['email']; $pwrd=$_POST['password']; $pwrd2=$_POST['password2']; session_start(); try { if (!filled_out($POST)) { throw new Exception ('The form is not filled out.' ); } if (!valid_email($email)) { throw new Exception('Not a valid email adress'); } if ($pwrd != $pwrd2) { throw new Exception ('passwords do not match'); } register ($fname, $lname, $email, $pwrd); $_SESSION['valid_user'] = $fname; } catch (Exception $e) {} ?>
why on some places u r using $conn-> mysql_query and on some places u r using $conn->query? it looks like u r typing wrong function name
<?php require_once( "db_connect.php" ); global $query; function register($email, $password, $fname, $lname) { $conn = db_connect(); //$query= $result = $conn-> query ("select * from user where email='".$email."'"; if (!$result) { throw new exception('Query error'); } if ($result->num_rows>0) { throw new exception ('that email is in use, find another.'); } $result = $conn->query("insert into user values ( '".$email."', sha1('".$password."'), '".$fname."', '".$lname."')"); if (!$result) { throw new exception ('could not register you, database error, contact me, and try again later.'); } return true; } ?> I also attempted to make the query variable a superglobal it did ni work
Are you sure your db_connect returns object ? It may fail somehow. If you want access global $query inside register() then declare global $query inside it. $query= "select * from user where email='".$email."'"; function register($email, $password, $fname, $lname) { global $query; ... } PHP:
no luck. Just tried still comes out with the same error. I was reading the error report again. It mentions non-object. Perhaps as in object oriented php. Do i have to give $result a constant class or definition. I kinda skimed over that part of php and moved on. if that is the problem how do it do this and where?
why dont u use mysql_real_escape_string() function with every variable u r using in any query may be the query has some syntax error becoz of some ' char that is why the query is throwing the syntax error and becoz of that the object is not being created
I tried this $query = "select * from users where email ='%s'"; mysql_real_escape_string($email); got a failed to connect with blah user and blah password. is there a way to change the login in parameters for this function?
Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\xampp\htdocs\function_register.php on line 15 Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in C:\xampp\htdocs\function_register.php on line 15 Fatal error: Call to a member function query() on a non-object in C:\xampp\htdocs\function_register.php on line 19 the good news is i fixed the for each error
The call to a member function error is caused by you not declaring the class as a new object/variable in the function_register.php document. I don't know the contents of the db_connect.php file, but this is what you need to do... Just add the below code (modify it to work for you) to the top of your document somewhere.. $conn = new NAMEOFYOURCONNECTIONCLASS; PHP: That should help with at least that error.. Let me know!
lol i tried webcoding because i didn't want to learn oop, but this sounds like it might work . I'm going to suck it up and try to cram it. then code what you just said into my function. while i'm at it do i define it in the fuction, db_connect, or separate file also this is db_connect: <?php function db_connect() { $result = new mysqli('-------', '-----', ----', '------'); if (!result) { throw new Exception('could not connect to database'); } else { return $results; } } ?>
20 or so pages of oophp and i think i know what i need to do. brett do i create a class around query, create an object for it then call the object where i would normally call the query variable? also in my reading they stressed that oophp made php more modular. how is oophp better than just encapsulating my php code in functions then calling the functions in view pages?
actually would it be possible for me to create a master class with all variables, functions and objects to be used in the project and then just inherit all of the definitions and atributes? then maybe my query could be defined.
alright so i created a class around db_connect.php literally i just put class connect{ the connecttion function} now i get Call to undefined method connect::query() in C:\xampp\htdocs\function_register.php on line 20 does this mean my query needs to be in a class or that i made the connect class wrong db_connect.php is now <?php class connect { function db_connect() { $result = new mysqli('-------', '-----', ----', '------'); if (!result) { throw new Exception('could not connect to database'); } else { return $results; } } } ?> the only change to the register code was this $conn = new connect; do i create an object for the $conn variable that includes the function db connect if so how?
This is what I would usually do.... In db_connect.php: <?php class db{ function connect(){ $con = mysql_connect(PUTYOURINFOHERE); $db = mysql_select_db(DB); } function query($query){ if(!isset($con)){ echo "ERROR: Not connected to database; Please connect"; } else { $query = mysql_query($query); }} function close(){ if(isset($con)){ mysql_close($con); } else { echo "ERROR: Not connected to database; Could not close DB"; }}} ?> PHP: Then in register.php: <?php include('db_connect.php'); $db = new db; ?> PHP: To connect: <?php $db->connect(); ?> PHP: To make a query: <?php $sql = "SELECT * from `members` WHERE id='1'"; $db->query($sql); ?> PHP: To close DB: <?php $db->close(); ?> PHP: That should work.. but I did that very quickly. Let me know if any errors occur so I can fix them. -Brett
Well you solved the error reports.Unfortunetly the page just reders blank. I made some errors on purpose but still blank. mysql shell reads no input. I've started to completely rewrie the page from the grounds up. anyway register_function <?php require_once( "db_connect.php" ); function register($email, $password, $fname, $lname) { $db = new db; $db->db_connect(); $query= "select * from users where email ='.$email.'"; $result = $db-> query ($query); //global $query; if (!$result) { throw new exception('Query error'); } $db->close(); if ($result->num_rows>0) { throw new exception ('that email is in use, find another.'); } $result = $conn->query("insert into user values ( '".$email."', sha1('".$password."'), '".$fname."', '".$lname."')"); if (!$result) { throw new exception ('could not register you, database error, contact me, and try again later.'); } return true; } ?> db_connect is <?php class db{ function db_connect() { $conn = mysql_connect ('--------', '-----', '----', '------'); $db = mysql_select_db(DB); } function query($query) { if(!isset($con)) { echo "ERROR"; } else { $query = mysql_query($query); }} function close() { if(isset($con)) { mysql_close($con); } else { echo "error"; }}} ?> and the register page is <?php Require_once( 'login_con2.php' ); $fname=$_POST['firstname']; $lname=$_POST['lastname']; $email=$_POST['email']; $pwrd=$_POST['password']; $pwrd2=$_POST['password2']; $form_vars=$_POST["form_vars"]; session_start(); try { if (!filled_out($form_vars)) { throw new Exception ('The form is not filled out.' ); } if (!valid_email($email)) { throw new Exception('Not a valid email adress'); } if ($pwrd != $pwrd2) { throw new Exception ('passwords do not match'); } register ($fname, $lname, $email, $pwrd); $_SESSION['valid_user'] = $fname; echo "winn"; } catch (Exception $e) {} ?> Brett, you are hero