this is my code where i have problem, here i am not getting any result for stmt2 fetch data,
$result = $user_hash_conn->query($q1);
if($result->num_rows > 0){
$stmt = $store_conn->prepare('SELECT `location`,`institute`, `category`, `sub_category`, `date`,`title`, `message` FROM `pre_demand_by_key` WHERE `key`=?');
$stmt->bind_param("s",$key);
# for no of match found
$stmt2 = $store_conn->stmt_init();
$stmt2 = $store_conn->prepare('SELECT `key` FROM `store_list` WHERE `location`=? AND `institute` LIKE ? AND `category`=? AND `sub_category` LIKE ?');
while($row = $result->fetch_assoc()){
$key = $row['varchar_128'];
$stmt->execute();
$stmt->bind_result($location,$institute,$category,$sub_category,$date,$title,$message);
while($stmt->fetch()){
#no of match found
$location2 = $location;
$institute2 = $institute."%";
$category2 = $category;
$sub_category2 = $sub_category."%";
$stmt2->bind_param("ssss",$location2,$institute2,$category2,$sub_category2);
$stmt2->execute();
$stmt2->bind_result($item_key2);
echo $stmt2->error;
$item_keys = [];
while($stmt2->fetch()){
echo $item_key2;
}
}
}
}
i have record in my table for my second stmt, but here i doesn't give my any result all my code is correctly written(mean i didn't have spelling mistake related to db or tb name), when i manually check it on phpmyadmin it responsed with some result. Please help me. i am struggling with my code for last 4-5 hours
Related
Ive used two queried in a function. FIrst query to find an array of data. And second query is to select rows as per checking the array data from the first query. But the overall function return only one row data.
function getCartItems($conn)
{
$cust_id=$_SESSION['cust_id'];
$stmtSelect1 = $conn->prepare("SELECT product_id FROM tbl_cart WHERE cust_id=cust_id");
$stmtSelect1->bindParam('cust_id',$cust_id);
$stmtSelect1->execute();
$product_id= $stmtSelect1->fetchAll();
foreach($product_id as $productid) {
$stmtfetch = $conn->prepare("SELECT * FROM tbl_item WHERE product_id=:products_id");
$stmtfetch->bindParam(':products_id',$productid['product_id']);
$stmtfetch->execute();
$datas = $stmtfetch->fetchAll();
print_r($datas);
exit();
}
}
First of all, your code has typos (Missing ":" before parameter, product_id/products_id).
This should work:
$cust_id=$_SESSION['cust_id'];
$stmt = $conn->prepare("SELECT tbl_item.* FROM tbl_cart, tbl_item WHERE tbl_cart.cust_id = :cust_id AND tbl_item.product_id = tbl_cart.product_id);
$stmt->bindParam('cust_id',$cust_id);
$stmt->execute();
$rows = $stmt->fetchAll();
foreach($rows as $row)
{
print_r($row);
}
I'm trying to echo out every single thing in a table from sql, my code is as follows
$stmt = $link->prepare("SELECT * FROM articles");
$stmt->execute();
$stmt->store_result();
if (mysqli_stmt_num_rows($stmt) >= 1) {
$result = mysqli_stmt_get_result($stmt); //get result object
while ($row = mysqli_fetch_assoc($result)){ //get associative array
$news = $row['title'];
}
}
It doesn't work, returning as mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given
I've done my research but literally nothing works :(
You don't need to use prepared statements for this SELECT query as you aren't specifying anything WHERE.
$query = 'SELECT * FROM articles';
if ($result = $link->query($query)) {
while ($row = $result->fetch_assoc()) {
$news = $row['title'];
}
}
For a really good in-depth answer check this out: https://stackoverflow.com/a/11575617/1427345
Super new to PHP here, only using PHP to create my json data and having a hard time to understand the syntax. Here is some partial code:
All I am trying to do is to retrieve the value '2af8ddda-2be4-11e5-9453-b82a72d52c35' and put it in variable #sharepointID:
function selectWithSharepointID($table, $columns, $where){
try{
//Get Sharepoint file ID first
$stmt = $this->db->prepare("SELECT ID FROM table1 ORDER BY DownloadedTimeStamp DESC LIMIT 1");
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
//$data[] = array("ID" => $rows['ID']);
//$sharepointID = $data[0];
//$sharepointID = $rows[0];
$where = array('id'=>$sharepointID);
//$where = array('id'=>'2af8ddda-2be4-11e5-9453-b82a72d52c35'); //this works fine
...
PS: also tried to use print_r and echo but cant see anything in the console.
Thank you
You don't need to fetchAll if you only have one record. Try:
$stmt = $this->db->prepare("SELECT ID FROM table1 ORDER BY DownloadedTimeStamp DESC LIMIT 1");
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$sharepointID = $row['ID'];
If you have multiple records the fetchAll makes sense but then you iterate through that to get each row, and its values.
For a rough example where I'd use fetchAll...
$stmt = $this->db->prepare("SELECT name, userid FROM users");
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach($rows as $row){
echo 'Name: ' . $row['name'] . ' userid :' . $row['id'];
}
This expression returns array of rows:
$stmt->fetchAll(PDO::FETCH_ASSOC);
So, you can get data from row 0 in your case:
$sharepointID = $rows[0]['ID'];
When I use the following code, it returns nothing - no errors, no values. When I want to select just one specific row and display that data, using fetch() works fine. But the moment I put it in a loop to get multiple rows, nothing happens. Here is the code I am using:
$stmt = $pdo->prepare("SELECT name FROM company WHERE position = :position AND branch = :branch ORDER BY name ASC");
$position = 23;
$branch = "Management";
$stmt->bindParam(':position', $position, PDO::PARAM_INT);
$stmt->bindParam(':branch', $branch, PDO::PARAM_STR);
$stmt->execute();
while ($row = $stmt->fetch()) {
echo $row['name'];
}
I've made a function that accepts a search column, search term and an id number, and am trying to construct a prepared statement and fetch results, and return in json.
Here is what I have:
function searchBooks($searchColumn, $searchTerm, $teacherid) {
$books = array();
$link = connect_db();
$sql = "SELECT * FROM book WHERE teacher_id = ? AND ? LIKE ?";
$searchTerm = "%{$searchTerm}%";
$stmt = $link->stmt_init();
$stmt->prepare($sql);
$stmt->bind_param('iss', $teacherid, $searchColumn, $searchTerm);
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_array(MYSQLI_BOTH)) {
$book = new Book();
$book->id = $row['id'];
$book->title = $row['title'];
$book->author = $row['author'];
$book->ar_quiz = $row['ar_quiz'];
$book->ar_quiz_pts = $row['ar_quiz_pts'];
$book->book_level = $row['book_level'];
$book->type = $row['type'];
$book->teacher_id = $row['teacher_id'];
array_push($books, $book);
}
mysqli_stmt_close($stmt);
return json_encode($books);
}
I'm using a test page that passes values that I know should return results (using 'the' as a wildcard and 'title' for search column):
echo searchBooks('title', 'the', 1);
...but I am not getting any results at all... [] output on the test page.
Assume connect_db() retrieves a connection. Assume I'm doing all my error checking and everything in my controller level, and might add stuff like that later. Just trying to get results right now. Thanks in advance for anything you can point out.
searchcolumn cannot be a bind variable. You can't bind table/column names
$sql = sprintf("SELECT * FROM `book` WHERE teacher_id = ? AND `%s` LIKE ?", $searchColumn);
$searchTerm = "%{$searchTerm}%";
$stmt = $link->stmt_init();
$stmt->prepare($sql);
$stmt->bind_param('is', $teacherid, $searchTerm);
It would also be a good idea to whitelist $searchColumn, validating that it really is a column in your book table before executing this
EDIT
And why bother using fetch_array(MYSQLI_BOTH) when you're only using associative values from the array? Using fetch_assoc() would be better, or you could be even cleverer, and use fetch_object(), and then you wouldn't need to populate your Book object property by property
Consider:
while ($book = $result->fetch_object('Book')) {
array_push($books, $book);
}