I am trying to inset data to a numeric array php graph.
I tried to do it this way:
$query = "select id,sales from riders";
$results = mysql_query($query);
while($row = mysql_fetch_array($results)) {
//$rider[i]=$row['id'];
$rider[$counter]=$row['sales'];
$counter++;
}
and insert into data array as follows:
$data = array($rider[0],$rider[1],$rider[2],$rider[3],$rider[4]);
It gives me error.. Please help me... Thank you very much for your kind help.....
Add $counter = 0; before the while loop.
PS: You can just use $data[] = $row['sales']; in the loop without the $counter.
Use $rider[],
$rider=array();
while($row = mysql_fetch_array($results))
{
$rider[]=$row['sales'];
}
Related
I'm trying to create an array(); from the mysql results/query.
The Array OUTPUT should look exactly like this:
{"1":{"info":"Rooz","lat":51.503363,"lng":-0.127625},
"2":{"info":"Michelle","lat":51.503343,"lng":-0.127567}}
So I tried to do it this:
$sql = "SELECT id, name, lat, lng FROM positions ORDER BY id";
$query = mysqli_query($db_conx, $sql);
$productCount = mysqli_num_rows($query); // count the output amount
$testLocs = array();
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
$testLocs[] = $row;
}
print_r(json_encode($testLocs));
but the OUTPUT of the code above is like this:
[{"id":"1","name":"Rooz","lat":"51.5033630","lng":"-0.1276250"},{"id":"2","name":"Michelle","lat":"51.503343","lng":"-0.127567"}]
I have no idea how to achieve the output that I want in an array.
Could someone please advise on this?
any help would be appreciated.
Change
$testLocs[] = $row;
to
$testLocs[$row["id"]] = array("info"=>$row["name"], "lat"=>$row["lat"], "lng"=>$row["lng"]);
Addition to #Sean's answer: You could also use (array) $row instead of create the array manually with every key:
$testLocs[$row["id"]] = (array) $row;
If you change your query in the future, you don't have to change this line.
PHP arrays are not built in a distinct way from JSON objects:
$testLocs[$row["id"]] = $row;
If you purposely want to exclude the $id property from $row you can remove it from the $row array first.
$id = $row["id"];
unset($row["id"]);
$testLocs[$id] = $row;
$result = mysql_query("SELECT * FROM Race");
$rows = mysql_num_rows($result);
for ($i = 0 ; $i < $rows ; ++$i)
{
$row = mysql_fetch_row($result);
echo $row[0];
}
above is probably an awkward method but it'll print out all datas stored in first column, which is good but now, I want to store each one of them into an array...
I tried
$array[$i]=$row[0];
and echoed it out, but it just prints"Array"...
I tried
$result = mysql_query("SELECT raceid FROM Race");
while ($row = mysql_fetch_array($result)) {
$array[]=$row[0];
}
...which does the same as code written before, i guess, since it too just print "Array".
Please help! Thank you!!
Do you use simple echo $array;? It's wrong. You can't output array this way.
Use this:
$array = array();
$result = mysql_query("SELECT raceid FROM Race");
while ($row = mysql_fetch_array($result)) {
$array[]=$row[0];
}
foreach($item in $array) {
echo $item."<br>"; // and more format
}
If you want to watch array contents without any format use (e.g. for debugging) print_r or var_dump:
print_r($array);
var_dump($array);
Advice: better to use assoc array.
$array = array();
$result = mysql_query("SELECT raceid FROM Race");
while ($row = mysql_fetch_array($result)) {
$array[]=$row['raceid'];
}
Advanced advice: better to use PDO and object results.
You SQL code will be invulnerable to SQL injections
Code will be more modern and readable.
I have an MSSQL query where it SELECTS all rows that meet a specific criteria. In PHP I want to fetch that array, but then I want to convert it into one array per row. So if my query returns 3 rows, I want to have 3 unique arrays that I can work with.
I'm not sure how to go about this. Any help would be greatly appreciated!
Thanks, Nathan
EDIT:
$query = "SELECT * FROM applicants WHERE applicants.user_id ='{$_SESSION['user_id']}'";
$query_select = mssql_query($query , $connection);
if (mssql_num_rows($query_select) == 2){
$message = '2 students created successfully';
}
$i = 0;
while($row = mssql_fetch_array($query_select)) {
$student.$i['child_fname'][$i] = $row['child_fname'];
$student.$i['child_lname'][$i] = $row['child_lname'];
$i++;
}
$query_array1 = $student0;
$query_array2 = $student1;
You will notice from the code above that I am expecting two rows to be returned. Now, I want to take those two rows and create two arrays from the results. I tried using the solution that was give below. Perhaps my syntax is incorrect or I didn't understand how to properly implement his solution. But any help would be greatly appreciated.
$query = mssql_query('SELECT * FROM mytable');
$result = array();
if (mssql_num_rows($query)) {
while ($row = mssql_fetch_assoc($query)) {
$result[] = $row;
}
}
mssql_free_result($query);
Now you can work with array like you want:
print_r($result[0]); //first row
print_r($result[1]); //second row
...
$i=0;
while( $row = mysql_fetch_array(query){
$field1['Namefield1'][$i] = $row['Namefield1'];
$field2['Namefield2'][$i] = $row['Namefield2'];
$field3['Namefield3'][$i] = $row['Namefield3'];
$i++;
}
or if you preffer an bidimensional array:
$i=0;
while( $row = mysql_fetch_array(query){
$result['Namefield1'][$i] = $row['Namefield1'];
$result['Namefield2'][$i] = $row['Namefield2'];
$result['Namefield3'][$i] = $row['Namefield3'];
$i++
}
I managed to output the column 'resort' in the Json array, but I need 'country' too, as well as 'aantal'. Have no idea how to do that. Can someone please help me?
if ($numrows < 1 && strlen($sq) > 3)
{
$sql = "SELECT resort, country, COUNT(image) AS aantal FROM sx_cam
LEFT JOIN sv_orte ON sv_cam.res_id = sv_orte.res_id
WHERE sound=soundex('$sq') and (status < 1) GROUP BY resort order by aantal desc";
$result2 = mysql_query($sql) or die(mysql_error());
$numrows = mysql_num_rows($result2);
$suggest = 2;
}
$items = array();
while($row = mysql_fetch_assoc($result2)){
$items[$row['resort']] = $row['resort'];
}
foreach ($items as $key=>$value) {
echo strtolower($key)."|$value\n";
}
You're building the array the wrong way. Once you get the array right, it is as simple as making a call to json_encode
I'm not entirely sure how you want your json to look, but something like this should get you started
$items = array();
while($row = mysql_fetch_assoc($result2)){
//first we build an 'object' of the current result
$item['country'] = $row['country'];
$item['resort'] = $row['resort'];
//now push it on the array of results
$items[] = $item;
}
echo json_encode($items);
Once you get the above code working, you can tweak the PHP array to change the structure of the JSON to suit your needs.
I am looking to create an array of data to be pass to another function that is populated from a database query and am not sure how to do this.
$dataArray[0][1];
$qry = mysql_query("SELECT Id, name FROM users");
while($res = mysql_fetch_array($qry)) {
$dataArray[$res['Id']][$res['name']]
}
Thanks in advance.
This would look better
$dataArray = array();
$qry = mysql_query("SELECT Id, name FROM users");
while($res = mysql_fetch_array($qry)) {
$dataArray[$res['Id']] = $res['name'];
}
you can take a look at the PHP manual how to declare and manipulate arrays.
The below code sniper is very handy...
$select=" WRITE YOUR SELECT QUERY ";
$queryResult= mysql_query($select);
//DECLARE YOUR ARRAY WHERE YOU WILL KEEP YOUR RECORD SETS
$data_array=array();
//STORE ALL THE RECORD SETS IN THAT ARRAY
while ($row = mysql_fetch_array($queryResult, MYSQL_ASSOC))
{
array_push($data_array,$row);
}
mysql_free_result($queryResult);
//TEST TO SEE THE RESULT OF THE ARRAY
echo '<pre>';
print_r($data_array);
echo '</pre>';
Thanks