I do following:
$query .=" SELECT * , COUNT(PRESENT) FROM seventh_a";
$query .=" WHERE class ='0' AND NAME ='Alexander Kirkby Scherer'";
$query .=" AND PRESENT = 'TS' GROUP BY lesson";
$query = " SELECT * , COUNT(lesson) FROM seventh_a";
$query .=" WHERE class ='0' AND NAME ='Alexander Kirkby Scherer'";
$query .=" GROUP BY lesson";
After that I can echo out $row['COUNT(PRESENT)'] but not $row['COUNT(lesson)'].
Can anybody tell me what to do to get both values, so that i am able to work with them?
According to the man page, if you're using mysqli_multi_query, you can access results from the different queries like so:
To retrieve the resultset from the first query you can use mysqli_use_result() or
mysqli_store_result(). All subsequent query results can be processed using
mysqli_more_results() and mysqli_next_result().
They give some sample code:
$query = "SELECT CURRENT_USER();";
$query .= "SELECT Name FROM City ORDER BY ID LIMIT 20, 5";
/* execute multi query */
if ($mysqli->multi_query($query)) {
do {
/* store first result set */
if ($result = $mysqli->store_result()) {
while ($row = $result->fetch_row()) {
printf("%s\n", $row[0]);
}
$result->free();
}
/* print divider */
if ($mysqli->more_results()) {
printf("-----------------\n");
}
} while ($mysqli->next_result());
}
You can't use 2 query . Use combine query like this
$query .=" SELECT * , COUNT(PRESENT),COUNT(lesson) FROM seventh_a";
$query .=" WHERE class ='0' AND NAME ='Alexander Kirkby Scherer'";
$query .=" AND PRESENT = 'TS' GROUP BY lesson";
you will get your result $row['COUNT(PRESENT)'] and $row['COUNT(lesson)']
OR use mysqli's multi_query function for this add ; between queries like this
$query .=" SELECT * , COUNT(PRESENT) FROM seventh_a";
$query .=" WHERE class ='0' AND NAME ='Alexander Kirkby Scherer'";
$query .=" AND PRESENT = 'TS' GROUP BY lesson;"; // semicolon here
$query .= " SELECT * , COUNT(lesson) FROM seventh_a";
$query .=" WHERE class ='0' AND NAME ='Alexander Kirkby Scherer'";
$query .=" GROUP BY lesson";
Related
I want to order data by Id, how can i do this ?
if($_GET["grupid"]>0){
$DUZEN = array();
$sql = "SELECT * FROM siparis_ana WHERE grupid =".$_GET["grupid"];
$rsDuzen = mysql_query($sql, $conn) or die(mysql_error());
while ($r = mysql_fetch_assoc($rsDuzen)) {
$DUZEN[] = $r;
}
}
i can read all data with this code which have same group id. But data aline random.
You have to use mysql order clause in your query like order by id asc. Which you can use at the end of your query.
$sql = "SELECT * FROM siparis_ana WHERE grupid =".$_GET["grupid"]." order by id asc";
Your sql query should be like given below...
$sql = "SELECT * FROM siparis_ana where grupid = " . $_GET['grupid'] . " ORDER BY id asc ";
please help me, i want to make search query but list of table is not shown in php but it work in mysql..here is my code
//$sql = "SELECT * FROM table_name WHERE 1=1" work perfectly
$sql = "SELECT a.* FROM (SELECT table_name FROM information_schema.tables WHERE table_schema like 'rft%') a where 1=1";
if( !empty($requestData['search']['value']) )
{ // if there is a search parameter, $requestData['search']['value'] contains search parameter
//search based on input
$sql.=" AND ( Code LIKE '".$requestData['search']['value']."%' ";
$sql.=" OR Year LIKE '".$requestData['search']['value']."%' )";
}
//start search after input
$query=mysqli_query($conn, $sql) or die("employee-grid-data.php: get employees");
$totalFiltered = mysqli_num_rows($query);
continue here
// when there is a search parameter then we have to modify total number filtered rows as per search result.
$sql.=" ORDER BY ". $columns[$requestData['order'][0]['column']]." ".$requestData['order'][0]['dir']." LIMIT ".$requestData['start']." ,".$requestData['length']." ";
/* $requestData['order'][0]['column'] contains colmun index, $requestData['order'][0]['dir'] contains order such as asc/desc */
$query=mysqli_query($conn, $sql) or die("employee-grid-data.php: get employees");
$data = array();
while( $row=mysqli_fetch_array($query) )
{ // preparing an array
$nestedData=array();
$nestedData[] = $row["Type"];
$nestedData[] = $row["Code"];
$nestedData[] = $row["Year"];
$data[] = $nestedData;
}
You only select table_name from information_schema.tables and later you do your WHERE code=.... There is no Code in the result of your select, so this can't work.
If I get correctly what you are trying to do you have to do it in two steps like this:
// get all table names:
$tquery = mysqli_query($conn, "SELECT table_name FROM information_schema.tables WHERE table_schema like 'rft%'");
while ($table = mysqli_fetch_array($tquery)) {
// do your query per table
$sql = "SELECT * FROM " . $table['table_name'] . " WHERE ( Code LIKE '".$requestData['search']['value']."%' ";
$sql .=" OR Year LIKE '".$requestData['search']['value']."%' )";
$query = mysqli_query($conn, $sql);
while ( $row = mysqli_fetch_array($query) ) {
// do what you want with $row
}
}
I build a query which i get an assoc array from. But how can I loop through them. this is what I have.
My SQL Query:
public static function album_display() {
global $database;
$sql = "SELECT a.*, ";
$sql .= "(SELECT photographs.filename ";
$sql .= "FROM photographs ";
$sql .= "WHERE photographs.albumid = a.name ";
$sql .= "ORDER BY photographs.creation_date ASC ";
$sql .= "LIMIT 1) ";
$sql .= "AS filename FROM album a ";
$result_set = $database->query($sql);
while($rows = mysqli_fetch_assoc($result_set)){
foreach($rows as $row) {
print_r($row);
}
}
}
If I use print_r() on $row the output is:
15Jaap1Jaap1.JPG16Jaap2Jaap4.JPG17Jaap3Jaap7.JPG18Jaap4Jaap12.JPG19Jaap5
I want to loop through Jaap1, Jaap2, Jaap3, Jaap 4 and put these in $albumid from the function album_path($albumid, $filename). I want to do the same wiht the JPG files. This function is in Class() Album. So is the function album_display()
The function album_path($albumid, $filename) if called needs to output the complete album/images path en echo these to the screen. And display the images
public function album_path($albumid, $filename) {
return $this->upload_dir.DS.$albumid.DS.$filename;
}
How can I use foreach for this. Or is there an other better way to do it?
Kind Regards,
Coos Wolff
Change the select a.*, into a select a.albumid. That should only return you the album id, and the filename, coming from the second SELECT statement.
Perhaps something like this? You can break the sql up onto multiple lines within the quotes which makes it easier to read and also use an alias for each of the tables, again making it slightly easier to read and quicker to write.
<?php
public static function album_display() {
global $database;
$sql = "SELECT a.*,
( SELECT p.`filename`
FROM `photographs` p
WHERE p`.`albumid` = a.name
ORDER BY p.`creation_date` ASC
LIMIT 1
) AS 'filename' FROM `album` a ";
$result_set = $database->query( $sql );
/* using object notation rather than array for ease */
while( $rows = mysqli_fetch_object( $result_set ) ){
$this->album_path( $rows->albumid, $rows->filename );
}
}
?>
$my_array = [];
public static function album_display() {
global $database;
$sql = "SELECT a.*, ";
$sql .= "(SELECT photographs.filename ";
$sql .= "FROM photographs ";
$sql .= "WHERE photographs.albumid = a.name ";
$sql .= "ORDER BY photographs.creation_date ASC ";
$sql .= "LIMIT 1) ";
$sql .= "AS filename FROM album a ";
$result_set = $database->query($sql);
while($rows = mysqli_fetch_assoc($result_set)){
foreach($rows as $row) {
$my_array['albumid'][] = $row->albumid;
$my_array['filename'][] = $row->filename;
}
}
}
foreach ($my_array['albumid'] as $albumid) {
echo $albumid;
}
foreach ($my_array['filepath'] as $filepath) {
echo $filepath;
}
album_path($my_array['albumid'], $my_array['filepath']);
mysqli_fetch_assoc returns one row at a time from the result set built by your query.
Each $row is an associative array and if you look more closely at the output of the var_dump() you will see that the array is a set of key - value pairs.
while($rows = mysqli_fetch_assoc($result_set)){
//echo $row['id'];
//echo $row['filename'];
//echo $row['albumid'];
// just to be sure add this line
// so we know exactly whats in $row
print_r($row);
}
Sorry because you used SELECT * for the other fields I dont know what to call the array elements. I hope you get the basic idea.
How should i write my PDO Prepare and bindValue/param statement for this type of query where i check whether the value is not null then only add it to the query string.....
$query = "SELECT * FROM cabs WHERE DATE='$date' ";
if ($mode!=='' || $mode!=="")
$query .="AND MODE='$mode' ";
if ($tfno!=='')
$query .="AND TFNO='$tfno' ";
$query .="ORDER BY TIME";
Quick answer, without testing:
<?php
$params = array(':date' => $date);
$query = "SELECT * FROM cabs WHERE DATE=':date' ";
if ($mode!=='' || $mode!=="") {
$query .="AND MODE=':mode' ";
$params[':mode'] = $mode;
}
if ($tfno!=='') {
$query .="AND TFNO=':tfno' ";
$params[':tfno'] = $tfno;
}
$query .="ORDER BY TIME";
$req = $dbh->prepare($query);
$req->execute($params);
Just push in the param array each time your query gets more filters, and using a name should be easier, I'm not sure that array_push would preserve the order, so ..
Hello can u help me with a script that i have trouble with ;\ i want to create a search script from different columns and price range: so the search.php is that:
<?php
/* connect to the database*/
$db = new mysqli("localhost", "root", "", "movie");
/* check connection */
if ($db->connect_errno) {
echo "Connection failed: " . $db->connect_error;
exit();
}
$query="SELECT * FROM `filmi`
WHERE (`nomer` rlike '%$search%' OR
`rezume` rlike '%$search%' OR
`kategoriq` rlike '%$search%') AND
`seriq` BETWEEN '%$pricemin%' AND '$pricemax'
ORDER BY seriq ASC";
/* create a prepared statement */
if ($stmt = $db->prepare($query)) {
/* bind parameters for markers */
$stmt->bind_param("sssdd", $search, $search, $search, $pricemin, $pricemax);
/* execute query */
$stmt->execute();
/* get result */
$result = $stmt->get_result();
if ($result) {
/* now you can fetch the results into an assoc array */
while ($row = $result->fetch_assoc()) {
echo $row['nomer']. ", " .$row['rezume']. ", " .$row['kategoriq']. ", " .$row['seriq']. "<br>";
}
}
/* close statement */
$stmt->close();
}
/* close db connection */
$db->close();
?>
What shoud be the "<form method=?>" form fill the $search and $pricemin / $pricemax fields ?
Check whether $search exists or not. Means, does there is some value in $search or not.
if(isset($search) && $search!=''){
case 1
}
else{
case 2
}
Case 1:
If there is value in $search then include it in where clause.
below query is an example to explain my idea.
$query="SELECT * FROM `filmi`
WHERE (`nomer` rlike '%$search%' OR
`rezume` rlike '%$search%' OR
`kategoriq` rlike '%$search%') AND
`seriq` BETWEEN '%$pricemin%' AND '$pricemax'
ORDER BY seriq ASC";
Case 2:
If there is no value in $search then does not include it in where clause.
check below query
$query="SELECT * FROM `filmi`
WHERE `seriq` BETWEEN '%$pricemin%' AND '$pricemax'
ORDER BY seriq ASC";
You can use either GET or POST method in your <form> tagg
and
in your PHP script you can use $form_var_name = $_REQUEST['form_var_name'] to access the data dispatched by form. To be more specific as follows
$search = $_REQUEST['search'];
$pricemin = $_REQUEST['pricemin'];
$pricemax = $_REQUEST['pricemax'];