teen342 Posted February 17, 2008 Share Posted February 17, 2008 Hey there I seem to have been having problems getting this peice of code to work on a MySQL 4.x server but the code given was wrote for a MySQL 5.x server. P.S. I have commented out the mysqli version which came with the example given. [pre] 41 // Define the read_session() function: 42 // This function takes one argument: the session ID. 43 // This function retrieves the session data. 44 function read_session($sid) { 45 46 global $sdbc; 47 48 // Query the database. 49 #$q = sprintf('SELECT data FROM session WHERE id="%s"', mysqli_real_escape_string($sdbc, $sid) ); 50 #$r = mysqli_query($sdbc, $q); 51 $q = sprintf('SELECT data FROM session WHERE id="%s"', mysql_real_escape_string($sid, $sdbc) ); 52 $r = @mysql_query($q); 53 54 // Retrieve the results. 55 #if(mysqli_num_rows($r) == 1) { 56 if(mysql_num_rows($r) == 1) { 57 58 #list($data) = mysqli_fetch_array($r, MYSQLI_NUM); 59 list($data) = mysql_fetch_array($r, MYSQL_NUM); 60 61 // Returns the data: 62 return $data; 63 64 } else { 65 66 return ''; 67 68 } 69 70 } [/pre] The error I am getting is Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/users/uks61947/html/teen342online.me.uk/projects/php5adv/ch03/includes/db_sessions.inc.php on line 56 Quote Link to comment https://forums.phpfreaks.com/topic/91503-converting-mysqli-to-mysql-functions/ Share on other sites More sharing options...
uniflare Posted February 17, 2008 Share Posted February 17, 2008 an invalid result resource usually means the query returned false or errored. Change line 52 to: $r = @mysql_query($q) or die("MYSQL QUERY ERROR: <br>".mysql_error()."<hr>"); See if mysql is saying anything, could be a problem with your query. Unless your query result is empty, try the exact same query in phpmyadmin if u can. Quote Link to comment https://forums.phpfreaks.com/topic/91503-converting-mysqli-to-mysql-functions/#findComment-468708 Share on other sites More sharing options...
wildteen88 Posted February 17, 2008 Share Posted February 17, 2008 The problem is you're using both mysqli_* and mysql_* functions. You cannot use both function types together they are incompatible. You'll have to convert all mysqli_* functions to the old mysql_* functions if you dont want to use mysqli based functions. However mysqli_* functions are compatible with MySQL4.1.3 or above according to php.net. Your mysql queries are most probably the issue and not the code. Quote Link to comment https://forums.phpfreaks.com/topic/91503-converting-mysqli-to-mysql-functions/#findComment-468773 Share on other sites More sharing options...
teen342 Posted February 20, 2008 Author Share Posted February 20, 2008 Have looked into it and found that the resource in question was reciving a false response due to a little boo boo in the SQL! wildteen88, thanx but if you look closer you will see that all mysqli_ functions are commented out so is not that. Heres the corrected code: [pre]53 // Define the read_session() function: 54 // This function takes one argument: the session ID. 55 // This function retrieves the session data. 56 function read_session($sid) { 57 58 global $sdbc; 59 60 // Query the database. 61 #$q = sprintf('SELECT data FROM session WHERE id="%s"', mysqli_real_escape_string($sdbc, $sid) ); 62 #$r = mysqli_query($sdbc, $q); 63 $q = sprintf('SELECT data FROM sessions WHERE id="%s"', mysql_real_escape_string($sid, $sdbc) ); 64 $r = mysql_query($q); 65 66 // Retrieve the results. 67 #if(mysqli_num_rows($r) == 1) { 68 if(mysql_num_rows($r) == 1) { 69 70 #list($data) = mysqli_fetch_array($r, MYSQLI_NUM); 71 list($data) = mysql_fetch_array($r, MYSQL_NUM); 72 73 // Returns the data: 74 return $data; 75 76 } else { 77 78 return ''; 79 80 } 81 82 } [/pre] Quote Link to comment https://forums.phpfreaks.com/topic/91503-converting-mysqli-to-mysql-functions/#findComment-471957 Share on other sites More sharing options...
wildteen88 Posted February 20, 2008 Share Posted February 20, 2008 Change line 64 to: $r = mysql_query($q) or die('Query error: ' . mysql_error() . '<pre>' . $q . '</pre>'); There is most probably a problem with your query. Quote Link to comment https://forums.phpfreaks.com/topic/91503-converting-mysqli-to-mysql-functions/#findComment-471988 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.