I've tried to find any reference to display data where no data in table for an hours, but I'm not yet found a code to fix the issue
I've this code:
//function
function readAll(){
$query = "SELECT * FROM ".$this->table_name." ORDER BY id_nilai ASC";
$stmt = $this->conn->prepare( $query );
$stmt->execute();
return $stmt;
}
//execute
$pro3 = new Nilai($db);
$stmt3 = $pro3->readAll();
while ($row3 = $stmt3->fetch(PDO::FETCH_ASSOC)){
// not yet fix how to display no data
//if($row3==false){ tried to change $row3==0 still won't work
//echo "No Data";
//}
//die(var_dump($row3)); showing `bool(false)`
echo $row3['ket_nilai'] (echo $row3['jum_nilai'])
}
any idea how to do?
I want to display some text when no data found in database table
You need to fetch the results before entering the loop, check if you do get the results then if you do enter the loop else display the message
see bellow code :
<?php
//function
function readAll(){
$query = "SELECT * FROM ".$this->table_name." ORDER BY id_nilai ASC";
$stmt = $this->conn->prepare( $query );
$stmt->execute();
return $stmt;
}
//execute
$pro3 = new Nilai($db);
$stmt3 = $pro3->readAll();
$results = $stmt3->fetchall(PDO::FETCH_ASSOC);
if(count($results) > 0){
foreach($results as $row3){
//display them
echo $row3['ket_nilai'] ;
echo $row3['jum_nilai'];
}
}else{
echo "No data available";
}
?>
Related
I attempt to get the result of a very simple query with the function query but nothing appears. If I execute the query in PHPMyAdmin, with the same data, I have a result.
There is my code :
$sql = "SELECT * FROM users WHERE email='$email'";
$response = $conn->query($conn, $sql);
The $conn variable is correct, I did an Insert with that.
$response is null. I can do an echo and there is nothing.
What can I do to solve this problem ? What can I check ?
Thank you very much.
You don't need to pass connection in query.
Solution:
$sql = "SELECT * FROM users WHERE email='$email'";
$response = $conn->query($sql);
while($res = $response->fetch_array()){
$name=$res['nameofuser']; //just an example
}
echo $name;
Real solution (prepare stmt):
$sql = "SELECT * FROM users WHERE email=?";
$response = $conn->prepare($sql);
$response->bind_param('s',$email);
if(!$response->execute()){
echo "Error query: " . $response->error . ".";
}
$result=$response->get_result();
while($res = $result->fetch_array()){
$name=$res['nameofuser']; //just an example
}
echo $name;
'Tips' add to real solution check if query is done.
After execute query . fetch the results
$stmt = $conn->prepare( "SELECT * FROM users WHERE email= ? ");
$stmt->bind_param("s", $email);
$stmt->execute();
$result = $stmt->get_result();
if($result->num_rows === 0) exit('No rows');
while($row = $result->fetch_assoc()) {
// your code
}
This is my code but it runs without any results, I have a table "sliders " that contains id and URL for image and I'm sure connect.php is fine.
<?php
$sliders=array();
include"connect.php";
$query_getinfo="select * from sliders";
$result=$connect->prepare($query_getinfo);
$result->execute();
while($row=$result->fetch(PDO:: FETCH_ASSOC)){
$record=array();
$record["id"]=$row["id"];
$record["slide_url"]=$row["slide_url"];
$slider[]=$record;
//$sliders[]=$row["slide_url"];
}
print_r ($sliders);
?>
Looks like you do not have any parameters to pass so you can just replace prepare with query and remove execute, below are 2 examples
if you want to pass parameters
// prepare the query
$sliders = array();
$statement = $connect->prepare("select * from sliders WHERE type = ?");
// assuming type is string eg admin / user
$statement->bind_param("s", $_SESSION['type']);
// execute it
$statement->execute();
// ger result
$result = $statement->get_result();
// check if rows exists in table
if($result->num_rows > 0){
while($row = $result->fetch_assoc()) {
$record=array();
$record["id"]=$row["id"];
$record["slide_url"]=$row["slide_url"];
$sliders[]=$record;
}
print "<pre";
print_r($sliders);
}else{
print "no sliders found";
}
$statement->close();
if you do not have parameters in query
$sliders = array();
// prepare the query
$result = $connect->query("select * from sliders");
// check if rows exists in table
if($result->num_rows > 0){
while($row = mysqli_fetch_assoc($result)) {
$record=array();
$record["id"]=$row["id"];
$record["slide_url"]=$row["slide_url"];
$sliders[]=$record;
}
print "<pre";
print_r($sliders);
}else{
print "no sliders found";
}
// if you want to close connection here.
$connect->close();
$stmt is execute and give Result in Print_r($stmt). Result is this "Resource id #4" but when Print_r($stmt) is put in if (odbc_num_rows($stmt) > 0) {Print_r($stmt);}. it's not give Result. and gone else conditon give message else condition.so How to Put odbc function instead of odbc_num_rows($stmt).if right Parameter pass query execute and gone if condition.
which Odbc function used in if condtion.
<?php
include 'Connection.php';
if(isset($_REQUEST["insert"]))
{
$user = $_GET['user'];
$pwd = $_GET['pass'];
$yid = $_GET['yid'];
$sql = "select RegNo, UserName, Pasword from Std_Reg where UserName= '$user' and Pasword = '$pwd' and YearID = $yid and IsActive = True";
$stmt = odbc_exec($conn, $sql);
$result = array();
if (!empty($stmt)) {
// check for empty result
if (odbc_num_rows($stmt) > 0)
{
print_r($stmt);
$stmt1 = odbc_fetch_array($stmt);
$product = array();
$product['RegNo'] = $stmt1['RegNo'];
$product['UserName'] = $stmt1['UserName'];
$product['Pasword'] = $stmt1['Pasword'];
// success
$result["success"] = 1;
// user node
$result["product"] = array();
array_push($result["product"], $product);
// echoing JSON response
echo json_encode($result);
} else {
// no product found
$result["succes"] = 0;
$result["message"] = "No product found";
// echo no users JSON
echo json_encode($result);
}
//sqlsrv_free_stmt($stmt);
odbc_close($conn); //Close the connnection first
}
}
?>
For INSERT, UPDATE and DELETE statements odbc_num_rows() returns the number of rows affected. The manual says-
Using odbc_num_rows() to determine the number of rows available after a SELECT will return -1 with many drivers.
one way around this behaviour is to do a COUNT(*) in SQL instead. See here for an example.
I have a php script which retrieves data from mysql db.
Everything works fine, but my problem is that this $result = $dao->joinedEvents($userId); returns an array of numbers and what I would like to do is to run this $secondResult = $dao->joinedEventsInfo($receivedIds); for every ID and this script I'm using right now returns data only for one ID.
This is part of my php script:
$userId = htmlentities($_REQUEST["userId"]);
$result = $dao->joinedEvents($userId); //This is getting the IDs array
if(!empty($result)) {
$receivedIds = $result["event_id"];
$ids = explode(",", $receivedIds);
foreach($ids as $id){
$secondResult = $dao->joinedEventsInfo($id);
if(!empty($secondResult)) {
$returnValue["finalResult"][] = $secondResult;
} else {
$returnValue["status"] = "error";
$returnValue["message"][] = "Could not find records for id" . $id;
}
}
} else {
$returnValue["status"] = "Empty error";
$returnValue["message"] = "Could not find records";
}
$dao->closeConnection();
echo json_encode($returnValue);
And this is joinedEvents script:
public function joinedEvents($userId){
$returnValue = array();
$sql = "SELECT event_id from MyTable WHERE userId= '$userId' LIMIT 0 , 30";
$statement = $this->conn->prepare($sql);
if (!$statement)
throw new Exception($statement->error);
$statement->execute();
$result = $statement->get_result();
while ($myrow = $result->fetch_assoc())
{
$returnValue[] = $myrow;
}
return $returnValue;
}
This is joinedEventsInfo script:
public function joinedEventsInfo($eventId){
$returnValue = array();
$sql = "SELECT * FROM Events WHERE eventId = '$eventId' LIMIT 0 , 30";
$statement = $this->conn->prepare($sql);
if (!$statement)
throw new Exception($statement->error);
$statement->execute();
$result = $statement->get_result();
while ($myrow = $result->fetch_assoc())
{
$returnValue[] = $myrow;
}
return $returnValue;
}
Edit: Tha reason I need this is that I have two tables. In the first one I have just IDs and in the second one I have info. So first I need to get the IDs and then I need to get data for every ID I have just received.
Thank you very much , I'm totally stuck.
Based on the updated code snippets and the discussion below, it is found that $result is indeed an array, and the solution is:
$userId = htmlentities($_REQUEST["userId"]);
$result = $dao->joinedEvents($userId);
if(count($result)){
foreach($result as $array){
$event_id = $array['event_id'];
$secondResult = $dao->joinedEventsInfo($event_id);
if(!empty($secondResult)) {
$returnValue["finalResult"][] = $secondResult;
} else {
$returnValue["status"] = "error";
$returnValue["message"][] = "Could not find records for id: " . $event_id;
}
}
}else {
$returnValue["status"] = "Empty error";
$returnValue["message"] = "Could not find records";
}
$dao->closeConnection();
echo json_encode($returnValue);
Have you tried array_map()?
That would allow you to call a php function on each member of an array easily.
Another way would be to use the common while ($row = mysql_fetch_array($result)) which would execute the code in the while loop for each row of your returned results. Note, you will likely have to change the mysql_fetch_array to something specific for your SQL connection.
public function showSingleVisit(){
//echo $this->doctorID; the printed 1111
//$this->doctorID = 1111;
$db = connect_db();
$query = "SELECT * FROM visit WHERE doctorID = :doctorID";
$result = $db->prepare($query);
$result->bindParam(':doctorID', $this->doctorID);
$result->execute();
return $result;
}
This query doesn't return any row but when putting $this->doctorID = 1111 I get the rows that is wanted. I use bindParam in INSERT query in this class and works correctly. What's the problem?
UPDATE:
class visit{
//define public varibles
public function showSingleVisit(){
$db = connect_db();
$query = "SELECT * FROM visit WHERE visit = 0 AND doctorID = :doctorID AND patientID = :patientID";
$result = $db->prepare($query);
$result->bindParam(':patientID', $this->patientID);
$result->bindParam(':doctorID', $this->doctorID);
$result->execute();
return $result;
}
}
Here's how I call the function un the other page:
$visit = new visit;
$visit->doctorID = $auth->user->IDNo;
$visit->caseNo = $_SESSION['caseNo'];
$result = $visit->showSingleVisit();
if($result){
while($row = $result->fetch()){
echo'<p>
<label>Date:</label>
<span>'.$row->visitDate.'</span>
</p>';
}
}
else{
echo "No exists!";
}
Neither it shows any dates, nor it prints "No exists!".
you have to specify the type of the param :
$result->bindParam(':doctorID', $this->doctorID, PDO::PARAM_INT);
look at here :
http://php.net/manual/fr/pdostatement.bindparam.php
since doctorID is integer, you should add data_type to INT. it look like this
$result->bindParam(':doctorID', $this->doctorID, PDO::PARAM_INT);