Save sql result to variable using php [duplicate] - php

This question already has answers here:
Get the single value of a sql result set
(3 answers)
Closed 9 years ago.
I have this query:
$result = "SELECT MAX(N) as maxid FROM (
SELECT SUBSTRING(id_f, 2,len(id_f) -1) as N
From formas WHERE id_f LIKE '%D%'
) t" ;
$res = odbc_exec($connection, $result) or die(odbc_error());
Now, if i put the query in SQL SERVER i get the correct result.
What i need is to save the maxid as a variable.. how to do it?
Thanks

What you need is the function odbc_fetch_array.
$result = "SELECT MAX(N) as maxid FROM (
SELECT SUBSTRING(id_f, 2,len(id_f) -1) as N
From formas WHERE id_f LIKE '%D%'
) t" ;
$res = odbc_exec($connection, $result) or die(odbc_error());
$info = odbc_fetch_array($res);
$content[] = $info;
$maxN = $content[0]
For a multiple rows query, you need to encapsulate the function in a while loop :
while($row = odbc_fetch_array($res))
{
print_r($row);
}

Related

SQL getting SUM of column [duplicate]

This question already has an answer here:
<cfoutput> a MySQL SUM function
(1 answer)
Closed 12 months ago.
SELECT SUM(total_cost)
FROM purchase;
How do I make the value of SUM(total_cost) to a variable?
If SUM(total_cost) = 300, how do I assign 300 to $result for example?
I'm trying to do this in coldfusion, but php will work too.
You can completely handle it in the MySQL query:
SELECT SUM(column_name) FROM table_name;
Using PDO (mysql_query is deprecated)
$stmt = $handler->prepare('SELECT SUM(value) AS value_sum FROM codes');
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$sum = $row['value_sum'];
Or using mysqli:
$result = mysqli_query($conn, 'SELECT SUM(value) AS value_sum FROM codes');
$row = mysqli_fetch_assoc($result);
$sum = $row['value_sum'];
For mysqli
$data = mysqli_query($conn, 'SELECT SUM(value) AS value_sum FROM codes');
$row = mysqli_fetch_assoc($data);
$result = $row['value_sum'];
Now u can get a Sum of value in $result

How create Nested JSON with mysqli group_concat? [duplicate]

This question already has answers here:
Split a comma-delimited string into an array?
(8 answers)
Closed 3 years ago.
Extracting Values from Nested JSON Data !
$response = array();
$result = mysqli_query($conn, "
SELECT id, GROUP_CONCAT(CONCAT_WS(':', Name) SEPARATOR ',') AS Result
FROM mytbl GROUP BY id
");
mysqli_num_rows($result);
if(mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_assoc($result)){
$char = array("id"=>$row["id"], "char"=>[$row["Result"]]);
array_push($response,array('Attr'=>$char));
}
}
echo json_encode($response);
i added photo for what i need
Just Explode it with PHP
$response = array();
$result =mysqli_query($conn, "
SELECT id, GROUP_CONCAT(CONCAT_WS(':', Name) SEPARATOR ',') AS Result
FROM mytbl GROUP BY id ");
mysqli_num_rows($result);
if(mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_assoc($result)){
$char = array("id"=>$row["id"],"char"=>explode(',', $row["Result"]));
array_push($response,array('Attr'=>$char));
}
}

MySQL query with and array [duplicate]

This question already has answers here:
Array in SQL Query? [duplicate]
(4 answers)
Closed 5 years ago.
I'm trying to create a tag function in my webpage and I'm using an array of tags to query the database.
array[tag1, tag2, tag3];
The tags are created in a field
what I'm trying to archive is something like
$query = "SELECT * FROM TABLE WHERE `Tags` = '$tag1' AND '$tag2' AND '$tag3'";
Except with an array, so
$query = "SELECT * FROM TABLE WHERE `Tags` = $array[]";
Thanks
$tag = $_POST['tag'];
$query = "SELECT * FROM ComicStripTags WHERE `Tag` = '$tag'";
$result = mysqli_query($link, $query);
while ($row = mysqli_fetch_array($result)){
$ID[] = $row['ImageID'];
print_r ($ID);
}
BTW I want to use $ID[] to use in another query
Try using implode to convert array to string, and use "IN" operator instead of equal:
$query = "SELECT * FROM TABLE WHERE `Tags` in (". implode(", ",$array) .")";

PHP: Pass variable in sql query [duplicate]

This question already has answers here:
How can I combine two strings together in PHP?
(19 answers)
Closed 7 years ago.
I have following sql query in PHP and output is as JSON:
<?php
$user_id = "1";
$sql = 'SELECT *,DATE_FORMAT(c.`date`, "%d.%m.%Y") AS date
FROM
conversation c,
users u
WHERE
c.user_two = u.uid AND c.user_one = '$user_id'
ORDER By c.precious_time DESC';
$result = mysqli_query($con, $sql);
$rows = array();
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
$rows [] = $row;
}
mysqli_close($con);
echo json_encode($rows);
?>
When I execute the the php file it does not echo anything, but if I set c.user_one = "1" instead of c.user_one = '$user_id' then it works and it gives me the results. Is there a formatting error ?
If you use doublequotes it works:
$sql = "SELECT *,DATE_FORMAT(c.`date`, '%d.%m.%Y') AS date
FROM conversation c, users u
WHERE c.user_two = u.uid AND c.user_one = '$user_id'
ORDER By c.precious_time DESC";
FYI: http://php.net/manual/en/language.types.string.php#language.types.string.parsing

display the mysql count function? [duplicate]

This question already has answers here:
SELECT COUNT(*) AS count - How to use this count
(5 answers)
Closed 1 year ago.
$query = "select count(*)
from relationships
where leader = 'user_id'";
$result = mysql_query($query);
how can i display the count? thanks
$count = mysql_fetch_array($result);
echo $count[0];
$query = "SELECT COUNT(*) AS total FROM table";
$result = mysql_query($query);
$values = mysql_fetch_assoc($result);
$num_rows = $values['total'];
echo $num_rows;
use $row = mysql_fetch_array($result) and access it by index 0: $row[0]
use an alias in your query ("select count(*) as cnt from ...") and $row = mysql_fetch_assoc($result) and access it by name: $row['cnt']
$abc="SELECT count(*) as c FROM output WHERE question1=4";
$result=mysqli_query($conn,$abc);
if($result)
{
while($row=mysqli_fetch_assoc($result))
{
echo $row['c'];
}
}
Its work completely in this it count the number of occurrences of 4 in the column

Categories