Mysqli query fetching no records - php

I am trying to run a PHP script that uses mysqli library, but it fetches no results on the webhosting server while in my machine running WLAMP PHP 5.14 it works well.
Can you please tell me if the problem is related with mysqli and if there is a way to make it work for my case?.
The lines I am looking to make work look like this:
$sql = "call sp_give_me_data(null,null,null);";
if (!$mysqli->query($sql)) {
die('Invalid query: ' . mysql_error());
} else {
$result = $mysqli->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$datainfo[] = $row;
}
$output = json_encode($datainfo);
echo utf8_encode($output);
} else {
echo "0";
}
}
as I said it fetches "0" records on the hosting which supports PHP 5.3-5.5 and it fetches the correct number of records on my machine. The connection to the remote database (which is the same in both cases) seems successful as well.
Thanks for the insight.
Update
I changed my code so now I am sending the query just once:
mysqli_set_charset($mysqli,"utf8"); //utf-8 accents query
$result = $mysqli->query($sql);
if (!$result) {
die('Invalid query: ' . mysql_error());
} else {
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$datainfo[] = $row;
}
//utf-8 accents unescaping to be shown correctly
$output = json_encode($datainfo,JSON_UNESCAPED_UNICODE);
echo $output;
} else {
echo "0";
}
}
This works now. Thx!

Your table is empty in the remote server and not empry in local

With the suggestion of #VolkerK, this works now:
mysqli_set_charset($mysqli,"utf8"); //utf-8 accents query
$result = $mysqli->query($sql);
if (!$result) {
die('Invalid query: ' . mysql_error());
} else {
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$datainfo[] = $row;
}
//utf-8 accents unescaping to be shown correctly
$output = json_encode($datainfo,JSON_UNESCAPED_UNICODE);
echo $output;
} else {
echo "0";
}
}

Related

Mysql delete a row php

I have a MySQL table and i would like to delete a row in my table and after deleting the row the result must show all data left in my table .
<?php
include 'Connection.php';
// Create connection
$con= mysqli_connect($host,$user,$pass,$db);
if ($con->connect_error) {
die("Connection failed: " . $con->connect_error);
}
$sql = "SELECT * FROM productos";
$result = $con->query($sql);
if ($result->num_rows >0) {
while($row[] = $result->fetch_assoc())
{
$tem = $row;
$json = json_encode(array("productos"=>$tem));
}
} else {
echo "No Results Found.";
}
echo $json;
$con->close();
?>
This is my select code... now i don't know how create the delete function..
Any help please.
You are overwritting, make array of rows and then echo json_encode at last
$items = array();
if ($result->num_rows >0) {
while($row = $result->fetch_assoc())
{
$items[] = $row;
}
} else {
echo "No Results Found.";
}
echo json_encode(array("productos"=>$items));
$con->close();

Search multiple words in differnt table at a time in Mysql php

0)
{
while($row = mysqli_fetch_array($result))
{
echo $company_data[] = $row;
}
}
else
{
echo 'Data not Found';
}
}
?>
Refer Insert Query.
Your code should have condition-
if ($rest)

Conditional Logic on MySQL Query with PHP

I am doinga MySQL query to retreive data using PHP. I need to have a logic that if the data set returned is empty it shows a warning message else it displays the results:
$searchQuery = mysql_escape_string($_POST['searchQuery']);
$sql="SELECT * FROM db.tblname WHERE column1 = '".$searchQuery."'";
$result = mysqli_query($conn,$sql);
while($row = mysqli_fetch_array($result)){
echo $row["column1"];
}
mysqli_close($conn);
You need num_rows on the object
$result = mysqli_query($conn,$sql);
if ($result->num_rows == 0) {
echo 'result empty';
} else {
while($row = mysqli_fetch_array($result)){
echo $row["column1"];
}
}
http://php.net/manual/en/mysqli-result.num-rows.php
Change mysql_escape_string to mysqli_escape_string here
$where = "";
if(isset($_POST['searchQuery']) && trim($_POST['searchQuery'])){
$searchQuery = mysqli_escape_string($conn,$_POST['searchQuery']);
$where =" WHERE column1 = '".$searchQuery."'";
}
$sql="SELECT * FROM db.tblname ".$where;
It's as simple as this:
if ($result) {
//Your code
} else {
echo "Error: ".mysqli_error($conn);
}
First check with mysqli_num_rows(). Here is how to
$searchQuery = mysql_escape_string($_POST['searchQuery']);
$sql="SELECT * FROM db.tblname WHERE column1 = '".$searchQuery."'";
$result = mysqli_query($conn,$sql);
if(mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_array($result)){
echo $row["column1"];
}
}
else {
echo "No records found";
}
mysqli_close($conn);
$searchQuery = mysql_escape_string($_POST['searchQuery']);
$sql="SELECT * FROM db.tblname WHERE column1 = '".$searchQuery."'";
$result = mysqli_query($conn,$sql);
if(mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_array($result)){
echo $row["column1"];
}
} else {
echo "No results found";
}
mysqli_close($conn);

changing data fetched from server in json format in php

ive got a script in php which fetched data from server, but i want to encode it in json format.
<?php
// attempt a connection
$dbh = pg_connect("host=10.22.35.11 dbname=iwmp_dev2 user=postgres ");
if (!$dbh) {
die("Error in connection: " . pg_last_error());
}
// execute query
//$sql = $_POST['pLat'];
$sql = "SELECT officer_name FROM iwmp_officer";
$result = pg_query($dbh, $sql);
if (!$result) {
die("Error in SQL query: " . pg_last_error());
}
$array = array();
while ($row = pg_fetch_assoc($result)) {
$i++;
$comm = implode(",",$row);
echo json_encode($comm);
}
// free memory
pg_free_result($result);
// close connection
pg_close($dbh);
?>
but my ouptput is coming in format
"V. M. ARORA""Dr. C. P. REDDY""ARTI CHOWDHARY""JAGDISH SINGH"
also when using implode func on *pgsql_fetch_assoc*, no ","(coma's) are coming.
please help
I think you're doing it in wrong way. Try to do like below.
while ($row = pg_fetch_assoc($result)) {
$rows[] = $row;
}
echo json_encode($rows);
Try this;
while ($row = pg_fetch_assoc($result)) {
echo json_encode($row);
}
You don't need to implode the $row. Try replacing your while loop with below code
while ($row = pg_fetch_assoc($result)) {
echo json_encode($row);
}

sending variable to function to execute odbc SELECT

I am trying to execute the function below but does not display anything.
function displayNr($x){
$sql="SELECT D4741 FROM table_x WHERE D4711='".$x."';
if (!$result = odbc_exec($pconn, $sql)) {
echo "Query error! ODBC: ", odbc_error();
} else {
while ($row = odbc_fetch_array($result)) {
echo $row["D4741"] . "\n";
}
}
}
displayNr('name');
However, if I remove the function it works correctly:
x='name';
$sql="SELECT D4741 FROM table_x WHERE D4711='".$x."';
if (!$result = odbc_exec($pconn, $sql)) {
echo "Query error! ODBC: ", odbc_error();
} else {
while ($row = odbc_fetch_array($result)) {
echo $row["D4741"] . "\n";
}
}
What could be the problem?
$pconn is not set in the function.
in the function, $pconn is a new local variable (with a scope of the function) and is not the same $pconn defined outside the function. pass it in as a parameter:
function displayNr($x,$pconn){
$sql="SELECT D4741 FROM table_x WHERE D4711='".$x."';
if (!$result = odbc_exec($pconn, $sql)) {
echo "Query error! ODBC: ", odbc_error();
} else {
while ($row = odbc_fetch_array($result)) {
echo $row["D4741"] . "\n";
}
}
}
displayNr('name');
watch out for SQL injection!!! your code is a perfect example of what not to do!!:
$sql="SELECT D4741 FROM table_x WHERE D4711='".$x."';
see this: SQL Injection or just google it
function displayNr($x, $pconn)
{
$sql = "SELECT D4741 FROM table_x WHERE D4711='" . $x . "'"; //here " is remaining
if (!$result = odbc_exec($pconn, $sql)) {
echo "Query error! ODBC: ", odbc_error();
} else {
while ($row = odbc_fetch_array($result)) {
echo $row["D4741"] . "\n";
}
}
}
displayNr('name');

Categories