My php script is woking outside function but not inside function.
$uid = $_SESSION['user_id'];
function getUserName($conn) {
$sql = "SELECT * FROM `tbl_user` WHERE `user_id` ='".$uid."' ";
$row = $conn->query($sql)->fetch_array();
return $row["user_name"];
}
Unable to get it where I am wrong.
DO NOT use global variable. It's evil.
Pass the user ID as parameter to the function:
function getUserName($conn, $uid) {
$sql = "SELECT * FROM `tbl_user` WHERE `user_id` ='".$uid."' ";
$row = $conn->query($sql)->fetch_array();
return $row["user_name"];
}
echo getUserName($conn, $_SESSION['user_id']);
try this
function getUserName($uid, $conn) {
// code here
}
Best thing you can do is avoid globals and just pass the uid as a parameter:
$uid = $_SESSION['user_id'];
$username = getUserName($conn, $uid);
function getUserName($uid, $conn) {
$sql = "SELECT * FROM `tbl_user` WHERE `user_id` ='".$uid."' ";
$row = $conn->query($sql)->fetch_array();
return $row["user_name"];
}
Read http://php.net/manual/en/language.variables.scope.php to learn about variable scope in PHP
Also read this post about SQL injection
you need to call the function to work code inside funtion
Related
In my code I use $user_id = $_SESSION["USER_ID"]; to get the users id that is valid. If I use echo function in PHP it displays the correct id but when I try to use it in query it says it is undefined.
I am using XAMPP with:
PHP 7.1.27 , 7.2.16 , 7.3.3
Apache 2.4.38
MariaDB 10.1.38
Perl 5.16.3
OpenSSL 1.0.2r (UNIX only)
phpMyAdmin 4.8.5
$user_id = $_SESSION["USER_ID"];
// Funkcija prebere oglase iz baze in vrne polje objektov
function get_oglasi(){
global $conn;
$query = "SELECT * FROM ads WHERE user_id= echo '$user_id'; ";
$res = $conn->query($query);
$oglasi = array();
while($oglas = $res->fetch_object()){
array_push($oglasi, $oglas);
}
return $oglasi;
}
I expect the output of $user_id = 17 and I get error that it is undefined. But if I try <p>Opis: <?php echo $user_id;?></p> I get correct number.
In php you are not able read variable without Global declaring or send to function parameter.
Please set this session user_id like below code
$user_id = $_SESSION["USER_ID"];
function get_oglasi(){
$user_id=$GLOBALS['user_id'];
global $conn;
$query = "SELECT * FROM ads WHERE user_id='$user_id'; ";
$res = $conn->query($query);
$oglasi = array();
while($oglas = $res->fetch_object()){
array_push($oglasi, $oglas);
}
return $oglasi;
}
Hopefully that will help you.
You need to make $user_id as global global $user_id; because You are using in function and it is define outside function.
Or pass a parameter to a function then you will get the id inside the function
function get_oglasi($id)
I have this code which bring a single post from my database depend on id
global $connect;
$id = $_GET['id'];
$sql_query="SELECT * FROM `topics` WHERE id = {$id}";
$result = mysqli_query($connect,$sql_query);
while ($row = mysqli_fetch_assoc($result)) { ....}
How can i make this as function (My try) :
Function get_single_post($id) {
global $connect;
$id = (int)$id;
$sql_query="SELECT * FROM `topics` WHERE id = {$id}";
$result = mysqli_query($connect,$sql_query);
while ($row = mysqli_fetch_assoc($result)) {
$post[] = $row;
}
return $post;
}
to use this function i use this :
get_single_post($_GET['id']);
and when i call something i use :
$post['title']; for title as ex
Remember, a function returns a value, but that value must be assigned to a variable for you to be able to access it.
$post = get_single_post($_GET['id]);
Using the above should now allow you to access the post as you expect.
If your id is primary key than you don't need while loop as it will return only one result
modified function
Function get_single_post($id) {
global $connect;
$id = (int)$id;
$sql_query="SELECT * FROM `topics` WHERE id = {$id}";
$result = mysqli_query($connect,$sql_query);
return mysqli_fetch_assoc($result);
}
I have a mysql connection which is included in a separate file:
require 'settings.php';
and I have a file with all functions, also included:
require 'functions.php';
In the settings there it looks like this:
$db = mysqli_connect("host", "username", "passwort", "database");
if(!$db) {
exit("Error: ".mysqli_connect_error());
}
and a function uses this connection like this:
function includehomepage() {
$data = array();
$query = "SELECT pagecontent FROM `pages` WHERE `id` = `0`";
$query = mysqli_query($db, $query);
$data = mysqli_fetch_assoc($query);
return $data['pagecontent'];
}
But I get an error message like this:
Undefined variable: db in /var/... on line 18
Do you have an answer? The variable have to be defined in the included file.. I am confused. Thanks for your answers!
Variable scope problem. Look at global
function includehomepage() {
global $db;
$data = array();
$query = "SELECT pagecontent FROM `pages` WHERE `id` = '0'";
$query = mysqli_query($db, $query);
$data = mysqli_fetch_assoc($query);
return $data['pagecontent'];
}
$db is a global variable to includehomepage function. If you want to access it, then you have to pass it to the function or declare it as global inside the function.
like
function includehomepage() {
global $db;
$data = array();
$query = "SELECT pagecontent FROM `pages` WHERE `id` = '0'";
$query = mysqli_query($db, $query);
$data = mysqli_fetch_assoc($query);
return $data['pagecontent'];
}
or have it as a parameter in your function and pass it via call.
function includehomepage($db) {
$data = array();
$query = "SELECT pagecontent FROM `pages` WHERE `id` = '0'";
$query = mysqli_query($db, $query);
$data = mysqli_fetch_assoc($query);
return $data['pagecontent'];
}
includehomepage($db);
Sorry about the last post I had. Here's my revision, please help me.
<?php
//connect database
$sql = "SELECT * FROM user where user_id = 8320 AND password = 'admin' ";
$query = pg_query($sql);
var_dump($row = pg_fetch_array($query)); //dumps correctly.
?>
BUT THE PROBLEM IS THIS..when I try to make it as a function LIKE:
function check($user_id, $password)
{
$sql = "SELECT * FROM user where user_id = $user_id AND password = '$password' ";
$query = pg_query($sql);
$row = pg_fetch_array($query);
return $row;
}
AND CALL IT HERE:
var_dump($data = check(8320, 'admin')); DUMPS NULL;
How come it ended up like this?
Its returning NULL because there is an error with your SQL query, and no results are being returned. You should do some error checking in your function, try this version:
function check($user_id, $password)
{
$dbconn = pg_connect("host=localhost dbname=test");
$sql = "SELECT * FROM user where user_id = $1 AND password = $2 ";
$result = pg_query_params($dbconn, $sql, array($user_id,$password));
$row = pg_fetch_array($result);
if (!$row) {
echo pg_last_error($dbconn);
} else {
return $row;
}
}
Try the code below. It should work fine for you.
$data = check(8320, 'admin');
var_dump($data);
Seems like your PostgreSQL resource is missing inside the function. You have two options.
Declare the connection resource inside the function using global.
Establish the connection inside the function.
This is the first option:
$conn = pg_connect('host','user','pass','db');
function check($user_id, $password)
{
global $conn;
$sql = "SELECT * FROM user where user_id = $user_id AND password = '$password' ";
$query = pg_query($conn, $sql);
$row = pg_fetch_array($query);
return $row;
}
And this is the second option:
function check($user_id, $password)
{
$conn = pg_connect('host','user','pass','db');
$sql = "SELECT * FROM user where user_id = $user_id AND password = '$password' ";
$query = pg_query($conn, $sql);
$row = pg_fetch_array($query);
return $row;
}
According to the PHP manual, You may omit connection resource, but it is not recommended, since it can be the cause of hard to find bugs in scripts.
I am writing a script to access a specific detail about the user and I was hoping to make the database query be function.
function connectUser($ip) {
$q = "SELECT * FROM users where ID='$ID'";
$s = mysql_query($q);
$r = mysql_fetch_array($s);
}
But when I try and use it it will not access the row the way I want it to.
$user = '999';
connectUser($user)
echo $r['name'];
But if I put echo $r['name']; in the function it will work.
your function is not returning anything. add return $r['name'] at the end of function.
then echo connectUser($user);
thare are 2 major problems in your code
the function doesn't return anything and you don't assign it's result to a variable.
Your variables doesn't match. $ip doesn't seem the same variable with $ID
so, this one would work
function connectUser($id) {
$q = "SELECT * FROM users where ID=".intval($id);
$s = mysql_query($q);
return mysql_fetch_array($s);
}
$user = '999';
$r = connectUser($user)
echo $r['name'];
That's because the variable $r isn't being returned by the function, so it's never being set outside of the function. Here's what you should have:
function connectUser($ip) {
$q = "SELECT * FROM users where ID='$ip'";
$s = mysql_query($q);
return mysql_fetch_array($s);
}
And then outside have:
$user = '999';
$r = connectUser($user)
echo $r['name'];
You might also want to take a look at this question: prepared statements - are they necessary
This function is not working,
as you did not supplied the database connection into function,
and you did not return anything (PHP will return NULL)
Please understand what is variable scope first,
and the function
A workable example, can be like :-
function connectUser($db, $ip)
{
$q = "SELECT * FROM users where ID='$ID'"; // vulnerable for sql injection
$s = mysql_query($q, $db); // should have error checking
return mysql_fetch_array($s); // value to be returned
}
How to use :-
$db = mysql_connect(...);
$res = connectUser($db, "some value");