cripin Posted May 12, 2013 Share Posted May 12, 2013 started learning php last week, at first didn't know anything about it so used mysql, now yesterday reading some article i saw that i should be using mysqli instead of mysql, so i changed all the code for my registration and login pages every works fine. However can't figure out the mysql_result to mysqli, couldn't find it on google toothe code:the error im getting:Warning: mysql_result() expects parameter 1 to be resource, object given in /Applications/XAMPP/xamppfiles/htdocs/index.php on line 12<?phprequire 'conect.php';$user_ip = $_SERVER['REMOTE_ADDR'];function update_count(){ global $dbconnect;$query = "SELECT count FROM hits_count";if ($query_run = mysqli_query($dbconnect, $query)){$count = mysql_result($query_run, 0, 'count');echo $count;}}update_count();?> Link to comment https://forums.phpfreaks.com/topic/277928-very-short-code-mysql_result-to-mysqli-please-help/ Share on other sites More sharing options...
mac_gyver Posted May 12, 2013 Share Posted May 12, 2013 afaik, there's no direct equivalent to msyql_result, because it is so inefficient at what it does. you need to fetch the row and access the value. you can do this all at once using - list($count) = mysqli_fetch_row($query_run); Link to comment https://forums.phpfreaks.com/topic/277928-very-short-code-mysql_result-to-mysqli-please-help/#findComment-1429716 Share on other sites More sharing options...
cripin Posted May 12, 2013 Author Share Posted May 12, 2013 thanks Link to comment https://forums.phpfreaks.com/topic/277928-very-short-code-mysql_result-to-mysqli-please-help/#findComment-1429718 Share on other sites More sharing options...
Barand Posted May 12, 2013 Share Posted May 12, 2013 As an aside, do not use globals. Pass the connection to the function as a parameter. If you inadvertently assign a value to the global inside a function you could spend hours in a large script trying to find why the whole script is screwed. function update_count($db) { $query = "SELECT count FROM hits_count"; $count = 0; if ($query_run = mysqli_query($db, $query)) { list($count) = mysqli_fetch_row($query_run); } return $count; } $count = update_count($dbconnect); Link to comment https://forums.phpfreaks.com/topic/277928-very-short-code-mysql_result-to-mysqli-please-help/#findComment-1429722 Share on other sites More sharing options...
cripin Posted May 12, 2013 Author Share Posted May 12, 2013 On 5/12/2013 at 11:26 AM, Barand said: As an aside, do not use globals. Pass the connection to the function as a parameter. If you inadvertently assign a value to the global inside a function you could spend hours in a large script trying to find why the whole script is screwed. function update_count($db) { $query = "SELECT count FROM hits_count"; $count = 0; if ($query_run = mysqli_query($db, $query)) { list($count) = mysqli_fetch_row($query_run); } return $count; } $count = update_count($dbconnect); Can i include it inside the function parameter even if I already have a parameter inside it? the parameter is the name of another function. Basically can i have 2 paramaters in a function and if so should the connection one come before or after Link to comment https://forums.phpfreaks.com/topic/277928-very-short-code-mysql_result-to-mysqli-please-help/#findComment-1429732 Share on other sites More sharing options...
Barand Posted May 12, 2013 Share Posted May 12, 2013 You can have many parameters but you must call the function with parameters in the correct order as defined in the function definitions Link to comment https://forums.phpfreaks.com/topic/277928-very-short-code-mysql_result-to-mysqli-please-help/#findComment-1429736 Share on other sites More sharing options...
cripin Posted May 12, 2013 Author Share Posted May 12, 2013 so say if the function has 3 parameters and the one you wanna use is the 3rd one, would it calling the function look something like this: myfunction('','',$myparameter); or my function($myparameter) ; or something else? Link to comment https://forums.phpfreaks.com/topic/277928-very-short-code-mysql_result-to-mysqli-please-help/#findComment-1429738 Share on other sites More sharing options...
Barand Posted May 12, 2013 Share Posted May 12, 2013 http://www.php.net/manual/en/language.functions.php Link to comment https://forums.phpfreaks.com/topic/277928-very-short-code-mysql_result-to-mysqli-please-help/#findComment-1429740 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.