How can I concatenate two columns in where clause
this is my normal query:
$result = mysql_query("SELECT stud_id FROM tb_student WHERE instructor_id = '$instructor_id' AND description = '$description' AND stud_fname = '$stud_fname' AND stud_lname = '$stud_lname'") or die(mysql_error());
I want the stud_fname and stud_lname to be concatenated.
here is my full code:
<?php
error_reporting(0);
$link = mysql_pconnect("localhost", "root", "") or die("Could not connect");
mysql_select_db("dbmobile_class_record") or die("Could not select database");
// array for JSON response
$response = array();
$instructor_id=$_GET['instructor_id'];
$description=$_GET['description'];
$stud_fname=$_GET['stud_fname'];
$stud_lname=$_GET['stud_lname'];
// get all items from myorder table
$result = mysql_query("SELECT stud_id FROM tb_student WHERE instructor_id = '$instructor_id' AND description = '$description' AND stud_fname = '$stud_fname' AND stud_lname = '$stud_lname'") or die(mysql_error());
if (mysql_num_rows($result) > 0) {
$response["student"] = array();
while ($row = mysql_fetch_array($result)) {
// temp user array
$student = array();
$student["stud_id"] = $row["stud_id"];
// push ordered items into response array
array_push($response["student"], $student);
}
// success
$response["success"] = 1;
}
else {
// order is empty
$response["success"] = 0;
$response["message"] = "No Records Found";
}
// echoing JSON response
echo json_encode($response);
?>
$instructor_id = $_GET['instructor_id'];
$description = $_GET['description'];
$fullname = $_GET['fullname'];
// get all items from myorder table
$result = mysql_query("SELECT stud_id FROM tb_student WHERE instructor_id = '$instructor_id' AND description = '$description' AND CONCAT(stud_fname, ' ', stud_lname) = '$fullname'") or die(mysql_error());
Use this one
$result = mysql_query("SELECT stud_id, CONCAT_WS('', stud_fname, stud_lname) AS stud_fullname FROM tb_student WHERE instructor_id = '$instructor_id' AND description = '$description' AND stud_fname = '$stud_fname' AND stud_lname = '$stud_lname'") or die(mysql_error());
stud_fullname should contain what you want.
Related
SO i want to insert my item id and together with my item name into a distribution_item,it only insert item id but not the name because of the array
i use item[$i] whenever there is a item in the table row, so it get the data and insert.
#$recipient = $_POST['recipient'];
#$address = $_POST['address'];
#$contact = $_POST['contact'];
#$date = $_POST['in_date'];
#$itemID = $_POST['id'];
#$remark = $_POST['remark'];
#$spec_remark = $_POST['spec_remark'];
$itemBalance = $_POST["count"];
$count = count($itemID);
// authentication to the database
$servername = "localhost";
$username = "root";
$password = "";
$dbName = "hopeplace";
//Create connection
$Conndb = mysqli_connect($servername, $username, $password, $dbName);
// Check connection
if (!$Conndb) {
die("Connection failed: " . $Conndb->connect_error);
}
else {
// select database
mysqli_select_db($Conndb, $dbName);
$full_name = "SELECT * FROM recipient WHERE `FULL_NAME` = '$recipient'";
$result = mysqli_query($Conndb, $full_name);
$rec = mysqli_fetch_array($result);
$recipient_id = $rec['HP_ID'];
$item_id = "SELECT ITEM_NAME FROM inventory WHERE ITEM_ID = '$itemID'";
$result2 = mysqli_query($Conndb, $item_id);
$rec2 = mysqli_fetch_array($result2);
$item_name = $rec2["ITEM_NAME"];
$string = implode(',',$item_id);
$sql = "SHOW TABLE STATUS WHERE `Name` = 'distribution';";
$result = mysqli_query($Conndb, $sql);
$data = mysqli_fetch_assoc($result);
$DISTRIBUTION_ID = $data['Auto_increment'];
// Add into distribution table
$sql = "INSERT INTO distribution(DISTRIBUTION_ID,HP_ID,FULL_NAME, ADDRESS, CONTACT, DISTRIBUTION_DATE, SPEC_REMARK) VALUES ('$DISTRIBUTION_ID','$recipient_id','$recipient', '$address', '$contact', '$date', '$spec_remark')";
if (mysqli_query($Conndb, $sql)) {
//Add item into distribution_item table
$item_count = 0;
for ($i=0; $i<$count; $i++){
$sql = "INSERT INTO distribution_item (DISTRIBUTION_ID, ITEM_ID,ITEM_NAME,OUT_QUANTITY,REMARK) VALUES ('$DISTRIBUTION_ID', '$itemID[$i]','$string[$i]','$itemBalance[$i]', '$remark[$i]')";
if (mysqli_query($Conndb, $sql)){
$out = "UPDATE inventory set QUANTITY = QUANTITY - '$itemBalance[$i]' where ITEM_ID= '$itemID[$i]'";
mysqli_query($Conndb, $out);
//echo "<p>Item $itemID[$i] has been added to $DISTRIBUTION_ID</p>";
$item_count++;
} else {
echo "Error: $sql <br />" . mysqli_error($Conndb);
}
}
if ($item_count == $count){
echo "<div>
<script>
window.alert('Record added successfully!');
</script>
</div>";
}
} else {
echo "Error: $sql <br />" . mysqli_error($Conndb);
}
}
mysqli_close($Conndb);
?>
it pop out an error like this
Notice: Array to string conversion in C:\xampp\htdocs\hopeplace\distribution\add_distribution.php on line 55
Warning: implode(): Invalid arguments passed in C:\xampp\htdocs\hopeplace\distribution\add_distribution.php on line 56
it seems like i have to convert my array to string so i can pass the value into table. my database table is something like this
DISTRIBUTION_ID | ITEM_NAME | ITEM_NAME|
1 1 APPLE
1 2 ORANGE
The problem here is that $string[$i] cannot be found because no $string array exists, since $string = implode(', ',$item_id) can only work if $item_id is an array.
In the question, $item_id = "SELECT ITEM_NAME FROM inventory WHERE ITEM_ID = '$itemID'"; gives $item_id as a string. That is why the warning error indicates that the implode function cannot work. The notice error shows that you cannot get an array element from the $string variable, and this is because that variable is not an array.
To get $string as an array, you would have to correct the following code:
$item_id = "SELECT ITEM_NAME FROM inventory WHERE ITEM_ID = '$itemID'";
$result2 = mysqli_query($Conndb, $item_id);
$rec2 = mysqli_fetch_array($result2);
$item_name = $rec2["ITEM_NAME"];
$string = implode(',',$item_id);
For example, the above code could be changed to the following:
$string = [];
$item_id = "SELECT ITEM_NAME FROM inventory WHERE ITEM_ID = '$itemID'";
$result2 = mysqli_query($Conndb, $item_id);
while($rec2 = mysqli_fetch_array($result2){
$string[] = $rec2["ITEM_NAME"];
}
Then in the sql query for inserting the item name you would first define the counting variable as $count = count($string);
Can someone tell me what is wrong with these IF statements?
if(isset($_POST['submit']))
{
$dropship = $unitid['id'];
$jumpship = $_POST['jumpship'];
$dsdest = $_POST['planet'];
$dslz = $_POST['landingzone'];
$dsmission = $_POST['mission'];
$ds1 = mysql_query("SELECT id, ds1 FROM gc3025_game_jumpships WHERE `id`='$jumpship'");
$ds2 = mysql_query("SELECT id, ds2 FROM gc3025_game_jumpships WHERE `id`='$jumpship'");
$ds3 = mysql_query("SELECT id, ds3 FROM gc3025_game_jumpships WHERE `id`='$jumpship'");
$dist_loc_get2 = mysql_query("SELECT * FROM gc3025_dist_game WHERE `planet`='$dsdest' AND `districtid`='$dslz'");
$distloc2 = mysql_fetch_assoc($dist_loc_get2);
$newdist = $distloc2['g_district'];
$ds_name_get = mysql_query("SELECT * FROM gc3025_game_dropships WHERE `id`='$dropship'");
$ds_name = mysql_fetch_assoc($ds_name_get);
$dsname = $ds_name['unit_name'];
$dest_name_get = mysql_query("SELECT gc3025_planets_game.Game, gc3025_planets_game.owners, gc3025_planets_game.g_planet, gc3025_planets_game.Planet_id, gc3025_planets_id.planet_name FROM gc3025_planets_id JOIN gc3025_planets_game ON gc3025_planets_id.id = gc3025_planets_game.Planet_id WHERE `g_planet`='$dsdest'");
$dest_name = mysql_fetch_assoc($dest_name_get);
$destname = $dest_name['planet_name'];
$dsdz_name_get = mysql_query("Select gc3025_dist_game.districtid, gc3025_dist_game.g_district, gc3025_dist_labels.id, gc3025_dist_labels.dist_name FROM gc3025_dist_game JOIN gc3025_dist_labels ON gc3025_dist_game.districtid = gc3025_dist_labels.id WHERE `g_district`='$newdist'");
$dsdz_name = mysql_fetch_assoc($dsdz_name_get);
$dsdzname = $dsdz_name['dist_name'];
$dsmission_name_get = mysql_query("SELECT * FROM gc3025_movement_dropdowns WHERE `id`='$dsmission'");
$dsmission_name = mysql_fetch_assoc($dsmission_name_get);
$dsmissionname = $dsmission_name['mission_type'];
if ($ds1 == 0){
mysql_query ("UPDATE `gc3025_game_jumpships` SET `ds1` = '$dsname', `ds1dest` = '$destname', `ds1dz` = '$dsdzname', `ds1mission` = '$dsmissionname' WHERE `id`='$jumpship'");
}
if ($ds1 == 1){
mysql_query ("UPDATE `gc3025_game_jumpships` SET `ds2` = '$dsname', `ds2dest` = '$destname', `ds2dz` = '$dsdzname', `ds2mission` = '$dsmissionname' WHERE `id`='$jumpship'");
}
if ($ds2 == 1){
mysql_query ("UPDATE `gc3025_game_jumpships` SET `ds3` = '$dsname', `ds3dest` = '$destname', `ds3dz` = '$dsdzname', `ds3mission` = '$dsmissionname' WHERE `id`='$jumpship'");
}
if ($ds3 == 1){
echo "This Jumpship is Full!";
}
echo "<p>$dsname Loaded on $jumpship going to $destname and to complete $dsmissionname In District $dsdzname!</p>";
Hope this is enough.
Basically the table is for a jumpship that carries 3 dropships. I need the if statement to basically If ds1 has a dropship in it then the dropship will be entered to ds2 column. if there is a dropship in ds1 and ds2 then it will update ds3 column. if all three have dropships in them then the echo "this jumpship is full" will post.
Function mysql_query returns resource, and you must use mysql_fetch_assoc for data extraction. Besides, data inside the query should be properly escaped.
$ds_query = mysql_query(sprintf(
"SELECT ds1, ds2, ds3 FROM gc3025_game_jumpships WHERE `id`='%s'",
mysql_real_escape_string($jumpship)
));
$ds_result = mysql_fetch_assoc($ds_query);
if (!$ds_result['ds1'])
{
mysql_query(sprintf(
"UPDATE `gc3025_game_jumpships` " .
"SET `ds1` = '%s', `ds1dest` = '%s', `ds1dz` = '%s', `ds1mission` = '%s' " .
"WHERE `id`='%s'",
mysql_real_escape_string($dsname),
mysql_real_escape_string($destname),
mysql_real_escape_string($dsdzname),
mysql_real_escape_string($dsmissionname),
mysql_real_escape_string($jumpship)
));
}
elseif ($ds_result['ds1'])
{
// and so on
}
elseif ($ds_result['ds2'])
{
// and so on
}
Please help, I tried everything. I getting data from my database that is utf8_bin and displaying in php, but when i put characters like ć,č,ž,š Json displays numbers instead.I already tried mysql_query("SET NAMES 'utf8'");
mysql_query("SET CHARACTER_SET 'utf8'"); but nothing. my Json code is :
<?php
/*
* Following code will list all the products
*/
// array for JSON response
$response = array();
$sviArray = array();
$responseZagreb=array();
// include db connect class
require_once('DB_connect.php');
// connecting to db
$db = new DB_connect();
mysql_query("SET NAMES 'utf8'");
mysql_query("SET CHARACTER_SET 'utf8'");
// get all products from products table
$result = mysql_query("SELECT *FROM Istra") or die(mysql_error());
$resultZagreb = mysql_query("SELECT *FROM Zagreb") or die(mysql_error());
$place=array();
// check for empty result
if (mysql_num_rows($result) > 0) {
// looping through all results
// products node
$response["Istra"] = array();
while ($row = mysql_fetch_array($result)) {
// temp user array
$product = array();
$product["id"] = $row["ID"];
$product["datum"] = $row["DATUM"];
$product["grad"] = $row["GRAD"];
$product["place"] = $row["PLACE"];
$product["adresa"] = $row["ADRESA"];
$product["Dogadaj"]=$row["DOGADAJ"];
$product["Cijena"]=$row["CIJENA"];
$product["Slika"]=$row["SLIKA"];
$product["Tip"]=$row["TIP"];
$place=$row['PLACE'];
$result0 = mysql_query("SELECT SUM(ocjena) AS value_sum FROM Ocjena where place='$place'");
$cijena=mysql_query("SELECT SUM(cijena) AS values_cijena FROM Ocjena where place='$place'");
$cijenacount=mysql_query("SELECT COUNT(cijena) AS cijena_count FROM Ocjena where place='$place'");
$result1=mysql_query("SELECT COUNT(ocjena) AS value_sum1 FROM Ocjena where place='$place'");
$row0 = mysql_fetch_assoc($result0);
$row1 = mysql_fetch_assoc($result1);
$row2=mysql_fetch_assoc($cijena);
$row3=mysql_fetch_assoc($cijenacount);
$sum0 = $row0['value_sum'];
$sum1 = $row1['value_sum1'];
$sum2=$row2['values_cijena'];
$sum3=$row3['cijena_count'];
if($sum1!=0){
$rez=$sum0/$sum1;
}
else $rez=0;
$product["Ocjena"]=$rez;
if($sum2!=0){
$rezCijena=$sum2/$sum3;
}
else $rezCijena=0;
$product["cijena"]=$rezCijena;
// push single product into final response array
array_push($response["Istra"], $product);
}
// success
$response["success"] = 1;
$responseZagreb["Zagreb"] = array();
while ($row1 = mysql_fetch_array($resultZagreb))
{
// temp user array
$productZagreb = array();
$productZagreb["id"] = $row1["ID"];
$productZagreb["datum"] = $row1["DATUM"];
$productZagreb["grad"] = $row1["GRAD"];
$productZagreb["place"] = $row1["PLACE"];
$productZagreb["adresa"] = $row1["ADRESA"];
$productZagreb["Dogadaj"]=$row1["DOGADAJ"];
$productZagreb["Cijena"]=$row1["CIJENA"];
$productZagreb["Slika"]=$row1["SLIKA"];
$productZagreb["Tip"]=$row["TIP"];
$place=$row['PLACE'];
$result0 = mysql_query("SELECT SUM(ocjena) AS value_sum FROM Ocjena where place='$place'");
$result1=mysql_query("SELECT COUNT(ocjena) AS value_sum1 FROM Ocjena where place='$place'");
$row0 = mysql_fetch_assoc($result0);
$row1 = mysql_fetch_assoc($result1);
$sum0 = $row0['value_sum'];
$sum1 = $row1['value_sum1'];
if($sum1!=0){
$rez=$sum0/$sum1;
}
$productZagreb["Ocjena"]=$rez;
// push single product into final response array
array_push($responseZagreb["Zagreb"], $productZagreb);
}
// looping through all results
// products node
$sviArray=array_merge($responseZagreb,$response);
// echoing JSON response
echo stripcslashes(json_encode ($sviArray));
}
else {
// no products found
$response["success"] = 0;
$response["message"] = "No products found";
// echo no users JSON
}
?>
Try to use JSON_UNESCAPED_UNICODE option.
echo stripcslashes(json_encode ($sviArray, JSON_UNESCAPED_UNICODE));
When I do this I am only getting one result back Fantasy0.1429 when I should be getting 7 different ones any one know what I am doing wrong.
$userid = $_SESSION['sess_id'];
$genreQuery = $con ->query ("select distinct(genre) from movies");
$movieGenre = array();
while($row = $genreQuery->fetch_object()) {
$movieGenre[] = $row;
}
foreach($movieGenre as $MGenre){
$query = $con ->query
(" select '$MGenre->genre' genre, IFNULL(count(*)/(select count(*) from user_movie_ratings where user_id = '$userid'),0) rating
from user_movie_ratings umr,
movies m
where umr.user_id = '$userid'
and umr.movie_id = m.id
and m.genre = '$MGenre->genre'; ");
$movieTitle = array();
while($row = $query->fetch_object()) {
$movieTitle[] = $row;
}
}
Then I echo it out later
<?php foreach($movieTitle as $movie): echo $movie->genre; echo $movie->rating; endforeach; ?>
Try to move away the instanciation of your array before the foreach like:
// Some code ...
$movieTitle = array();
foreach($movieGenre as $MGenre){
$query = $con ->query
(" select '$MGenre->genre' genre, IFNULL(count(*)/(select count(*) from user_movie_ratings where user_id = '$userid'),0) rating
from user_movie_ratings umr,
movies m
where umr.user_id = '$userid'
and umr.movie_id = m.id
and m.genre = '$MGenre->genre'; ");
while($row = $query->fetch_object()) {
$movieTitle[] = $row;
}
}
I am getting null results when I use the following Like query in PHP Script
$MasjidName = $_GET['MasjidName'];
$Percent = "%";
$search = $Percent.$MasjidName.$Percent;
echo $search;
$sql = "SELECT * FROM `MasjidMaster` WHERE `MasjidName` LIKE '".$search."'";
// get a product from products table
$result = mysql_query($sql) or die(mysql_error());
I have tried the following too
$result = mysql_query("SELECT * FROM `MasjidMaster` WHERE `MasjidName` LIKE '%moh%'") or die(mysql_error());
The following is the null result I have been getting
{"masjids":[{"MasjidName":null,"Address":null,"Latitude":null,"Longitude":null}],"success":1,"masjid":[]}
whole code added below the following is the script i have been trying to get work
<?php
$response = array();
require_once dirname(__FILE__ ). '/db_connect.php';;
$db = new DB_CONNECT();
if (isset($_GET["MasjidName"]))
{
$MasjidName = $_GET['MasjidName'];
$MasjidName = mysql_real_escape_string($MasjidName); // you have to escape your variable here.
$sql = "SELECT * FROM `MasjidMaster` WHERE `MasjidName` LIKE '%$MasjidName%'";
$result = mysql_query($sql) or die(mysql_error());
$response["masjids"] = array();
if (!empty($result)) {
// check for empty result
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_array($result)) {
$row = mysql_fetch_array($result);
$masjid = array();
$masjid["MasjidName"] = $row["MasjidName"];
$masjid["Address"] = $row["Address"];
$masjid["Latitude"] = $row["Latitude"];
$masjid["Longitude"] = $row["Longitude"];
// success
$response["success"] = 1;
// user node
$response["masjid"] = array();
array_push($response["masjids"], $masjid);
}
// echoing JSON response
echo json_encode($response);
} else {
// no product found
$response["success"] = 0;
$response["message"] = "No product found";
// echo no users JSON
echo json_encode($response);
}
} else {
// no product found
$response["success"] = 0;
$response["message"] = "No product found";
// echo no users JSON
echo json_encode($response);
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
}
?>
Try this:
$MasjidName = $_GET['MasjidName'];
$MasjidName = mysql_real_escape_string($MasjidName); // you have to escape your variable here.
$sql = "SELECT * FROM `MasjidMaster` WHERE `MasjidName` LIKE '%$MasjidName%'";
$result = mysql_query($sql) or die(mysql_error());
Try using
$sql = "SELECT * FROM MasjidMaster WHERE MasjidName LIKE '".$search."'";
I tried SELECT * FROM Customers WHERE City LIKE 's%'; and it gave me perfectly fine result. But when i tried SELECT * FROM 'Customers' WHERE 'City' LIKE 's%'; it gave me a null result.
Just remove '' and give it a try. Hope it helps.