php statements fail to work when wrapped in a function [duplicate] - php

This question already has answers here:
Should I pass my $mysqli variable to each function?
(3 answers)
Closed 7 years ago.
The code below works fine when its not in wrapped in a function
$opt = 'logo_img';
$sql="SELECT option_value FROM r0_options WHERE option_name='".$opt."'";
$result=mysqli_query($db,$sql);
$row=mysqli_fetch_array($result,MYSQLI_NUM);
var_dump($row);
However when I do as follows, and call the function, it gives NULL.
function get_result($opt){
$sql="SELECT option_value FROM r0_options WHERE option_name='".$opt."'";
$result=mysqli_query($db,$sql);
$row=mysqli_fetch_array($result,MYSQLI_NUM);
var_dump($row);
}
get_result('logo_img');

It's because you are not passing $db variable, either pass it to function or do the following:
function get_result($opt){
global $db;
$sql="SELECT option_value FROM r0_options WHERE option_name='".$opt."'";
$result=mysqli_query($db,$sql);
$row=mysqli_fetch_array($result,MYSQLI_NUM);
var_dump($row);
}
get_result('logo_img');

You are forgotten to pass he '$db' to your function:
function get_result($db, $opt){

Related

Issue with PHP MySQL function output [duplicate]

This question already has answers here:
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
(3 answers)
Closed 3 years ago.
I made a simple function that will go into the MYSQL DB and get the field I need based on an id.
It works OUTSIDE the function. Inside the function the SQL statement is correct but I cannot figure out why it will not output the results of $row[$field]. Something I am doing wrong is not displaying when its inside the function.
function getVendor($field,$id) {
$sql = "SELECT $field from manufacturer where id=$id LIMIT 1";
echo $sql;
$result = $conn->query($sql);
while($row = $result->fetch_assoc()) {
echo $row[$field];
}
}
getVendor('name','2');
Your $conn is not in the function scope. Also, You don't need the while loop due the id should be unique.
function getVendor($conn, $field, $id) {
$sql = " SELECT {$field} FROM manufacturer WHERE id={$id} ";
$result = $conn->query($sql);
$row = $result->fetch_assoc();
echo $row[$field];
}
getVendor($conn, 'name','2');

How to make SQL to a function [duplicate]

This question already has answers here:
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
(3 answers)
Closed 4 years ago.
I am trying to make the following code to be a php variable. The idea is so I can get all the user data I need by giving the variable the id of the user.
$sql = "SELECT * FROM `users` WHERE user_id='$user_id'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while ($row = $result->fetch_assoc()) {
$displayname = $row['user_displayname'];
}
}
"display name" here is what I want to get.
It works when not in a function but it does not work in the function.
Perhaps I am doing something wrong, here. is how I am doing it.
function get_userdetails($user_id) {
// Figure out displayname of rulechanger
$sql = "SELECT * FROM `users` WHERE user_id='$user_id'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while ($row = $result->fetch_assoc()) {
$displayname = $row['user_displayname'];
}
}
}
Thanks in advance!
Presumably $conn is a global variable (object) representing your database connection. In order to access it inside a function you need to use the global keyword - in this case global $conn;
You will need to do the same with $displayname if you intend to refer to it outside of the function (after the function has set its value). Probably best here will be to simply return $displayname from your function.

PDO prepared statement for update doesn't work properly [duplicate]

This question already has answers here:
What is the difference between bindParam and bindValue?
(7 answers)
Closed 7 years ago.
This is my php code:
public function update($table,$fields_and_values,$condition_field,$condition_field_value)
{
$query="UPDATE $table SET ";
foreach($fields_and_values as $field=>$value) $query.=($field."=:".$field." ,");
$query.=" ";
$query=str_replace(", "," WHERE ",$query);
$query.=($condition_field."='".$condition_field_value."'");
echo $query;
$stmt=$this->conn->prepare($query);
foreach($fields_and_values as $field=>$value) $stmt->bindParam(":".$field,$value);
$stmt->execute();
}
and this is how i call the function in my class:
$db=new db_connection('localhost','root','','maps');
$db->connect();
$arr=array('username'=>'testfromnewclass3','password'=>'123456');
$db->update('users',$arr,'username','term');
$db->disconnect();
It doesn't matter what the other functions like disconnect do! They work correctly.
My problem is that when this command executes, both username and password become 123456 !
And this is what i get from that echo $query:
UPDATE users SET username=:username ,password=:password WHERE username='term'
Is something wrong with my function? and if so how can i fix it?
Use $stmt->bindValue($field, $value);
instead of $stmt->bindParam(":".$field,$value);
Check this to understand difference between PDOStatement::bindParam() and PDOStatement::bindValue()

PHP: simple mysql query function - cant get it working [duplicate]

This question already has answers here:
How do you debug PHP scripts? [closed]
(30 answers)
Closed 9 years ago.
i know this must be only a small bug, but i cant find it.
My function:
function del_mysql($table,$id)
{
$id = $_GET['id'];
$exec = mysqli_query($con, "delete from $table where id = '$id'");
return $exec;
}
in Code:
if ($_GET['action'] == 'delete')
{
del_mysql("awsome","$id");
}
if make in function:
$id = $_GET['id'];
echo $table;
echo $id;
i get right table and id.
Somebody see the bug?
I removed already the $exec and return part and leave only mysqli_query command. but dont want to work.
The problem is that in your del_mysql function, you are referencing the connection object $con, which does not exist in the scope of the function. Either pass it into the function as a parameter like this:
function del_mysql($table, $id, $con) {
or access it as a global variable like this:
function del_mysql($table, $id) {
global $con;
I hope that helps.
Regards,
Ralfe

php syntax - why does making my code a function stop it working? [duplicate]

This question already has answers here:
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
(3 answers)
Closed 9 years ago.
Probably a simple one so please excuse me.
At the top of my page I include a file:
<?
require("common.php");
?>
Later on in my code I do the following:
$stmt = $db->prepare($query);
$result = $stmt->execute();
$result = $stmt->fetchAll();
(the db connection files being in my include file).
This works fine, until I try and make the code a function For example: If I do
function doAQuery(){
$stmt = $db->prepare($query);
$result = $stmt->execute();
$result = $stmt->fetchAll();
}
It can't find the db details. Please can someone explain why?
It's not a syntax issue. Your $db and $query variables are not available to your function scope. You can make them available by declaring global $db, $query as the first line of your function.
It's because $db is a global variable. PHP ignores it in the function scope unless you explicity declare it should be used.
function doAQuery() {
global $db;
$stmt = $db->prepare($query);
$result = $stmt->execute();
$result = $stmt->fetchAll();
}
Take a look at how variables scope works in PHP.
Pass Query string to function,
function doAQuery($query){

Categories