How to convert PDO to MYSQLi [closed] - php

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I'm following this tutorial, but my problem is that everything is written for PDO, but the my website is MySQLi.
I've been trying to just search for conversions on www.php.net (for example; $statement = $db->prepare($query); = $statement = $db->mysqli_prepare($query) (source))
This is my code that doesn't work:
<?php
$query = "
SELECT shape FROM inventory
";
$statement = $db->mysqli_prepare($query)
$statement->mysqli_execute();
$result = $statement->mysqli_fetch()
foreach($result as $row) {
?>
<!-- HTML code here -->
<?php } ?>
It's supposed to query the shape column from the database but I keep getting this error Fatal error:
Uncaught Error: Call to a member function stmt_init() on null in /homepages/7/d410968336/htdocs/Inventory/vendors/php/Filters/Filters.php:68 Stack trace: #0 {main} thrown in /homepages/7/d410968336/htdocs/Inventory/vendors/php/Filters/Filters.php on line 68
(in this case line 7 $statement = $db->mysqli_prepare($query))

Well, first in a simple select query you really don't need the prepared statement. but if you wanna do I think this would help you.
$query = "SELECT shape FROM inventory";
$statement = mysqli_prepare($db, $query);
mysqli_stmt_execute($statement);
while(mysqli_stmt_fetch($statement)){
....
}

Related

Use WHERE IN with PDO PHP [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am trying to build a shopping cart using PHP, I get a list of IDs from the products added to the basket, I then want to query my database with these IDS using a WHERE IN SQL statement to get the details of the items added to the basket.
At the moment my query just comes back with false.
if(isset($_SESSION["cart"])) {
foreach ($_SESSION["cart"] as $id => $value) {
$ids .= $id . ',';
$count += $value['quantity'];
}
$query = $database->find_item_db($ids);
EDIT I have now changed my function to use the PDO syntax.
function find_item_db($product_code) {
$query = substr($product_code, 0,-1);
$product_codes = explode(",", $query);
$product_code_new = "(".implode("', '", $product_codes).")";
//we need to get product name and price from database.
$sql = "SELECT * FROM `Sweets` WHERE `Sweet_ID` IN :id";
$statement = $this->connection->prepare($sql);
$statement->bindParam(':id', $product_code_new);
$statement->execute();
return $done = $statement->fetchAll();
}
However this is still returning nothing, I get this error in my logs.
/var/www/html/sweetshop/partials/categories-nav.php(32): Database_Functions->find_item_db('1,10,6,23,')\n#2 /var/www/html/sweetshop/category.php(17): include('/var/www/html/s...')\n#3 {main}\n thrown in /var/www/html/sweetshop/classes/class-database-functions.php on line 139, referer: http://localhost/sweetshop/category.php?type=Chocolate
I know my connection works fine as all my other queries work perfectly.
1. Incorrect syntax
If $ids is something like:
$ids = "1,2,3,4,5";
Then the query is:
SELECT * FROM `Sweets` WHERE `Sweet_ID` IN (1,2,3,4,5)
Which is incorrect because each value needs to be wrapped in single quotes:
function find_item_db($product_code){
$query = substr($product_code, 0,-1);
//Wrap each product id
$product_codes = explode("," $product_code);
$product_codes = "'".implode("', '", $product_codes)."'";
//.......
}
That way the query will read:
SELECT * FROM `Sweets` WHERE `Sweet_ID` IN ('1', '2', '3', '4', '5')`
2. Mixing SQL APIs
mysqli_* syntax and PDO syntax are not interchangeable. bind_param() is for PDO, however your query is using mysqli.
When you are using $statement->bind_param(':id', $new);, what are you binding? There is no :id value in the query, and therefore the line is unnecessary as well as incorrect SQL query format!

Fatal error: Call to a member function bindParam() on boolean in D:\xampp\htdocs\ipack\insertstatus.php on line 14 [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I am getting an error while running this code:
Fatal error: Call to a member function bindParam() on boolean in D:\xampp\htdocs\ipack\insertstatus.php on line 9
<?php
header('Access-Control-Allow-Origin: *');
include 'dbconnection.php';
$jobno = "AFE/0001/2015";
$jobseq = 0;
//to get INTJOBNO
$intjobno = "";
$data = $dbh->query("select INTJOBNO from PRTJOBHD where JOBNO = :jobno and JOBSEQ = :jobseq");
$data->bindParam(':jobno',$jobno,PDO::PARAM_STR);
$data->bindParam(':jobseq',$jobseq,PDO::PARAM_STR);
$data->execute();
foreach($data as $row) {
$intjobno = $row['INTJOBNO'];
echo $intjobno;
}
>
Have a look at this answer: PDO's query vs execute. You cannot bind parameters to PDO query, you need to use prepare instead.
header('Access-Control-Allow-Origin: *');
include 'dbconnection.php';
$jobno = "AFE/0001/2015";
$jobseq = 0;
//to get INTJOBNO
$intjobno = "";
$data = $dbh->prepare("select INTJOBNO from PRTJOBHD where JOBNO = :jobno and JOBSEQ = :jobseq");
$data->bindParam(':jobno',$jobno,PDO::PARAM_STR);
$data->bindParam(':jobseq',$jobseq,PDO::PARAM_STR);
$data->execute();
foreach($data as $row) {
$intjobno = $row['INTJOBNO'];
echo $intjobno;
}
PDO::query() returns a PDOStatement object, or FALSE on failure.
Source
It means your query has failed for some reason.
In this case you are using the wrong function to do what you want to do.
You need to prepare your statement since you want to bind two parameters in your query.
Use $dbh->prepare() instead of $dbh->query().

Printing PDO query results [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I am trying to print all the results of a query, but for some reason I get the following error: Fatal error: Call to a member function fetch() on boolean in H:\Some-Location\ on line X
This is my code:
<?php
$query = "SELECT adID FROM given WHERE toUser = :userid";
$query_params = array( ':userid' => $_SESSION['user']['ID'] );
try
{
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch(PDOException $ex)
{
echo "Failed to run query: " . $ex->getMessage();
}
while ($row = $result->fetch(PDO::FETCH_ASSOC))
{
echo $row['adID'];
}
?>
What's wrong?
As the docs and the error show, execute returns a boolean: http://php.net/manual/en/pdostatement.execute.php
You need to call fetch() on the statement, not the return value of execute. : http://php.net/manual/en/pdostatement.fetch.php
so replace $result>fetch() with $stmt->fetch().
The execute() method on the PDOStatement does not return the results, it only signals if the query succeeded or not (only useful when not using exceptions).
PHP Documentation:
public bool PDOStatement::execute ([ array $input_parameters ] )
What you want to do is use the PDOStatement itself to get the data:
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
echo $row['adID'];
}
Note: According to your current logic, the loop will execute even when an exception is thrown. Try either returning from the catch block or putting the loop inside the try block.
$result is only a boolean holding success/failure of execute statement.
You need to run fetch on $stmt:
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
echo $row['adID'];
}
As per documentation here:
http://php.net/manual/en/pdostatement.fetch.php

MySQLi get_result alternative for PHP < 5.3? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have a big problem
public function get_setting($setting) {
// Prepate statement
$prepared = $this->prepare("SELECT `val` FROM `filex_settings` WHERE `setting`=?", 'get_setting');
$this->bind_param($prepared->bind_param('s', $setting), 'get_setting()');
$this->execute($prepared, 'get_setting()');
$result = $prepared->get_result();// < 5.3 PHP
$row = $result->fetch_object();
return $row->val;
}
Alternative please ?
You can try using fetch() with bind_result() instead.
public function get_setting($setting) {
// Prepate statement
$prepared = $this->prepare("SELECT `val` FROM `filex_settings` WHERE `setting`=?", 'get_setting');
$this->bind_param($prepared->bind_param('s', $setting), 'get_setting()');
$this->execute($prepared, 'get_setting()');
$this->bind_result($col_1,$col_2,..)
while($this->fetch())
{
return $col_n // the value which you want
}
}

I used this code for displaying 4 numbers randomly without repeating, but i got error as resource id#3 [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I used this code for displaying 4 numbers randomly without repeating, but i got error as resource id#3
$test = nonRepeat(0,4,4); //calling function nonrepeat defined earlier
for ( $i = 0; $i < 4; $i++ ) {
$result = mysql_query( "select * from abc where id='test[i]'" ); accessing data from data base as id in the array test.
print_r( $result );
}
1.- Don't use mysql_* functions they are deprecated and will not be included in future updates.
2.- Your question is pretty unclear, but when you execute a query using mysql_query you get a resource, so you need to iterate through this:
$test = nonRepeat(0,4,4); //calling function nonrepeat defined earlier
for($i=0;$i<4;$i++)
{
$result = mysql_query( "select * from abc where id='{$test[i]}'" ); <<<---- CHANGED
if ($result){
while ($row = mysql_fetch_assoc($result)) {
print_r($row); //Display each row data
}
}else{
print "Error:" . mysql_error();
}
}
Try this and see what is showing, and take a look to mysqli_ and PDO

Categories