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');
Related
This question already has answers here:
How to use return inside a recursive function in PHP
(4 answers)
Closed 9 months ago.
I write this recursive function, but it’s not working. I am not able to find the mistake. It’s not returning anything. But if I write an "echo" on the else statement, it’s printing perfectly.
function find_parent($referralid, $leg)
{
$query = "SELECT memberid FROM memberdetails where parentid='" . $referralid . "' and leg='" . $leg . "'";
$result = mysql_query($query);
if (mysql_num_rows($result) == 0)
return $referralid;
else
{
while($row = mysql_fetch_array($result))
{
find_parent($row[0], $leg);
}
}
}
When you have recursion, you need to pass the value back as well, otherwise the value isn't propagated back up. So the line which is...
find_parent($row[0],$leg);
needs to be
return find_parent($row[0],$leg);
This is assuming that you will only ever get 1 row as a parent.
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.
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){
This question already has answers here:
Object of class mysqli_result could not be converted to string
(5 answers)
Closed 1 year ago.
Catchable fatal error: Object of class mysqli_result could not be converted to string in C:\xampp\htdocs\xxx\dash.php on line 20
I am quite fairly new, and being a old-school coder, simply using mysql_result to grab such data, I am unaware of how to go about this. I have a class->function setup.
Line 20 of dash.php contains:
echo $user->GetVar('rank', 'Liam', $mysqli);
While, the function is:
function GetVar($var, $username, $mysqli)
{
$result = $mysqli->query("SELECT " . $var . " FROM users WHERE username = '" . $username . "' LIMIT 1");
return $result;
$result->close();
}
Now, to my understanding, I am meant to convert $result into a string, but I am not fully aware of how to do so. I've tried using a few methods, but to no avail. So I've come to the community to hopefully get a answer, I've also looked around but noticed that all other threads are asking for num_rows, while I just want to grab the string from the query select.
You have to fetch it first before echoing the results. Rough Example:
function GetVar($var, $username, $mysqli) {
// make the query
$query = $mysqli->query("SELECT ".$var." FROM users WHERE username = '".$username."' LIMIT 1");
$result = $query->fetch_assoc(); // fetch it first
return $result[$var];
}
Then use your function:
echo $user->GetVar('rank', 'Liam', $mysqli);
Important Note: Since you're starting out, kindly check about prepared statements. Don't directly append user input on your query.
if ($result = $mysqli->query($query)) {
while($row = $result->fetch_object()) {
echo row['column_name'];
}
}
$result->close();
where you see 'column_name put the name of the column you want to get the string from.
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