I am getting Fatal error: Cannot pass parameter 3 by reference in line# 4
please suggest me solution I want the binding part dynamic.
$values = array($username,$password);
$query = "select * from users where email_id = ? and password = ?"
$this->con = new mysqli('localhost', 'username', 'password','dbname');
$stmt = $this->con->prepare($query);
$count = 0;
for ($i = 0; $i < count($values); $i++) {
$stmt->bind_param(++$count,$values[$i], PDO::PARAM_STR,12);
}
if ($stmt->execute()) {
while ($row = $this->stmt->fetch()) {
$data[] = $row;
}
return $data;
} else {
return null;
}
use bindValue()
$stmt->bindValue(++$count,$values[$i], PDO::PARAM_STR,12);
Related
heyya all, well pdo is kinda new to me and i sure got no idea how to get this bit of code converted into pdo, if one of you could help me out in this would really be a great help
here is my code
$unique_ref_length = 8;
$unique_ref_found = false;
$possible_chars = "23456789BCDFGHJKMNPQRSTVWXYZ";
while (!$unique_ref_found) {
$unique_ref = "";
$i = 0;
while ($i < $unique_ref_length) {
$char = substr($possible_chars, mt_rand(0, strlen($possible_chars)-1), 1);
$unique_ref .= $char;
$i++;
}
$query = "SELECT * FROM table WHERE ref ='".$unique_ref."'";
$result = mysql_query($query) or die(mysql_error().' '.$query);
if (mysql_num_rows($result)==0) {
$unique_ref_found = true;
}
}
$ref = $unique_ref;
its fixed nevermind and thanks
$qry = "SELECT * FROM table WHERE token ='".$unique_ref."'";
$stm = $db->prepare($qry);
$stm->execute();
if ( $row = $stm->rowCount()==0) {
$unique_ref_found = true;
}
I have this function :
public function RemplirTab($nomCol)
{
$username = $this->getDb()->getUsername();
$sql = "SELECT DISTINCT $nomCol
FROM nautilus_users_page, nautilus_users_acces, nautilus_users_droit, nautilus_users_privilege, nautilus_users_menu
WHERE nautilus_users_page.id_page = nautilus_users_acces.id_page
AND nautilus_users_acces.id_droit = nautilus_users_droit.id_droit
AND nautilus_users_droit.id_droit = nautilus_users_privilege.id_droit
AND nautilus_users_page.id_menu = nautilus_users_menu.id_menu
AND login='$username'";
$row = $this->getDb()->fetchAssoc($sql, array($nomCol, $username));
$i = -1;
$Tab = array();
while($result = $row)
{
$i = $i+1;
$Tab[$i] = $result[$nomCol];
}
return $Tab;
}
Which shows me an error:
I use Silex with Doctrine DBAL.
This function was mysqli with this form:
function RemplirTab($nomCol, $login)
{
$sql = "SELECT DISTINCT $nomCol
FROM nautilus_users_page, nautilus_users_acces, nautilus_users_droit, nautilus_users_privilege, nautilus_users_menu
WHERE nautilus_users_page.id_page = nautilus_users_acces.id_page
AND nautilus_users_acces.id_droit = nautilus_users_droit.id_droit
AND nautilus_users_droit.id_droit = nautilus_users_privilege.id_droit
AND nautilus_users_page.id_menu = nautilus_users_menu.id_menu
AND login='$login'";
$link = connectdb('nautilus_users');
$req = execquery($link, utf8_decode($sql));
$i = -1;
while($row = $req->fetch_assoc())
{
$i = $i+1;
$Tab[$i] = $row[$nomCol];
}
return $Tab;
}
So for some reason when I use the = operater in my prepared statement there is no problem however when I use the below operator ( < ) it gives an error.
mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given.
So I know mysql gives false if there is a problem with my query. however I cant see the problem.
function availableapps($security)
{
if(empty($databasemanager)) { $databasemanager = new databasemanager(); }
$db = $databasemanager->connectionstring();
$result = $db->prepare("SELECT name FROM applications Where security<?");
$result->bind_param("s", $security);
$result->execute();
$types = array();
while(($row = mysqli_fetch_assoc($result->get_result()))) {
$types[] = $row['name'];
}
$result->close();
return $types;
}
Just for demonstration purposes,
final working code:
function availableapps($security)
{
if(empty($databasemanager)) { $databasemanager = new databasemanager(); }
$db = $databasemanager->connectionstring();
$result = $db->prepare("SELECT name FROM applications Where security<?");
$result->bind_param("s", $security);
$result->execute();
$results = $result->get_result();
$types = array();
while($row = $results->fetch_assoc()) {
$types[] = $row['name'];
}
$result->close();
return $types;
}
while ($row = $result->fetch_assoc()) {
I have some user uploaded images that can be sorted and need to save the image position. Was thinking that I could do this easy enough by just using the loop index while iterating through them. However using my $i variable to bind the 3rd param is being passed as a reference and I need the value. How do I get around this?
Here's the code:
$postId = $args['postId'];
$images = explode(",", $args['images']);
$sql = 'INSERT INTO post_image (name,postId,ordinal) VALUES ';
$part = array_fill(0, count($images), "(?, ?, ?)");
$sql .= implode(",", $part);
logit($sql);
try{
$db = DB::getInstance();
$stmt = $db->dbh->prepare($sql);
$count = count($images);
$n = 1;
for($i = 0; $i < $count; $i++){
$stmt->bindParam($n++, $images[$i]);
$stmt->bindParam($n++, $postId);
$stmt->bindParam($n++, $i);
}
$result = $stmt->execute();
if($result !== false) {
return true;
}else {
logit('Query Failed');
return false;
}
}catch(PDOException $e) {
logit($e->getMessage());
return false;
}
I fixed it by using bindValue for the third param.
I would like to return all the results from my prepared query (mysqli) as objects in an array but i cant find a fetchall method or something similar. How do i go about this?
public function getImageResults ($search_term)
{
if (empty($search_term))
return false;
$image_query = $this->db_connection->stmt_init();
$image_query_sql =
"
SELECT
Images.url as url
FROM
Images, ImageSearch, ImageSearchResults
WHERE
ImageSearch.search_string = ? AND
ImageSearchResults.search_id = ImageSearch.id AND
ImageSearchResults.image_id = Images.id AND
Images.deleted = 0 AND
ImageSearch.deleted = 0 AND
ImageSearchResults.deleted = 0
";
if ($image_query->prepare($image_query_sql))
{
$image_query->bind_param('s', $search_term);
$image_query->execute();
$image_query->store_result();
$image_query->bind_result($url);
return //need to return the entire result set here... any ideas?
}
return false;
}
I found this code on the net which kinda works but i get this error when i use it
Notice: Use of undefined constant
mysqli_stmt_bind_result - assumed
'mysqli_stmt_bind_result'
private function getresult ($stmt)
{
$result = array();
$metadata = $stmt->result_metadata();
$fields = $metadata->fetch_fields();
for (;;)
{
$pointers = array();
$row = new stdClass();
$pointers[] = $stmt;
foreach ($fields as $field)
{
$fieldname = $field->name;
$pointers[] = &$row->$fieldname;
}
call_user_func_array(mysqli_stmt_bind_result, $pointers);
if (!$stmt->fetch())
break;
$result[] = $row;
}
$metadata->free();
return $result;
}
The mysqli_stmt_bind_result on this line:
call_user_func_array(mysqli_stmt_bind_result, $pointers);
should be quoted:
call_user_func_array('mysqli_stmt_bind_result', $pointers);
You could also use PDO, an untested example but it should work:
public function getImageResults ($search_term)
{
$sql =
"
SELECT
Images.url as url
FROM
Images, ImageSearch, ImageSearchResults
WHERE
ImageSearch.search_string = ? AND
ImageSearchResults.search_id = ImageSearch.id AND
ImageSearchResults.image_id = Images.id AND
Images.deleted = 0 AND
ImageSearch.deleted = 0 AND
ImageSearchResults.deleted = 0
";
$pdo = new PDO('mysql:dbname=testdb;host=127.0.0.1', 'username', 'password');
$sth = $pdo->prepare($sql);
$sth->execute(array($search_term));
$result = $sth->fetchAll(PDO::FETCH_CLASS, "ArrayObject");
//var_dump($result);//debug
return $result;
}
Hm, you could try something like this, if I remember correctly:
call_user_func_array(array($stmt, 'bind_result'), $pointers);