user123
user123

Reputation: 5407

Fetching values from database and storing in variable

I want to get database column values in some variable. I dont want to use

mysql_fetch_assoc
mysql_fetch_row
mysql_fetch_array
mysql_fetch_object

anyone of this.

My table is:

+----------+-----------+
| ind_type | Index_val |
+----------+-----------+
| pcount   |       157 |
| ncount   |       210 |
+----------+-----------+

I want to get 157, 210 in some variable $x and $y. I am working in PHP. Can some one guide me.?

One can do $p= $mysqli->query('SELECT Index_val FROM view_name where ind_type=pcount'); and then iterate through all rows to fetch all values.

But I have only two rows and for my need I want to manually fetch the values of Index_val. IF someone can sort it out!

Upvotes: 0

Views: 1974

Answers (1)

echo_Me
echo_Me

Reputation: 37233

what about this ?

if ($p = $mysqli->prepare("SELECT max(CASE when ind_type= 'pcount' then Index_val end) as x , 
                                  max(CASE when ind_type= 'ncount' then Index_val end) as y
   FROM view_name ")){

    $p->execute(); 
    $p->store_result();
    $p->bind_result($x , $y);
    $p->fetch() ;
     }
    echo $x ."<br />". $y ;

where ind_type=pcount then in your table will get only 157 is it only matched value.

EDIT:

for both values . either you dont need a condition , fetch all or make an OR condition.

if ($p = $mysqli->prepare("SELECT Index_val FROM view_name where ind_type= ? OR ind_type= ? ")){
 $p->bind_param('ii', 'pcount' , 'ncount'); 

Upvotes: 1

Related Questions