ChrisCockram Posted February 8, 2009 Share Posted February 8, 2009 Hi all, This is my first post here so please go easy on me Basically I have made a user login site with php and mysql and it works fine. Today I wanted to add a "Welcome First Name" bit at the top of the site so I wrote this function and stored it in a class, (basically, it connects to the database, grabs the users first name and stores it in a session) but it keeps coming up with this error; Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/a2391794/public_html/classes/mysql.php on line 35 Ive looked at everything still cant find the solution. The class is as follows; <?php require_once 'inc/constants.php'; class Mysql { private $conn; function __construct() { $this->conn=new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME) or die('There was a problem connecting to the database.'); } function get_First_Name($un, $pwd) { $q = "SELECT firstname FROM users WHERE username = '$un' AND password = '$pwd' LIMIT 1"; $result=mysql_query($q, $this->conn) or die(mysql_error()); //<--------THE ERROR IS HERE $_SESSION['userfirstname'] = $result; mysql_close($this->conn) or die(mysql_error()); } } The constants.php is as follows; <?php // Define constants here define('DB_SERVER', 'mysql1.000webhost.com'); define('DB_USER', '*****************'); define('DB_PASSWORD', '***************'); define('DB_NAME', '**************'); I know that PHP is putting the $q query together correctly as I have echoed $q separately and it has displayed it with username and password intact. Can you see anything I'm missing? I really cant work this one out! Thanks a lot! Chris. Link to comment https://forums.phpfreaks.com/topic/144270-solved-help-with-a-mysql_query-error/ Share on other sites More sharing options...
ratcateme Posted February 8, 2009 Share Posted February 8, 2009 you are using the mysqli class but passing the class to mysql which is like very wrong try function get_First_Name($un, $pwd) { $q = "SELECT firstname FROM users WHERE username = '$un' AND password = '$pwd' LIMIT 1"; $this->conn->query($q, $this->conn) or die($this->conn->error); //<--------THE ERROR IS HERE $_SESSION['userfirstname'] = $result; $this->conn->close(); } also why are you making a class to handle your database queries but then closing the connection after the first one Scott. Link to comment https://forums.phpfreaks.com/topic/144270-solved-help-with-a-mysql_query-error/#findComment-757112 Share on other sites More sharing options...
ChrisCockram Posted February 8, 2009 Author Share Posted February 8, 2009 Hi Scott, Thanks for the speedy reply, I must admit, I'm new to php and mysql, so forgive me if my codes are a little mish-mashed I have implemented the code and and removed the closing of connections. Im now getting this error Warning: mysqli::query() expects parameter 2 to be long, object given in /home/a2391794/public_html/classes/mysql.php on line 35 Also what would be the best way of getting the result from the query to be put in to the session; $_SESSION['userfirstname'] = ????; Cheers. Chris. Link to comment https://forums.phpfreaks.com/topic/144270-solved-help-with-a-mysql_query-error/#findComment-757119 Share on other sites More sharing options...
ratcateme Posted February 8, 2009 Share Posted February 8, 2009 try $this->conn->query($q) or die($this->conn->error); //<--------THE ERROR IS HERE for the query line i must say i am not very fimilar with mysqli i know it is better than mysql but i think for getting the result $row = $result->fetch_assoc(); $_SESSION['userfirstname'] = $row['firstname']; should do it Scott. Link to comment https://forums.phpfreaks.com/topic/144270-solved-help-with-a-mysql_query-error/#findComment-757124 Share on other sites More sharing options...
ChrisCockram Posted February 8, 2009 Author Share Posted February 8, 2009 Nearly there, Im now getting; Fatal error: Call to undefined function fetch_assoc() in /home/a2391794/public_html/classes/mysql.php on line 36 could this be because $result isn't relating to anything? Sorry If I am coming across as a complete dimwit here Appreciate the help. Chris. Link to comment https://forums.phpfreaks.com/topic/144270-solved-help-with-a-mysql_query-error/#findComment-757126 Share on other sites More sharing options...
ratcateme Posted February 8, 2009 Share Posted February 8, 2009 yea thats it umm $this->conn->query($q) or die($this->conn->error); //<--------THE ERROR IS HERE should be $result = $this->conn->query($q) or die($this->conn->error); //<--------THE ERROR IS HERE also to avoid errors i add a check to see if the query returned any rows function get_First_Name($un, $pwd) { $q = "SELECT firstname FROM users WHERE username = '$un' AND password = '$pwd' LIMIT 1"; $result = $this->conn->query($q) or die($this->conn->error); if($result->num_rows == 1){ $row = $result->fetch_assoc(); $_SESSION['userfirstname'] = $row['firstname']; } $this->conn->close(); } Scott. Link to comment https://forums.phpfreaks.com/topic/144270-solved-help-with-a-mysql_query-error/#findComment-757128 Share on other sites More sharing options...
ChrisCockram Posted February 8, 2009 Author Share Posted February 8, 2009 Cheers, thanks alot Scott! Chris Link to comment https://forums.phpfreaks.com/topic/144270-solved-help-with-a-mysql_query-error/#findComment-757130 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.