Jump to content

need help regarding php/mysql fetching out rows from database using OOP


salmander

Recommended Posts

hi i am developing PHP OOP application, which I need help with. I am developing a class(fetch_assoc) which has a function db_connect. It simply connects to the database. Then i have getRows() function which extracts the rows from the database and then returns it. here is the code:

 

function __construct() {
	mysql_connect("localhost","root","") or die(mysql_error());
	mysql_select_db("TestProject") or die(mysql_error());


function getRows() {
	$sql = "SELECT * FROM Record";
	$this->arr2 = $this->queryString($sql);
	//Declaring that $return is an array
	$return = array();
	foreach ($this->arr2 as $key=>$value) {
		$return[$key] = $value;

	}//foreach ends here
	return $return;

}
function queryString($sql) {
	$query =  mysql_query($sql);
	$row = mysql_fetch_assoc($query);
	return $row;
}// function queryString ends here

 

then on the other page, i use this code to fetch out everything from the objects returned and display it....but I dont understand whats going wrong!

 

$class = new fetch_assoc;
$results = $class->getRows();

//echo out everything
foreach ($results as $result) {
foreach ($result as $key=>$value) {
	echo "hello";
	echo $key . ": " . $value;
}//foreach ($result as $key=>$value) ends here
}

 

 

Ok

Link to comment
Share on other sites

3 things:

- $row = mysql_fetch_assoc($query); 

That will fetch exactly one row.  You would need to while loop if you actually want to get more than the first row of the result set (assuming there is one)

- You have an associated array that you are foreaching through to make the exact same thing.  It doesn't make any sense.

- In my signature there's a link about having problems with mysql that you should take a look at.  It's probably relevant to debugging your problem. 

Link to comment
Share on other sites

3 things:

- $row = mysql_fetch_assoc($query); 

That will fetch exactly one row.  You would need to while loop if you actually want to get more than the first row of the result set (assuming there is one)

- You have an associated array that you are foreaching through to make the exact same thing.  It doesn't make any sense.

- In my signature there's a link about having problems with mysql that you should take a look at.  It's probably relevant to debugging your problem.

 

yes I was using while loop before to get all the rows in the database, but it wasnt working so i thought that let me simplify it and see the problem then later on, I will add the while loop later. And if I am not wrong you use for each loop to iterate through all the elements in the assosiative array.

 

Appreciate your effort in helping me,

Thanks

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.