Why is php not able to declare a function more than one time? I am using while loop with mySQL that contains list of IDs and I am trying to parse the ID to this function. But I get Fatal error: Cannot redeclare GetValue() (previously declared.... How do I fix this?
<?php
session_start();
ob_start();
$id = "";
$db = new PDO("mysql:host=localhost; dbname="DB";charset=utf8",'uid','');
$sqlID = "SELECT ID FROM Zip";
$stmtID = $db->query($sqlID);
$pdfArray = array();
while($dataID = $stmtID->fetch(PDO::FETCH_OBJ)){
$id = $dataID->ID;
$sql = "SELECT * FROM Datas WHERE ID = '$id'";
$stmt = $db->query($sql);
GetValue($id);
}
function GetValue($x){
//
}
?>
instead of:
GetValue($x){
//
}
try:
function GetValue($x){
//
}
Related
So I have my code
function GetApi($connection,$UserId){
global $Apicall;
$Apicall = array();
$Apiidquery = mysqli_query($connection, "SELECT ID FROM ` Characterapi` WHERE UserId = '$UserId'");
while($results = mysqli_fetch_assoc($Apiidquery)){
$Apicall[] = $results['ID'];
}
}
The output of this function if I call
$Apicall[0] = 3
$Apicall[1] = 11
and this is the information I want. But now I want to use a function like
function Keyquery($Apicall,$connection ){
global $keyidcall, $keyid ,$Vcode;
$Keyidquery = array();
$Keyidquery = mysqli_query($connection, "SELECT keyid, Vcode FROM `Characterapi` WHERE ID = '$Apicall'");
$results = mysqli_fetch_object($Keyidquery);
$keyid = $results->keyid;
$Vcode = $results->Vcode;
}
This code does run if i set $Apicall ="3"; The issue im having is that I want the first function to get All the IDs associated with $userId in my data base then for each Id run the second function to to get the two specific pieces of information from that query.
In response to the comment below, this is the solution which I would use. However you should be wary of using this method as it does not parameterize the values, and as such not sanitized.
<?php
function Keyquery($Apicall,$connection ){
global $keyidcall, $keyid ,$Vcode;
$string = "ID IN('";
$string.= implode("','", $Apicall);
$string.="')";
$Keyidquery = mysqli_query($connection, "SELECT keyid, Vcode FROM `Characterapi` WHERE ".$string.";");
$results = mysqli_fetch_object($Keyidquery);
$keyid = $results->keyid;
$Vcode = $results->Vcode;
}
?>
Im trying to delete a file from folder while deleting data from database. I have used a class.
I get the following error:
Call to undefined function getCodeName()
//This function is to get the file name from the database
public static function getCodeName($product_id) {
$db = Database::getDB();
$query = "SELECT * FROM products WHERE productID = '$product_id'";
$result = $db ->query($query);
$row = $result->fetch();
$product = new Product ($row['productCode']);
return $product;
}
//Delete file from folder and other data from database
public static function deleteProduct($product_id) {
$fileName = getCodeName($product_id);
$dir = "../images/";
unlink($dir.DIRECTORY_SEPARATOR.$fileName);
$db = Database::getDB();
$query = "DELETE FROM products
WHERE productID = '$product_id'";
$row_count = $db->exec($query);
return $row_count;
}
Use php code
unlink() function set full pat for delete file
I have a database.php file which stores the database connection info like this:
<?php
// Database connectivity stuff
$host = "localhost"; // Hostname for the database. Usually localhost
$username = "root"; // Username used to connect to the database
$password = "root"; // Password for the username used to connect to the database
$database = "blog"; // The database used
// Connect to the database using mysqli_connect
$connection = mysqli_connect($host, $username, $password, $database);
// Check the connection for errors
if (mysqli_connect_errno($connection)) {
// Stop the whole page from loading if errors occur
die("<br />Could not connect to the database. Please check the settings and try again.") . mysqli_connect_error() . mysqli_connect_errno();
}
?>
And also a functions.php file that has the following:
<?php
// Functions file for the system
function show_posts($user_id) {
$posts = array();
$sql = "SELECT body, stamp from posts where user_id = '$user_id' order by stamp desc";
$result = mysqli_query($connection, $sql);
}
function show_users() {
$users = array();
$sql = "SELECT id, username FROM users WHERE status = 'active' ORDER BY username";
$result = mysqli_query($connection, $sql);
while ($data = mysqli_fetch_array($result)) {
$users[$data->id] = $data->username;
}
return $users;
}
function following($user_id) {
$users = array();
$sql = "SELECT DISTINCT user_id FROM following WHERE follower_id = $user_id";
$result = mysqli_query($connection, $sql);
while ($data = mysqli_fetch_assoc($result)) {
array_push($users, $data->user_id);
}
return $users;
}
?>
Both files are inside an /includes folder. I now have a users.php files in which I want to display a list of users. Here's my code that tries to do that:
<?php
$users = show_users();
foreach ($users as $key => $value) {
echo $key . " " . $value;
}
?>
The problem I have is this:
Notice: Undefined variable: connection in
/Applications/MAMP/htdocs/blog/includes/functions.php on line 13
Warning: mysqli_query() expects parameter 1 to be mysqli, null given
in /Applications/MAMP/htdocs/blog/includes/functions.php on line 13
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result,
null given in /Applications/MAMP/htdocs/blog/includes/functions.php on
line 15
The users.php file has require('includes/functions.php') and require('includes/database.php'). But somehow the values are not passed?. What am I doing wrong? Please help me out. I hope this makes sense. The problem with the undefined variable occurs for each function of the 3.
Either pass the $connection variable to your functions or make it global.
E.g:
function show_posts($connection, $user_id) {
$posts = array();
$sql = "SELECT body, stamp from posts where user_id = '$user_id' order by stamp desc";
$result = mysqli_query($connection, $sql);
}
OR
function show_posts($user_id) {
global $connection;
$posts = array();
$sql = "SELECT body, stamp from posts where user_id = '$user_id' order by stamp desc";
$result = mysqli_query($connection, $sql);
}
Try $GLOBALS['connection'] instead of $connection in your function. See more here
Am learning & executing php by working on Joomla project
How to Improve this code & resolve the PHP Notices - Any suggestions - solutions - well appreciated !!
Notice: Undefined variable: cond in*/home/mygames/public_html/components/com_toys/models/category.php on line 140
(which is $sql line)*
function loadSubCat($id,$Carmodel,$minprice,$maxprice){
$mainframe =& JFactory::getApplication();
$option = JRequest::getCmd('option');
$database =& JFactory::getDBO();
global $Itemid;
if($Carmodel!="")
$cond=" and prod_id='$Carmodel' ";
$sql = "Select * from #__toycar_products Where prod_cat_id='".$id."' $cond and prod_status='1' and prod_id in (select v_prod_id from #__toycar_variants) Order By prod_sorder";
Notice: Trying to get property of non-object in /home/truecar7/public_html/components/com_toys/models/category.php on line 200
Line 200 is return $row->id;
function getItemIdByName($Name){
$mainframe =& JFactory::getApplication();
$option = JRequest::getCmd('option');
$database =& JFactory::getDBO();
$sql = "Select id from #__menu Where name = '".$Name."'";
$database->setQuery($sql);
$row = $database->loadObject();
return $row->id;
}
Edit
Hello Lodder & Elin, it works but like this, else it's showing undefined variable notice for row on return $row line.
function getItemIdByName($Name){
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->select('*')
->from('#__menu')
->where('id = ' . $db->quote($Name));
$db->setQuery($query);
$rows = $db->loadObjectList();
foreach ($rows as $row){
$row = $row->msg;
}
$row='';
return $row;
}
Try using the following. I have made some changes to your function and used Joomla 2.5 coding standards for the database query.
$Name = "XXXXXXXXX"; //define the name variable
function getItemIdByName($Name){
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->select('*')
->from('#__menu')
->where('id = ' . $db->quote($Name));
$db->setQuery($query);
$rows = $db->loadObjectList();
foreach ($rows as $row){
$row = $row->msg;
}
return $row;
}
echo getItemIdByName($Name); //echo the result of the function
For your Undefined Notice, You have to modify your codes like this
$cond = '';
if($Carmodel!="") {
$cond = " and prod_id='$Carmodel' ";
}
For Trying to get property of non-object Notice : I think $row is empty that is why throws notice.Check $row
var_dump($row);
Problem :
$database->loadObject(); // This line
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.