Rightnow i am facing a problem of print Long Text. In my database i have data in long text format and that field has around 2000 line of text so how can i print that.
I am giving here full code of my file please go through it and try to solve my problem
<?php
if($objConnect = mysql_connect("localhost","root","")){
$objDB = mysql_select_db("android");
$from_date=$_GET['from_date'];
$to_date=$_GET['to_date'];
$time = strtotime($from_date);
$time1 = strtotime($to_date);
$fdate = date( 'Y-m-d', $time );
$todate = date( 'Y-m-d', $time1 );
$tmpdate="";
$time2 = strtotime($tmpdate);
$tempdate = date( 'Y-m-d', $time2 );
if($fdate==$tempdate){
echo "if block";
$strSQL = "SELECT LessonText FROM lesson_details ";/*Here this field LessonText is Long Text*/
$objQuery = mysql_query($strSQL);
$intNumField = mysql_num_fields($objQuery);
$resultArray = array();
while($obResult = mysql_fetch_array($objQuery)){
$arrCol = array();
for($i=0;$i<$intNumField;$i++){
$arrCol[mysql_field_name($objQuery,$i)] = $obResult[$i];
}
array_push($resultArray,$arrCol);
}
$Jdata=(json_encode(array('lessons:'=>$resultArray)));
if($Jdata!=""){
$error_code=0;
$error_massage="Success";
$Result=array(array('Lesson_details:'=>$resultArray),array('error_code:'=>$error_code,'error_massage:'=>$error_massage));
echo json_encode($Result);
}
else{
echo "Else Block";
$error_code=1;
$error_massage="No Record Found";
$Result=array(array('Lesson_details:'=>$resultArray),array('error_code:'=>$error_code),array('error_massage:'=>$error_massage));
echo json_encode($Result);
}
}
elseif($fdate!="1970-01-01" && $todate!="1970-01-01"){
$strSQL = "SELECT * FROM lesson_details WHERE UpdatedDate>='$fdate' &&UpdatedDate<='$todate'";
$objQuery = mysql_query($strSQL);
$intNumField = mysql_num_fields($objQuery);
$resultArray = array();
while($obResult = mysql_fetch_array($objQuery)){
$arrCol = array();
for($i=0;$i<$intNumField;$i++){
$arrCol[mysql_field_name($objQuery,$i)] = $obResult[$i];
}
array_push($resultArray,$arrCol);
}
$Jdata=(json_encode(array('lessons:'=>$resultArray)));
if($Jdata!=""){
$error_code=0;
$error_massage="Success";
$Result=array(array('Lesson_details:'=>$resultArray),array('error_code:'=>$error_code,'error_massage:'=>$error_massage));
echo json_encode($Result);
}
else{
echo "Else Block";
$error_code=1;
$error_massage="No Record Found";
$Result=array(array('Lesson_details:'=>$resultArray),array('error_code:'=>$error_code),array('error_massage:'=>$error_massage));
echo json_encode($Result);
}
}
else{
$strSQL = "SELECT * FROM lesson_details WHERE UpdatedDate>='$fdate'";
$objQuery = mysql_query($strSQL);
$intNumField = mysql_num_fields($objQuery);
$resultArray = array();
while($obResult = mysql_fetch_array($objQuery)){
$arrCol = array();
for($i=0;$i<$intNumField;$i++){
$arrCol[mysql_field_name($objQuery,$i)] = $obResult[$i];
}
array_push($resultArray,$arrCol);
}
$Jdata=json_encode($resultArray);
$Jdata=(json_encode(array('lessons:'=>$resultArray)));
if($Jdata!=""){
$error_code=0;
$error_massage="Success";
$Result=array(array('Lesson_details:'=>$resultArray),array('error_code:'=>$error_code,'error_massage:'=>$error_massage));
echo json_encode($Result);
}
else{
echo "Else Block";
$error_code=1;
$error_massage="No Record Found";
$Result=array(array('Lesson_details:'=>$resultArray),array('error_code:'=>$error_code),array('error_massage:'=>$error_massage));
echo json_encode($Result);
}
}
}
else{
$error=array("ErrCode"=>2,"ErrMsg"=>"Database connection error");
echo (json_encode($error));
}
?>
I have find out the solution so i am sharing here
in my case it is the problem of utf8 encoding. so i have add following line below my select query
mysql_query('SET CHARACTER SET utf8')
Something like this, I believe:
print $longtext;
Related
json return this, images are equals
How to return image data from MySQL? This code returns always first image.When I fetching only one value, everything is ok.
Please help.
require_once 'connection.php';
mysqli_query ($conn,"SET NAMES UTF8");
$sql = "SELECT * FROM posts WHERE authorId = '$id' GROUP BY date DESC ";
$response = mysqli_query($conn, $sql);
$result['posts'] = array();
if ( mysqli_num_rows($response) > 0 ) {
while ($row = mysqli_fetch_array($response)){
$index['id'] = $row['id'];
$index['authorId'] = $row['authorId'];
$index['date'] = $row['date'];
$index['time'] = $row['time'];
$index['description'] = strval($row['description']);
$index['picture'] = base64_encode($row['picture']);
#echo '<img src="data:image/jpeg;base64,'.base64_encode( $row['picture'] ).'"/>';
array_push($result['posts'], $index);
$index['picture'] = null;
}
$result['success'] = "1";
$result['message'] = "success";
echo json_encode($result, JSON_UNESCAPED_UNICODE);
mysqli_close($conn);
} else {
$result['success'] = "0";
$result['message'] = "error";
echo json_encode($result, JSON_UNESCAPED_UNICODE);
mysqli_close($conn);
}
Set $index to an empty array at the start of the while loop, this should clear any existing values from the previous iteration
you are not resetting $index variable on each row.
just add $index=array(); as below.
if ( mysqli_num_rows($response) > 0 ) {
while ($row = mysqli_fetch_array($response)){
$index = array(); // this one.
}
}
Firstly I got the workers name from BIRTHDAYS and then want to get e-mail address from USERS.There is no problem to take workers name's from Table1 but when I try to get the e-mail addresses the db returns me NULL.My DB is mssql.
<?php
include_once("connect.php");
$today = '05.07';
$today1 = $today . "%";
$sql = "SELECT NAME FROM BIRTHDAYS WHERE BIRTH LIKE '$today1' ";
$stmt = sqlsrv_query($conn,$sql);
if($stmt == false){
echo "failed";
}else{
$dizi = array();
while($rows = sqlsrv_fetch_array($stmt,SQLSRV_FETCH_ASSOC))
{
$dizi[] = array('NAME' =>$rows['NAME']);
$newarray = json_encode($dizi,JSON_UNESCAPED_UNICODE);
}
}
foreach(json_decode($newarray) as $nameObj)
{
$nameArr = (array) $nameObj;
$names = reset($nameArr);
mb_convert_case($names, MB_CASE_UPPER, 'UTF-8');
echo $sql2 = "SELECT EMAIL FROM USERS WHERE NAME = '$names' ";
echo "<br>";
$stmt2 = sqlsrv_query($conn,$sql2);
if($stmt2 == false)
{
echo "failed";
}
else
{
$dizi2 = array();
while($rows1 = sqlsrv_fetch_array($stmt2,SQLSRV_FETCH_ASSOC))
{
$dizi1[] = array('EMAIL' =>$rows['EMAIL']);
echo $newarray1 = json_encode($dizi1,JSON_UNESCAPED_UNICODE);
}
}
}
?>
while($rows1 = sqlsrv_fetch_array($stmt2,SQLSRV_FETCH_ASSOC))
{
$dizi1[] = array('EMAIL' =>$rows['EMAIL']);
echo $newarray1 = json_encode($dizi1,JSON_UNESCAPED_UNICODE);
}
you put in $rows1 and would take it from $rows NULL is correct answer :)
take $rows1['EMAIL'] and it would work
and why foreach =?
you can put the statement in while-loop like this:
while ($rows = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
$names = $rows['NAME'];
$sql2 = "SELECT EMAIL FROM USERS WHERE NAME = '$names' ";
echo "<br>";
$stmt2 = sqlsrv_query($conn, $sql2);
if ($stmt2 == false) {
echo "failed";
} else {
$dizi2 = array();
while ($rows1 = sqlsrv_fetch_array($stmt2, SQLSRV_FETCH_ASSOC)) {
$dizi1[] = array('EMAIL' => $rows1['EMAIL']);
echo $newarray1 = json_encode($dizi1, JSON_UNESCAPED_UNICODE);
}
}
}
I'm working with SQLite on my webserver and have had no problems until now.
$sql = "SELECT * from TeammateCurrent;";
$ret = $db->query($sql);
if($ret != false) {
while($row = $ret->fetchArray(SQLITE3_ASSOC) ){
$uniqueid = $row['uniqueID'];
$name = $row['Name'];
$lineID = $row['LineID'];
$job = $row['Job'];
$sunday = $row['Sunday'];
$monday = $row['Monday'];
$tuesday = $row['Tuesday'];
$wednesday = $row['Wednesday'];
$thursday = $row['Thursday'];
$friday = $row['Friday'];
$saturday = $row['Saturday'];
//echo $name."<br>".$lineID."<br>".$job."<br>".$sunday."<br>".$monday."<br>".$tuesday."<br>".$wednesday."<br>".$thursday."<br>".$friday."<br>".$saturday."<br><br>";
$sql = "INSERT INTO TeammateHistory (uniqueID, Name,LineID,Job,Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday)
VALUES (NULL, '$name','$lineID','$job','$sunday','$monday','$tuesday','$wednesday','$thursday','$friday','$saturday');";
$ret = $db->exec($sql);
if(!$ret){
echo $db->lastErrorMsg();
} else {
}
$sql2 = "UPDATE TeammateCurrent set Sunday='$day0', Monday='$day1', Tuesday='$day2', Wednesday='$day3', Thursday='$day4', Friday='$day5', Saturday='$day6' where uniqueID='$uniqueid';";
$ret = $db->exec($sql2);
if(!$ret){
echo $db->lastErrorMsg();
} else {
}
}
} else {
echo "Query not successful";
}
When the code above executes, I get the error Fatal error: Call to a member function fetchArray() on a non-object in /home/xxxx/public_html/xxxx.com/trg/rc2/db/startNewWeek.php on line 39
I've ran code just like this on other pages and they work fine, I'm at a total loss here.
My tables:
you're reusing the variable $ret. You should have a different return variable for the SELECT statment and UPDATEs. In the second loop you're trying to do a fetchArray() of the return of an UPDATE statement.
i try to get results from my server. my application sending an array with users info and should insert that into the DB.
i get the following result:
{"flag":"failed","msg":"insert event","event id":"89","invitedusers":"[0508690186, 0508690187]","size_invited":1,"user_query":"SELECT id From users WHERE "}
and i would like to know how can i read the values in PHP under "invitedusers":"[0508690186, 0508690187]"
this is my php code:
<?php
/**
* Created by PhpStorm.
* User: matant
* Date: 9/17/2015
* Time: 2:56 PM
*/
include 'response_process.php';
class CreateEvent implements ResponseProcess {
public function dataProcess($dblink)
{
$output = array();
$sport = $_POST["sport_type"];
$date = date("Y-m-d",strtotime(str_replace('/','-',$_POST["date"])));
$s_time =$_POST["s_time"];
$e_time = $_POST["e_time"];
$lon = $_POST["lon"];
$lat = $_POST["lat"];
$event_type = $_POST["event_type"];
$max_p = $_POST["max_participants"];
$sched = $_POST["scheduled"];
$gen = $_POST["gender"];
$min_age = $_POST["minAge"];
$query = "SELECT * FROM event WHERE (event.longtitude = '$lon' AND event.latitude = '$lat')
AND event.event_date = '$date' And ((event.start_time BETWEEN '$s_time' AND '$e_time') OR (event.end_time BETWEEN '$s_time' AND '$e_time'))";
//AND (event.start_time = '$s_time' AND event.end_time = '$e_time')
//check time and place of the event
$result_q = mysqli_query($dblink,$query) or die (mysqli_error($dblink));
if(!$result_q)
{
$output["flag"]= "select failed";
$output["msg"] = $result_q;
return json_encode($output);
}
//case date and time are available
else {
$no_of_rows = mysqli_num_rows($result_q);
if ($no_of_rows < 1) {
$output["flag"] = "success";
$output["msg"] = "insert event";
$result = mysqli_query($dblink, "INSERT into event(kind_of_sport,event_date,start_time,end_time,longtitude,latitude,private,gender,min_age,max_participants,scheduled,event_status)
VALUES ('$sport','$date','$s_time','$e_time','$lon','$lat','$event_type','$gen','$min_age','$max_p','$sched','1')") or die (mysqli_error($dblink));
if (!$result) {
$output["flag"] = "failed to create event";
// return (json_encode($output));
}
if(isset($_POST["invitedUsers"])){
$query_id = "SELECT id From event WHERE event.event_date = '$date' and event.start_time = '$s_time' and event.end_time = '$e_time'";
$event_s_res = mysqli_query($dblink,$query_id) or die (mysqli_error($dblink));
if(!$event_s_res)
{
$output["flag"] = "failed";
$output["msg"] = "Event id not found";
}
else{
$row = mysqli_fetch_assoc($event_s_res);
$output["event id"]=$row["id"];
$json = json_decode($_POST["invitedUsers"]);
$invited_users = str_replace("\\","",$json);
$output["invitedusers"] = $_POST["invitedUsers"] ;
$output["size_invited"] = count($_POST["invitedUsers"]);
$query_users = "SELECT id From users WHERE ";
$i=0;
foreach($invited_users as $user) {
if ($i < (count($invited_users) - 1))
// add a space at end of this string
$query_users .= "users.mobile = '".$user[$i]."' or ";
else {
// and this one too
$query_users .= "users.mobile = '".$user[$i]."' ";
$output["users"][] = $user['mobile'];
}
$i++;
$output["index"]=$i;
}
$output["user_query"]= $query_users;
/* $event_user_s_res = mysqli_query($dblink,$query_users) or die (mysqli_error($dblink));
if(!$event_user_s_res)
{
$output["flag"] = "failed";
$output["msg"] = "user id not found";
}*/
}
$output["flag"] = "failed";
}
}
else {
$output["flag"] = "failed";
$output["msg"] = "Place is already occupied in this time";
}
}
return json_encode($output);
}
}
i resolve this issue by passing a JSON object from the application and using
json_decode method which convert it back.
I would appreciate it if anyone willing to tell how to echoing /print.
Below is the process of entering data into the database, before inserting it how can I echoing it to the table?
<?php
session_start();
if(isset($_POST['submit']))
{
include('class/stock_class.php');
$st = new st_exchange_conv(DEFAULT_SOURCE);
$from = mysql_real_escape_string(stripslashes($_POST['from']));
$value = floatval($_POST['amount']);
$date = date('Y-m-d H:i:s');
$_SESSION['selected'] = $from;
$stocks = $st->stocks();
asort($stocks);
foreach($stocks as $key=>$stock)
{
$st->convert($from,$key,$date);
$stc_price = $st->price($value);
$stock = mysql_real_escape_string(stripslashes($stock));
$count = "SELECT * FROM oc_stock WHERE stock = '$key'";
$result = mysql_query($count) or die(mysql_error());
$sql = '';
if(mysql_num_rows($result) == 1)
{
$sql = "UPDATE oc_stock SET stock_title = '$stock', stc_val = '$stc_price', date_updated = '$date' WHERE stock = '$key'";
}
else
{
$sql = "INSERT INTO oc_stock(stock_id,stock_title,stock,decimal_place,stc_val,date_updated) VALUES ('','$stock','$key','2',$stc_price,'$date')";
}
$result = mysql_query($sql) or die(mysql_error().'<br />'.$sql);
}
header("Location: index.php");
exit();
}
?>
Insert this:
echo "<table><tr><th>".implode(array_keys($stocks), '</th><th>')."</th></tr>";
foreach($stocks as $row) echo "<tr><td>".implode('</td><td>', $row)."</tr>";
echo "</table>";
Edit: If printing the data is the goal and the table-view is not important, I recommend print_r($stocks) instead.