hance2105 Posted June 21, 2013 Share Posted June 21, 2013 can anyone please tell me what is wrong in this piece of code? am having error where i mentioned in the title... <?php if($_GET['action']=='add'){ echo "<div>" . $_GET['prod_name'] . " was added to your favourites.</div>"; } if($_GET['action']=='exists'){ echo "<div>" . $_GET['prod_name'] . " already exists in your favourites.</div>"; } require "db_connect.php"; $query = "SELECT prod_id, prod_name, prod_price FROM tbl_product"; $stmt = $con->prepare( $query ); $stmt->execute(); $num = $stmt->rowCount(); Link to comment https://forums.phpfreaks.com/topic/279417-error-at-these-lines-if_getactionadd-if_getactionexists-and-stmt-con-prepare-query/ Share on other sites More sharing options...
boompa Posted June 21, 2013 Share Posted June 21, 2013 You need to start telling us what the errors are! If it's what I think, then it's really the same answer as your last question, so read this and understand it: If you try to access an element of an array and that element doesn't exist, you will get a notice from PHP (not truly an error). The way to prevent this is to check if the element of the array exists by calling isset($array['index']) before you try to access the element. Just like I wrote in the last question about your SESSION variable. Link to comment https://forums.phpfreaks.com/topic/279417-error-at-these-lines-if_getactionadd-if_getactionexists-and-stmt-con-prepare-query/#findComment-1437202 Share on other sites More sharing options...
denno020 Posted June 21, 2013 Share Posted June 21, 2013 Edit: Appears boompa beat me to the submit button . Well for some proper help you'll need to share your error as well, otherwise we've got nothing to go on.. But I will have a guess that you're getting an error saying that $_GET['action'] isn't set? You need to do it like this: if(isset($_GET['action']) && $_GET['action'] == 'add'){ .... } As for the other part, I'm not sure from looking at it what the error might be, let us know and I'm sure we'll be able to help more. Denno Link to comment https://forums.phpfreaks.com/topic/279417-error-at-these-lines-if_getactionadd-if_getactionexists-and-stmt-con-prepare-query/#findComment-1437203 Share on other sites More sharing options...
hance2105 Posted June 21, 2013 Author Share Posted June 21, 2013 thank you for the help denno...it solved out part of my issue am still having the following error - fatal error: call to a member function prepare() on a non-object am having it due to the line $stmt = $con->prepare( $query ); can you tell me why please? Link to comment https://forums.phpfreaks.com/topic/279417-error-at-these-lines-if_getactionadd-if_getactionexists-and-stmt-con-prepare-query/#findComment-1437206 Share on other sites More sharing options...
denno020 Posted June 21, 2013 Share Posted June 21, 2013 Where's the code that initializes the $con variable? Link to comment https://forums.phpfreaks.com/topic/279417-error-at-these-lines-if_getactionadd-if_getactionexists-and-stmt-con-prepare-query/#findComment-1437207 Share on other sites More sharing options...
hance2105 Posted June 21, 2013 Author Share Posted June 21, 2013 here it is $con = new PDO("mysql:host={$host};dbname={$db_name}", $username, $password); is forms part of my old database connection file my new database connection file does not have it how can i proceed now... Link to comment https://forums.phpfreaks.com/topic/279417-error-at-these-lines-if_getactionadd-if_getactionexists-and-stmt-con-prepare-query/#findComment-1437209 Share on other sites More sharing options...
hance2105 Posted June 21, 2013 Author Share Posted June 21, 2013 new database file has this one $con=mysql_connect("localhost",".....","....."); Link to comment https://forums.phpfreaks.com/topic/279417-error-at-these-lines-if_getactionadd-if_getactionexists-and-stmt-con-prepare-query/#findComment-1437210 Share on other sites More sharing options...
denno020 Posted June 21, 2013 Share Posted June 21, 2013 I find it a bit weird that you're using PDO in your 'old' database connection and mysql_connect() in your new one, because mysql_connect has been deprecated.. I suggest you move back to using PDO. And you will need to make sure you actually have a correct PDO object in the $con variable, so var_dump() it directly after initializing it, and see what it is Link to comment https://forums.phpfreaks.com/topic/279417-error-at-these-lines-if_getactionadd-if_getactionexists-and-stmt-con-prepare-query/#findComment-1437211 Share on other sites More sharing options...
hance2105 Posted June 21, 2013 Author Share Posted June 21, 2013 in case i continue using the mysql_connect thing, how can i bypass that $con. if i remove the $con from the piece of code i will have undefined $stmt...how can i define it then? Link to comment https://forums.phpfreaks.com/topic/279417-error-at-these-lines-if_getactionadd-if_getactionexists-and-stmt-con-prepare-query/#findComment-1437213 Share on other sites More sharing options...
denno020 Posted June 21, 2013 Share Posted June 21, 2013 Well if you want to use mysql_connect, which again, I suggest you dont, but if you do, then that will create a connection to the database. You will then use that in another function, mysql_query(), which will take a string as the first paramater, which is the query, and the second parameter is your $con variable (which represents the connection to the database). But seriously, you should learn to use PDO. It's really not hard, and will mean your code won't become useless when these functions are eventually removed from php.. Link to comment https://forums.phpfreaks.com/topic/279417-error-at-these-lines-if_getactionadd-if_getactionexists-and-stmt-con-prepare-query/#findComment-1437214 Share on other sites More sharing options...
hance2105 Posted June 21, 2013 Author Share Posted June 21, 2013 ok thanks....i know my friend i should learn that...but am nearly at the end of my project and a bit too late for that. i changed the code to this $query ="SELECT prod_id, prod_name, prod_price FROM tbl_product"; $result = $mysql_query( $query ); but i need to alter this piece as well $num = $stmt->rowCount(); how do i write this one? Link to comment https://forums.phpfreaks.com/topic/279417-error-at-these-lines-if_getactionadd-if_getactionexists-and-stmt-con-prepare-query/#findComment-1437215 Share on other sites More sharing options...
denno020 Posted June 21, 2013 Share Posted June 21, 2013 look into using yet another deprecated function, mysql_num_rows() Link to comment https://forums.phpfreaks.com/topic/279417-error-at-these-lines-if_getactionadd-if_getactionexists-and-stmt-con-prepare-query/#findComment-1437217 Share on other sites More sharing options...
hance2105 Posted June 21, 2013 Author Share Posted June 21, 2013 will it be like this $num = mysql_num_rows($result; wat abt the rowCount(); thing? Link to comment https://forums.phpfreaks.com/topic/279417-error-at-these-lines-if_getactionadd-if_getactionexists-and-stmt-con-prepare-query/#findComment-1437242 Share on other sites More sharing options...
denno020 Posted June 22, 2013 Share Posted June 22, 2013 rowCount is for PDO, which you're not using. But yes, with the closing bracket a the end of $result, that is how you use it. Link to comment https://forums.phpfreaks.com/topic/279417-error-at-these-lines-if_getactionadd-if_getactionexists-and-stmt-con-prepare-query/#findComment-1437332 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.