Jump to content

Converting MySQLI to MySQL functions


teen342

Recommended Posts

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

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.