PHP Logic Question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
jthermane24
Forum Newbie
Posts: 16
Joined: Wed Feb 17, 2010 8:15 pm
Location: New Jersey

PHP Logic Question

Post by jthermane24 »

This is more of a logic question, then a solve this specific error problem because I'm sure it's going to come up again in the development of my website at http://www.jerrysmith.co.cc (man I love free plugs for my site :)).

Okay so I have a functions.php file filled with a bunch of functions that I use across all pages of my website (databaseconnect, siteoption, ect, ect) but I'm running into a problem with one called getUserInfo. Through the login script required to view the page, it sets the username, among other data, into a session. I'm trying to run that session through the following php function and return the array to the page where I'll run it through a while statement to get the data into the page, but I'm having trouble
  1. Getting the username to run through the function (username required text displays)
  2. Getting the username metadata to be inserted into the hidden values of the form through a php echo (I know how to script it it's just not working)
This is the error I'm getting.
Warning: mysql_fetch_array() expects parameter 1 to be resource, string given in /home/jerrysmi/public_html/account/famchat/index.php on line 234
Here is the getUserInfo function and the form where the UserInfo has to be displayed.

function getUserInfo

Code: Select all

function getUserInfo($username = ' '){
	  database_connect('Admin');
	  if(strlen($username > 0)) {
			$sql = "SELECT * FROM `cmsusers` WHERE `username` ='$username'";
			$result = mysql_query($sql);
			/* Return result array */
			$_SESSION['dbarray'] = mysql_fetch_array($result);
	  }
	  else {
			$_SESSION['dbarray'] = 'Username required';
	  }	 
	  mysql_close();
} // getUserInfo
html/php form

Code: Select all

<fieldset>
<p style="margin-left:auto; margin-right:auto; text-align:center">
<legend>Add Comment</legend>
<?php
	$username = $_SESSION['myusername'];
	getUserInfo($username);
	while ($dbarray = mysql_fetch_array($_SESSION['dbarray'])) {?>
  <form name="chat" action="script.php" method="post">
 	  <input type="hidden" name="famchat_author" value="<?php echo $dbarray['firstname'] . ' ' . $dbarray['surname'] ?>" />
		<input type="hidden" name="famchat_timestamp" value="<?php echo(time(u)); ?>" />
   	<h3 id="nobottom">Your comment:</h3><br />
    <textarea name="famchat_comment" id="MCEEditor" rows="10" style="width:100%"></textarea>
    <br />
    <input type="submit" name="submit" style="margin:0;padding:2px 5px;color:#666666" />
    <input type="reset" style="margin:0;padding:2px 5px;color:#666666" />
	</form>
<?php } unset($_SESSION['dbarray']); ?>
  </fieldset>
Again I'm not looking for a specific fix, more of a overall answer so I can implement the function throughout the website later. "Give a man a fish he eats for a day, teach a man to fish he eats for a lifetime"
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: PHP Logic Question

Post by cpetercarter »

The problem is probably that mysql_query() is returning "false" rather than a resource. And the reason for that is probably the single quotation marks around the table name 'cmsusers' in the query - they are not needed and should be removed.
User avatar
jthermane24
Forum Newbie
Posts: 16
Joined: Wed Feb 17, 2010 8:15 pm
Location: New Jersey

Re: PHP Logic Question

Post by jthermane24 »

I'm pretty sure that's the proper MySQL syntax (I generated the code in phpMyAdmin) so I don't think that's my problem but I can try something that this post jarred in my memory. I'll let you know how it works out.
Post Reply