Ok thanks to all of u guys for your answers.
I've made some changing but i still can't display my array ...
Look at what i've done :
$prog = array();
$i=0;
while($row = $get_programmation->fetch_assoc()){
$prog[$i] = $row;
$i++;
}
echo json_encode($prog);
I would have this a result like this : {"id":"0","artist":"xxxx", .... etc} and then use it in my iOS app to display what i want.
mysql_query("SELECT * FROM `programmation`");
Backticks instead of single quotes
Also in here $articles[] = $row;
in $row variable you have to specify MySQL table column names which you have to insert into array, for instance $row['id'], etc...
NOTE: See mysql_ deprecated post too
you can not use quotes in table name or field name but you can use backtick()
$get_programmation = mysql_query("SELECT * FROM 'programmation'");
should be:
$get_programmation = mysql_query("SELECT * FROM `programmation`");
or
$get_programmation = mysql_query("SELECT * FROM programmation");
Replace this $get_programmation = mysql_query("SELECT * FROM 'programmation'");
with this $get_programmation = mysql_query("SELECT * FROM programmation");
Then importantly change code in while loop as below
while($row = mysql_fetch_assoc($get_programmation)){
array_push($articles, $row['yourcol1'],$row['yourcol2']);
}
echo json_encode($articles);
$row is not actual array. You can get the value if it using your actual column name only. And here I have pushed it to the array in while loop to form as array.(Here I have mentioned only two column, You can take all your columns in such way.)
Related
I need to retrieve the count of a field name called remark corresponding to a particular user.
select count(remark) from attendance where username=_POST['username']
How will I implement this in php? I need to store the count to a variable $totalcount.
This is what i tried out:
$result = mysql_query("select count(1) FROM attendance where stud_id='$_POST['user']'");
$row = mysql_fetch_array($result);
$total = $row[0];
Also quotes are not used properly in the query string. Try this code.
$result = mysql_query("select count(*) FROM attendance where stud_id='".$_POST['user']."'");
$row = mysql_fetch_array($result);
$total = $row[0];
I have the following code that fetches a single row:
$query = "SELECT *
FROM translations
WHERE iddoc = '$id'
AND submitted = 1;";
$result = mysqli_query($query);
$row = mysqli_fetch_array($result);
I know this is useful in a while loop, where you can just loop through the results.
But I need to be able to grab specific rows that meet this condition. Something following this pseudo code:
$query = "SELECT *
FROM translations
WHERE iddoc = '$id'
AND submitted = 1;";
$result = mysqli_query($query);
$arrayofrows = mysqli_fetch_arrayofrows($result);
$arrayofrows[0] //(to get the first row)
$arrayofrows[1] //(2nd row)
and so on...
How can I do this?
You can try something like this
$arrayofrows = array();
while($row = mysqli_fetch_array($result))
{
$arrayofrows = $row;
}
You can now have
$arrayofrows[0] //(to get the first row)
$arrayofrows[1] //(2nd row)
I think the function you are looking for is mysqli_fetch_all as shown below. This avoids the need to create an extra looping structure or a function to do something already provided.
$query = "SELECT *
FROM translations
WHERE iddoc = '$id'
AND submitted = 1;";
$result = mysqli_query($query);
$arrayofrows = mysqli_fetch_all($result);
$arrayofrows[0] //(to get the first row)
$arrayofrows[1] //(2nd row)
It depends on whether you require the entire result set back or not but I think the LIMIT could be used like:
$query = "SELECT *
FROM translations
WHERE iddoc = '$id'
AND submitted = 1 LIMIT 200,200;";
Otherwise as others say you will need to convert to an array (which is what fetch_all does) and then get the element from that array.
I have a query for my mysql database but I just want to know if there is more of a simpler way of achieving something, here is my code:
$sql="SELECT value
FROM drivers
WHERE drivers_id = '$driver1' OR drivers_id = '$driver2' OR drivers_id = '$driver3'";
$result = mysql_query($sql);
$i = 1;
while ($row = mysql_fetch_assoc($result)) {
${'value'.$i} = $row['value'];
$i++;
}
This is how I want my code as it is shorter, but the problem I am having is that I need to know that where the $driver1 variable is found in the database the data retrieved for that is placed inside the value1 variable, and the retrieved information for $driver2 is placed inside $value2. However it grabs the data from the database in the order that it comes across the matches. I don't want to have to write 3 different queries for this because I am sure it can be done in one.
$sql="SELECT drivers_id, value FROM drivers WHERE drivers_id IN ('$driver1','$driver2','$driver3')";
$result=mysql_query($sql);
while($row = mysql_fetch_assoc($result)) {
${'value_for_'.$row['drivers_id']}=$row['value'];
}
Boom.
Though personally, instead of on-the-fly variables, I'd use an array w/ the id's as keys:
...
$resultArray = array();
while($row = mysql_fetch_assoc($result)) {
$resultArray[$row['drivers_id']]=$row['value'];
}
If you want to change the order of the results, you can add an order by clause to the query:
$sql = "SELECT drivers_id, value FROM drivers WHERE drivers_id IN ('$driver1','$driver2','$driver3') ORDER BY drivers_id";
...
This question already has answers here:
Single result from database using mysqli
(6 answers)
Closed 6 months ago.
I want to select only unique values with php/mysql.
I can do it with many line, but I forget how to do it without while... :)
Thanks a lot.
Here is the code that I want to do without while.
$request_1m = "SELECT date1, date2 from mytable";
$result_1m = mysql_query($request_1m,$db);
while($row = mysql_fetch_array($result_1m))
{
/* Get the data from the query result */
$date1_1m = $row["date1"];
$date2_1m = $row["date2"];
}
mysql_fetch_assoc + SELECT with DISTINCT
I'm not sure I understand your question, but here's what I think you want to do :
$request_1m = "SELECT date1, date2 from mytable";
$result_1m = mysql_query($request_1m,$db);
list($date1_1m, $date2_1m) = mysql_fetch_row($result_1m);
Note that this will only get the first row from the result set (just as if you LIMIT 1)
like this?
$dbresult = mysql_query("SELECT DISTINCT field FROM table");
$result = array();
while ($row = mysql_fetch_assoc($dbresult))
{
$result[] = $row;
}
This gets you all unique values from "field" in table "table".
If you really wish to avoid the while loop, you can use the PHP PDO objects, and in particular call the PDO fetchAll() method to retrieve the complete results array in one go. PDO fetchAll() documentation
$db = new PDO('dblib:host=your_hostname;otherparams...');
$db->query("SELECT DISTINCT col FROM table");
$results = $db->fetchAll();
// All your result rows are now in $results
Heres how I do it and Json encode after. This will ensure it will encode only UNIQUE json Values (Without duplicates) as an example
$tbl_nm = "POS_P";
$prod_cat = "prod_cat";
//Select from the POS_P Table the Unique Product Categories using the DISTINCT syntax
$sql = "SELECT DISTINCT $prod_cat FROM $tbl_nm";
//Store the SQL query into the products variable below.
$products = mysql_query($sql);
if ($products){
// Create an array
$rows = array();
// Fetch and populate array
while($row = mysql_fetch_assoc($products)) {
$rows[]=$row;
}
// Convert array to json format
$json = json_encode(array('Categories'=>$rows));
echo $json;
}
//Close db connection when done
mysql_close($con);
?>
That is very easy, take out the while, like below
$row = mysqli_fetch_assoc($result);
$date1_1m = $row["date1"];
As you will see I am fetching the column, and trying to update column with new data.
$result2 line is my problem, I don't think I can add $row[0] in there. How do I do it?
$result = mysql_query("SELECT link FROM items LIMIT 3");
while($row = mysql_fetch_array($result))
{
$url=($row[0]);
$rez2 = get_final_url($url);
$result2 = mysql_query("UPDATE items SET link = $rez2 WHERE id = $row[0] LIMIT 1")
or die(mysql_error());
You should use quotes:
mysql_query("UPDATE items SET link = '{$res2}' WHERE id = $row[0]");
And it would be ideal to use mysql_escape_string() function.
So:
$rez2 = mysql_escape_string(get_final_url($url));
Also you're trying to use $row[0] as link and as id. Most likely you want $row[0] element to be an ID, and something like $row[n] where n > 0 to be a link. But if you still want to use link you should query in the following manner:
$result2 = mysql_query("UPDATE items SET link = '$res2' WHERE link = {$row[0]}");
And do not forget to escape $row
It is a good idea to use mysql_fetch_assoc() function - in this case you'll get an associative array, so you'll be able access elements by sql column names. And as result you could do something like:
$result = mysql_query("SELECT id, link FROM items LIMIT 3");
while($row = mysql_fetch_assoc($result))
{
$url=($row['link']);
$rez2 = mysql_escape_string(get_final_url($url));
$result2 = mysql_query("UPDATE items SET link = '{$res2}' WHERE id = {$row['id']}")
or die(mysql_error());
}
Also if ID is a primary key you do not need LIMIT 1 in the update query.
$row[0] is in fact valid in double-quoted strings. I think your problem is a misspelling: first you assign $rez2 a value and then in the query you use $res2.
What does get_final_url($url); do? If it doesn't surround link with quotes, and handle proper string escaping (i.e. mysql_real_escape_string), your query won't work.