Results of MySQL Query limited - php

I try to get a RESTApi with php ans MySQL running and I got pretty far. I'm more a Frontend guy so maybe you can help me figure this out. My code is this:
$sql = 'SELECT * FROM op_content';
$connection = new PDO(DB_CONN, DB_USERNAME, DB_PASSWORD);
$query = $connection->prepare($sql);
$query->execute();
$result = array();
if ($query->rowCount() > 0) {
while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
$result[] = $row;
}
$this->response($this->_toJson($result), 200);
} else {
$this->response('', 204);
}
private function _toJson($data) {
return is_array($data) ? json_encode($data) : '';
}
I dont get any results back from this, only if I add LIMIT 7 (Limit hast to be <= 7) to the SQL-Query. Are there any server-side limits (working on XAMPP atm) or where can that come from?
Edit:
Playing around a little bit I found out that I can return the results as XML (all of them), is there any reason this does not work with json?

OK, I finally figured it out. Since I'm from Germany we use umlauts, and altough I got back utf8, somehow it didnt really work, so I had to encode it first.

Related

mysqli_query only returning results when there's a WHERE statement

MySQL query is only showing information if I have a condition in the SQL statement.
I've successfully used the SQL statement in phpmyadmin and it works great. I've changed the table name in the PHP code and it functions properly, the "people" table is the only one that causes a problem.
<?php
include 'dbconnectLocal.php';
$sql = "SELECT * FROM people WHERE nameFirst = 'Karen'";
$billings = array();
$billingResults = mysqli_query($connL, $sql);
while($row = mysqli_fetch_assoc($billingResults)){
$billings[] = $row;
}
mysqli_close($connL);
$jsonOutput = json_encode($billings);
print("<pre>".json_encode($billings, JSON_PRETTY_PRINT)."</pre>");
?>
The above code produces the desired result, it gives me a JSON result of everyone whos name is Karen. But if I were to change it to $sql = "SELECT * FROM people" I get a blank screen.
Json have 4 mb limitation. You can make a var_dump of $billings to check what you get from db
After much digging around and frustration, it turned out to be a json_encode error. Specifically a UTF8 problem. Found this solution on php.net. I ran the array through this function before encoding it and it worked perfect.
function utf8ize($d) {
if (is_array($d)) {
foreach ($d as $k => $v) {
$d[$k] = utf8ize($v);
}
} else if (is_string ($d)) {
return utf8_encode($d);
}
return $d;
}

Cannot display image from DB2 in php pdo from blob format

we are working on HRM system where user profile pictures are displayed. We have a working Java version which take blob from db2 and after processing displays it. It works fine. Now we are trying to do the same stuff with php using pdo.
Here is the code we use to get the job done but it does not work.
BLOB is in format of
FFD8FFE000104A46494600010201012C012C0000FFE1129345786966000049492A000800000009000F01 ....
Code:
static function arr('SELECT * FROM TABLE WHERE ID_PERSON= 22')
{
global $conn;
$n = count($conn->query($sql)->fetchAll());
if ($n > 0) {
$q = $conn->query($sql);
if (!$q) {
$rs = $conn->errorInfo();
}
while ($row = $q->fetch(PDO::FETCH_ASSOC)) {
$rs[]=$row;
}
} else {
$rs = 'empty';
}
return $rs;
}
in page it is located as like this
<img src="data:image/jpeg;base64,<?=base64_encode(hex2bin($rs['IMAGE_COLUMN']))?>"
and it is view like this (almost similar like the java result but it is not and does not work).
src="data:image/jpeg;base64, /9j/4AAQSkZJRgABAgEBLAEsAAD/4RKTRXhpZgAASUkqAAgAAAAJAA8BAgAGAAAAegAAABABAgAWAAAAgAAAABIBAwABAAAAAQAAABoBBQABAAAAlgAAABsBBQABAAAAngAAACgBAwABAAAAAgAAADEBAgAbAAAApgAAADIBAgAHYCAACdggUAAQAAAH4CAAAiiAMAAQAAAAMAAAAniAMAAQAAAPQBAAAwiAMAAQAAAAIAAAAyiAQAAQAAAPQBAAAAkAcABAAAADAyMzADkAIAFAAAAIYCAAAEkAIAFAAAAJoCAAABkgoAAQAAAK4CA ....
We are might be wrong on hex2bin but it is taken advise from Stack Overflow and its result much more like the working one. and without it is is not looking like base64 at all.

Fetch returning null, but rowCount giving the correct number of rows

So I have this little PDO code right here:
$list=array();
$stmt = $pdo->prepare("SELECT * FROM cal_project JOIN cal_customer ON cal_project.cust_id = cal_customer.cust_id" . $limit);
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$list[] = $row;
}
return $list
Which returns empty when I echo it to my app. Now, I also have this little snipbit:
$num_returns = $stmt->rowCount();
Which returns the proper number of rows every time. Any ideas what could be the cause of the issue? I know that the sql works on its own, both from row count and testing in phpMyAdmin
Edit:
So response calls to list keys, the function that the code above is held in:
$response['list'] = listKeys($request,$object->techID, $limit);
echo json_encode($response);
As I stated in a comment, this code works perfectly fine in PHP 5.4, but not in 5.5 and above
Edit 2: Added in declaration for $list to help subvert confusion and returned $list because I forgot to paste it in

Convert MySQL query into JSON object using PHP

I am new to programming and have a question about converting a MYSQL query with a Join into a JSON object using PHP. When running the statement through phpMyAdmin I get results. However, when attempting the convert it into a JSON object I am getting a blank screen. Any help is greatly appreciated! Here is my code:
$myquery = "SELECT track_ticseverity.date, track_ticseverity.ticnum, track_ticseverity.user_id, track_fatigue.date, track_fatigue.fatiguenum, track_fatigue.user_id
FROM track_ticseverity
INNER JOIN track_fatigue
ON track_ticseverity.date=track_fatigue.date
WHERE track_ticseverity.user_id=1
AND track_fatigue.user_id=1;"
$query = mysqli_query($conn, $myquery);
if ( ! $query ) {
echo mysqli_error();
die;
}
$data = array();
for ($x = 0; $x < mysqli_num_rows($query); $x++) {
$data[] = mysqli_fetch_assoc($query);
}
echo json_encode($data);
mysqli_close($server);
Did you connect to your database? (mysqli_connect)
It's not in your code example, but maybe you did anyway and didn't copy it.
If you did, to which variable did you assign the connection? Once you're using $conn in mysqli_query and once youre using $server in mysqli_close.
Maybe this is helping already even I think PHP should show errors in this case?
Another Tipp
You can easily write the following:
while($datarow = mysqli_fetch_assoc($query)) {
$data[] = $datarow;
}
Like this, you can save the for-loop.
I just figured it out. I ended my Query with ;" instead of ";
Thanks for your replies! Sorry I did not catch that before posting!

How to fix mysql SELECT query from always stopping at a specific record?

I'm trying to make a php tool that will get the users registered between specific dates. However, I'm running into a strange issue. The query doesn't seem to recognize all of the records in the table. Let me explain.
Here's the php code:
function getRegistrations($startDate,$endDate) {
//echo $startDate;
//echo $endDate;
$startDate = date('Y-m-d H:i:s',$startDate);
$endDate = date('Y-m-d H:i:s',$endDate);
$query = "SELECT * FROM database.user WHERE created_date>='".$startDate."' AND created_date<='".$endDate."';";
$result = mysql_query($query);
$data = array();
$data['query'] = $query;
$data['content'] = array();
if($result) {
$data['status'] = 1;
$data['message'] = "Success";
}
$count = 0;
$data['num_rows'] = mysql_num_rows($result);
while ($row = mysql_fetch_assoc($result)) {
$data['content'][$row['user_id']] = $row;
$count++;
}
$data['count'] = $count;
echo json_encode($data);
}
When I run the query in the mysql workbench, it works just fine. However, when I'm running it through php, it seems to ignore every record after a certain one. I can see all the records up to user_id=243, and then it ends. num_rows and count are still equal, and they're equal to the length of content. However, if I run the query directly from the mysql workbench, I can see that it should be much higher.
Also, it's always stopping at the same record, regardless of the dates. If I choose an early start date, then it might return 50 records, but it still stops at the same one.
Does anyone have any ideas? Although I'm pretty new to MySQL, I haven't been able to find anyone with a similar problem.
Thanks in advance!
delete that one particular row. simple as that. reinsert it and see if the problem persists.
mysql_query() says query string should not end with a semicolon. Your query should be
$query = "SELECT * FROM database.user WHERE created_date>='".$startDate."' AND created_date<='".$endDate."'";

Categories