0

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!

6
  • 1
    How about fetchColumn? Commented Sep 30, 2013 at 16:22
  • @Mihai: column could not be fetched. But elements could be with similar query I mentioned. But I dont know currect syntax. Commented Sep 30, 2013 at 16:26
  • @Mihai: In mysql select * from view_name where ind_type='pcount'; gives me 157. Same thing I wanted in php and store in some variable. Your reputation does not owe this sort of description! Commented Sep 30, 2013 at 16:35
  • So what prevents you from using mysql assoc and do $variable=$row['pcount'] and in your query you use WHERE column='value' or LIMIT 1 or instead of while do if? Commented Sep 30, 2013 at 16:38
  • 1
    Just a reminder, you should never use mysqli_* as it is outdated (and scheduled for removal in PHP6 I believe). Commented Sep 30, 2013 at 16:49

1 Answer 1

1

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'); 
Sign up to request clarification or add additional context in comments.

7 Comments

thanks a lot. Anyways for both pcount and ncount in my table $mysqli->prepare("SELECT Index_val FROM view_name where ind_type= ? ?") or what?
And also you recommended not to use mysqli-> that what is it's alternative? even you also have used it. Just for my info
for both values . either you dont need a condition , fetch all or make an OR condition like that where ind_type= ? OR where ind_type= ?
thanks a lot. And binding should be done like this $p->bind_result($Index_val1,$Index_val2); for both values right?
with that variable you can do what you like with it. to filter which value u need.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.