Reputation: 125
I'm relatively new to php and am in the process of transferring a search engine i wrote to OOP.
Relevant SQL connection code/sqlconnect1 class:
public function query($query){
$this->stmt = $this->dbh->prepare($query);
}
public function bind($param, $value, $type = null){
if (is_null($type)) {
switch (true) {
case is_int($value):
$type = PDO::PARAM_INT;
break;
case is_bool($value):
$type = PDO::PARAM_BOOL;
break;
case is_null($value):
$type = PDO::PARAM_NULL;
break;
default:
$type = PDO::PARAM_STR;
}
}
$this->stmt->bindValue($param, $value, $type);
}
public function execute(){
return $this->stmt->execute();
}
public function resultset(){
$this->execute();
return $this->stmt->fetch(PDO::FETCH_ASSOC);
}
While loop:
$search = new LoadSearch;
$sqlconnect1 = new sqlconnect1;
$sqlconnect1->query("SELECT * FROM data1 WHERE address_city LIKE 'austin' AND address_zip LIKE '78758'");
//Assign Variables and Display Record Results
while ($row1 = $sqlconnect1->resultset()){
$Name1=$row1['name'];
$Address1=$row1['address_1'];
$City1=$row1['address_city'];
$State1=$row1['address_state'];
$ZIP1=$row1['address_zip'];
$Country1=$row1['address_country'];
$Phone1=$row1['phone'];
$Website1=$row1['website'];
$Category_11=$row1['category_1'];
$Category_21=$row1['category_2'];
echo"
<!--Table 2-->
<table class='table2'>
<tr>
<td></td><td><b>$Name1</b></td>
</tr>
<tr>
<td></td><td>$Address1, $City1, $State1 $ZIP1</td>
</tr>
<tr>
<td></td><td>$Country1</td>
</tr>
<tr>
<td></td><td>$Phone1</td>
</tr>
<tr>
<td></td><td>$Category_11</td>
</tr>
</table>";
Whenever the page is loaded, it repeats the first query result indefinitely and never moves beyond the first result. Any advice would be greatly appreciated. A similar setup worked fine when it was procedural.
Upvotes: 0
Views: 69
Reputation:
Remove the $this->execute()
call, that's because you call on every loop iteration execute and then fetch. In such way you're going to retrieve always the same result (the first row)
public function resultset(){
// $this->execute(); -- Remove this line
return $this->stmt->fetch(PDO::FETCH_ASSOC);
}
Obviously you need to call $sqlconnect1->execute()
before your while loop
Upvotes: 3