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
Related
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');
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:
Fatal error: Call to undefined function mysqli_result()
(2 answers)
Closed 8 years ago.
There is a table 'hit_count' containing only one column 'count' in database in which I am trying to count the hits by user. problem is whenever I am running this code it shows an error message "Fatal error: Call to undefined function mysqli_result()". Please help!!
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
require 'connect.inc.php';
function update_count()
{
global $link;
$query = "SELECT `count` FROM `hit_count`";
if($query_run = mysqli_query($link,$query) || die(mysqli_error($link)))
{
echo 'checking control';
$count = mysqli_result($query_run,0,'count');
echo $count;
}
else
{
echo 'Problem Occured!!';
}
}
update_count();
?>
There isn't a mysqli_result function (not that you can't define it, but what for?). There is a mysqli_result Class, and it has static methods that you can call, of course. But I believe you're doing this wrong.
The correct way would be something like
$count=array();
if($query_run = mysqli_query($link,$query) || die(mysqli_error($link))) {
while ($row = $query_run->fetch_array(MYSQLI_ASSOC)) {
$count[]=$row["count"];
}
}
remember that the outcom of mysqli_query will be an iterable object. Don't expect it to return an aggregate value by default.
PD: if you name your columns with reserved words like count, you're gonna have a bad time.
This question already has answers here:
Query doesn't work inside a function
(5 answers)
Closed 8 years ago.
I have this code inside a function:
function verifica($soph,$ano)
{
$sql_ver = "SELECT * FROM ativEns WHERE Sophia_ID = ".$soph." and AnoLetivo =".$ano."";
$lista_ver = sqlsrv_query($ligarBD,$sql_ver);
$result_ver = sqlsrv_fetch_array($lista_ver);
if (!$result_ver)
{
echo "Nothing.";
}
else
{
echo $result_ver["desc"];
}
}
To call the function:
$temp_ver = substr($anoatual,0,4) . substr($anoatual,5,4);
verifica($sophia,$temp_ver);
It works perfectly if not inside the function, but when I put it in the function it just echoes de "Nothing"
$sophia is undefined inside your function, so
$sql_ver = "SELECT * FROM ativEns WHERE Sophia_ID = ".$sophia." and AnoLetivo =".$temp_ver."";
is producing a query that looks like
SELECT ... WHERE Sophia_ID = and
^-----due to undefined variable.
Perhaps it should be just $soph instead, to match the arguments in the function definition?
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){