mysql - Query All Rows Within a Table based on Id's in php -


i have table includes 53 rows, in database. variables in rows int id, varchar var, , int votes. can't figure out how query rows within table array. want able 'var' variable while referencing id. here have far, indeed query first row. (hence id = '1').

<?php  include_once "code.php"; $new_array = array(); $sql = mysql_query("select * variables id='1'"); while($row = mysql_fetch_array($sql)){ $new_array[ $row['id']] = $row; }; 

i tried using loop , sticking variable '1' is, that's not working either. if please show me how var variable referencing of id's great. thanks

you declare $new_array each row. should move outside while loop. also, try not use mysql_* functions because deprecated. try mysqli_* or better pdo.

include_once "code.php"; $sql = mysql_query("select * variables"); $new_array = array(); //this array should not inside while while($row = mysql_fetch_array($sql)){     $new_array[$row['id']] = $row;     //do else here if want }; 

using pdo

try {     $link= new pdo("mysql:host=$hostname;dbname=mysql", $username, $password);     echo "pdo connection object created"; } catch(pdoexception $e) {     echo $e->getmessage();     die(); }  $sql = "select * users"; //prepare query $stmt = $link->prepare($sql); //execute query binding variables $result = $stmt->execute(array()); $result = $stmt->fetchall(); $new_array = array(); foreach ($result $row) {     $new_array[$row['id']] = $row; } 

edit after comment:

remove where id = '1' query , have rows. can take var referenced id using:

echo $new_array[1]['var']; echo $new_array[2]['var']; 

Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -