I have a script that pulls values from a MySQL database and puts into into a variable. Each time it iterates, it pulls a new value. I want to add these values together. Here's what I have:
while ($row = mysql_fetch_array( $result )) {
$getvalue = mysql_query("SELECT value FROM pointvalues WHERE code = '$row[code]'")or die(mysql_error());
$rowcode1 = mysql_fetch_array($getvalue);
$finalValue = $rowcode1["value"];
}
I want to keep adding $finalValue to itself.
I recommend saving yourself all that work and simply changing your query to:
SELECT SUM(value) FROM pointvalues WHERE code = x
Reference: SUM
Related
I want to get some elements from a database (phpmyadmin). The database "top" is set up like:
ID || Name
____________
1 || Home
2 || About
3 || Users
4 || Admin
...
I use the following Code to get the information:
<?php
$sql = "SELECT ID
FROM top
WHERE Name='Users' OR Name='Admin'";
$query = mysqli_query($dbconnect, $sql);
$oq = mysqli_fetch_assoc($query);
if(in_array($_GET['ID'], $oq)){
//Execute some Code
}
?>
If I execute the sql-code I get the result I want (ID 3 and 4) but in "$oq" there is only one element (the first one -> 3) left. Therefore the Code I want to execute is only displayed once.
You have to use a while loop:
$oq = array();
while($row = mysqli_fetch_assoc($query)) {
echo $row['ID'];
$oq[] = $row['ID'];
}
As you have done it you're only fetching one row in $oq. You will have to add each value to the array $oq in order to use in_array() for the test.
There are some other techniques you can use here, for instance you could fetch everything (an array of arrays) and loop through the array, depending on your needs.
You will have to use array to use have multiple DB values
$values=array();
while($row = mysql_fetch_array($result)){
$values = array('ID'=>$row['ID']);
}
I am trying to get the values using the below code
$query="select document_id from certificate_documents where certificate_id=$certificate_id";
$res = db_query($query);
$row_count = db_num_rows($res);
$doc_id=array();
for($j=1;j<=$row_count;$j++){
$document_copy = db_fetch_object($res);
$doc_id[$j]=$document_copy->document_id;
print "$doc_id[$j]";
}
But the above code print nothing.
I have to use this value into another query . How can i get this? please help.
You can try this:
$query="select document_id from certificate_documents where certificate_id=$certificate_id";
$res = db_query($query);
$doc_id=array();
while($document_copy = db_fetch_object($res)) {
$doc_id[]=$document_copy->document_id;
}
print_r($doc_id);
I'm using PHP and I do a mysql query and I save the result of this query in an array. Then, I use a loop and into the loop i do another mysql query using the values of the array in the where clause. It's right but if I try to get the result of the query outside the loop I can't.
Here an example code
$result=$mysqli->query("SELECT code FROM referee");
$i=0;
$arcode=array();
while($row=$result->fetch_array()){
$arcode[$i]=$row["code"];
$i++;
}
for($j=0;$j<sizeof($arcode);$j++){
$result2=$mysqli->query("SELECT code, time FROM match where referee_code IN ($arcode[$j])");
}
/*Here I can't get the result values*/
$matcode=array();
$hour=array();
$k=0;
while($row2=$result2->fetch_array()){
$matcode[$k]=$row2["code"];
$hour[$k]=$row2["time"];
}
If I put all in the same loop I get the result repeated (This code is one example of all my code but the idea is the same in the rest).
You are overwriting the result set values into $result2.
The correct method would be something like:
$matcode = array();
$hour = array();
$result2 = $mysqli->query("SELECT code,
time
FROM `match`
WHERE referee_code IN (SELECT code
FROM referee)");
while ($row2 = $result2->fetch_array())
{
$matcode[] = $row2["code"];
$hour[] = $row2["time"];
}
You may change the index values according to the way you want the array to look like.
Also match is a reserved word. So you would have to enclose it in backticks.
If you want to print complete data then try this :
$matcode = array();
$hour = array();
$result2 = $mysqli->query("SELECT code,
time
FROM `match`
WHERE referee_code IN (SELECT code
FROM referee)");
$completeData=array();
while ($row2 = $result2->fetch_array())
{
$completeData[] = $row2;
}
print_r($completeData);
Don forget to accept answer if it helps :)
I'm using this script to get data from a database
$sql = "SELECT * FROM items WHERE catid = 1";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)){
echo $row['extra_fields'];
}
The output is:
[{"id":"1","value":"johndoe"},{"id":"2","value":"marydoe"}]
I want to extract/print only the value corresponding to "id":"1" (that in this case is 'johndoe'). I'm not able to extract it from the above data type.
To read JSON in PHP use
while($row = mysql_fetch_array($result)){
$array = json_decode($row['extra_fields'];
// Do something with $array['id']
}
Did you realise you can directly go for that data in MySQL?
SELECT value FROM items WHERE id = 2;
edit:
Basically your query is
SELECT comma-separated column names or star for all, use only what you really need to save bandwidth, e.g. SELECT id, value
FROM table-name, e.g. FROM mytable
WHERE columnname = desired value, e.g. WHERE id = 2
You want to query only the required columns in the required rows. Imagine one day you would have to parse 1 million users every time you want to get an id... :)
The output is JSON. Use PHP's json_decode function.
while($row = mysql_fetch_array($result)){
$array = json_decode($row['extra_fields']);
foreach($array AS $item) {
echo $item['id'];
}
}
Currently this is the code that fits my needs:
while($row = mysql_fetch_array($result)){
$array = json_decode($row['extra_fields']);
$value = $array[0]->value;
}
You should do it in the mysql query part. For example, set the ID = 1 in the query.
How can i get every row of a mysql table and put it in a php array? Do i need a multidimensional array for this? The purpose of all this is to display some points on a google map later on.
You need to get all the data that you want from the table. Something like this would work:
$SQLCommand = "SELECT someFieldName FROM yourTableName";
This line goes into your table and gets the data in 'someFieldName' from your table. You can add more field names where 'someFieldName' if you want to get more than one column.
$result = mysql_query($SQLCommand); // This line executes the MySQL query that you typed above
$yourArray = array(); // make a new array to hold all your data
$index = 0;
while($row = mysql_fetch_assoc($result)){ // loop to store the data in an associative array.
$yourArray[$index] = $row;
$index++;
}
The above loop goes through each row and stores it as an element in the new array you had made. Then you can do whatever you want with that info, like print it out to the screen:
echo $row[theRowYouWant][someFieldName];
So if $theRowYouWant is equal to 4, it would be the data(in this case, 'someFieldName') from the 5th row(remember, rows start at 0!).
$sql = "SELECT field1, field2, field3, .... FROM sometable";
$result = mysql_query($sql) or die(mysql_error());
$array = array();
while($row = mysql_fetch_assoc($result)) {
$array[] = $row;
}
echo $array[1]['field2']; // display field2 value from 2nd row of result set.
The other answers do work - however OP asked for all rows and if ALL fields are wanted as well it would much nicer to leave it generic instead of having to update the php when the database changes
$query="SELECT * FROM table_name";
Also to this point returning the data can be left generic too - I really like the JSON format as it will dynamically update, and can be easily extracted from any source.
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo json_encode($row);
}
You can do it without a loop. Just use the fetch_all command
$sql = 'SELECT someFieldName FROM yourTableName';
$result = $db->query($sql);
$allRows = $result->fetch_all();
HERE IS YOUR CODE, USE IT. IT IS TESTED.
$select=" YOUR SQL QUERY GOOES HERE";
$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