I have a table called cms_settings. I name all the tabels with a prefix cms_ so i created a variable $dbpraefix="cms_"
when i call the entry using "select value from $dbpraefix.settings" command, it failed to proceed.
i also tried defferent version. like "select from '.$dbpraefix.'settings etc. nothing works.
but if i use "select value from cms_settings instead, it works!. how can i fix this. thanks a lot
<?PHP
function getSetting($property){
global $connection;
$dbpraefix= "cms_";
$sql= "SELECT value FROM $dbpraefix.settings WHERE property='$property'";
$ergebnis= mysqli_query($connection, $sql);
$row = mysqli_fetch_row($ergebnis);
return $row[0];
}
?>
Your query fails because in the string "...$dbapraefix.settings..." PHP doesn't realize that you want the . in the middle to be the string concatenation operator instead of a simple dot. As a result the string becomes cms_.settings instead of cms_settings
Change:
"SELECT value FROM $dbpraefix.settings WHERE property='$property'";
To
"SELECT value FROM {$dbpraefix}settings WHERE property='$property'";
You have a dot between the prefix and table name, that's why it won't work.
Try this: " . $dbpraefix . "settings.
The easiest way is to add a new parameter to your function so that when pass into the function it specify the table's name
Eg : $table = "settings";
function getSetting($property,$table){
global $connection;
$table= "cms_".$table;
$sql= "SELECT value FROM $table WHERE property='$property'";
$ergebnis= mysqli_query($connection, $sql);
$row = mysqli_fetch_row($ergebnis);
return $row[0];
}
change your query as below
$sql= "SELECT value FROM ".$dbpraefix."settings WHERE property='$property'";
Related
what is the $row variable in php? and whats the duty of $row variable in php?
Code :-
function getCats() {
global $con;
$get_cats = "select * from categories"; //select from database
$run_cats = mysqli_query($con, $get_cats); //
while($row_cats = mysqli_fetch_array($run_cats)) {
$cat_id = $row_cats['cat_id'];
$cat_title = $row_cats['cat_title'];
echo"<li><a href='#'>$cat_title</a></li>";
}
}
It is just a variable, it can be anything, like $line etc.
In your case, variable $row_cats is being used to fetch data from database.
As per your question, you can use any name of variable.. then is no special name for special variable types, but for standardization of code you have to use variable name with good naming convention.
I noticed in your code, so I suggesting you some points regarding variable naming conventions.
As per your code $get_cats is storing query string you can keep name of that variable $category_query or only $query.
$run_cates in this variable you are storing result reference of category query which is return from mysqli_query(), so meaning full name of that variable is $category_result or only $result.
$row_cats in while loop, mysqli_fetch_array() returns data of one row of result so better name is $category_row or $row only
$row is just a name of a variable.
When you are using mysqli_fetch_array, you can get rows from resource you get from MySQL like
while($row = mysqli_fetch_array($result)) {
/* do_something */
}
I am using $sqlCommand variable name to validate my user access
$sqlCommand = mysqli_query($conn, "SELECT......
then I have another $sqlCommandProductList variable to check for my existing product list.
$sqlCommandProductList = mysqli_query($conn, "SELECT......
and I also have $sqlCommandMostPopular and $sqlCommandLatestProducts....
My question is is it possible to reuse the same variable name $sqlCommand throughout the page instead of creating so many variable name for sql query that only used once?
Of course, you can keep using it like this:
$sqlCommand = mysqli_query($conn, "SELECT name......");
...
// after the using is finished, just unset($sqlCommand)
unset($sqlCommand);
// define $sqlCommand again
$sqlCommand = mysqli_query($conn, "SELECT password......");
But do recommend another Strategy, it can be like this:
extract the ''database access process''(db process) into a independent method
return the info needed from the db process result
give the info a understanable name
like following:
function getName($conn, $sql) {
$sqlCommand = mysqli_query($conn, "SELECT name......");
...
// this is really needed
$name = ...;
return $name;
}
$name = getName($conn, $sql);
Sure is this possible.
As long you are sure you do not loose or overwrite data you still need for furhter processing.
I'm trying to use mysqli_free_result function in my website, to free results after they are used.
But it says:
Warning: mysqli_fetch_assoc(): Couldn't fetch mysqli_result in
It might also be called before I use the result. I'd like to know how to properly use it
<?php
function find_all_subjects(){
global $connection;
$query = "SELECT * ";
$query .= "FROM blog_subject WHERE ID > 2";
$subject_set = mysqli_query($connection, $query);
mysqli_free_result($subject_set);
return $subject_set;
}
?>
I return value, and echo the function to display list of subjects.
Okay, if i have to use mysqli_free_result, after the return $subject_set,
Then i do i know that, the result is freed, if value has already been returned?
Do not use it.
function find_all_subjects($connection){
$query = "SELECT * FROM blog_subject WHERE ID > 2";
return $connection->query($query)->fetch_all();
}
I'm trying to use mysqli_free_result function in my website, to free results after they are used.
No need to do it. PHP will clean everything up automatically.
I am getting a value for return instead of getting my data from my database. The code should be good. Here is my code:
function getConfigurationData($configuration_value, $configuration_name_value) {
$sql = "SELECT '".$configuration_value."' FROM configuration WHERE name = '".$configuration_name_value."'";
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
echo $row[$configuration_value];
}
<? getConfigurationData("value", "configuration_website_name"); ?>
What is wrong with my configuration. I seem not be returning my data and instead I get a value
your query is wrong (quotes around field name)
Try this
$sql = "SELECT ".$configuration_value." FROM configuration WHERE name = '".$configuration_name_value."'";
and a more readable way
$sql = "SELECT $configuration_value FROM configuration WHERE name = '$configuration_name_value'";
I am getting values from mysql data base using get method i am passing survey_id and question_id from the url
like below
http://myserver.com/emrapp/surveyAnswersScreenOne.php?survey_id=1,question_id=1
but it is giving error
my php code is given below for fetching
$query = mysql_query("SELECT * from survey_Answers where survey_Id='".$survey_id."' AND question_Id='".$question_id"' ");
$rows = array();
while($row = mysql_fetch_assoc($query)) {
$rows[] = $row;
}
echo json_encode($rows);
This line is having error,this:
$question_id"' "
should be
$question_id . "'"
You should be separating get variables with & not , in the url.
That's because you should use & instead of , between url parameters
http://myserver.com/emrapp/surveyAnswersScreenOne.php?survey_id=1&question_id=1
$survey_id = mysql_real_escape_string($_GET['survey_id']);
$question_id = mysql_real_escape_string($_GET['question_id']);
GET params are normally separated with & not ,. So your link should look more like this:
http://myserver.com/emrapp/surveyAnswersScreenOne.php?survey_id=1&question_id=1
Also, please note that GET variables are not automatically translated to PHP variables. You need to pull them from $_GET array:
$survey_id = $_GET['survey_id']
You does not pass value comma separated in url.You have use & in url like
http://myserver.com/emrapp/surveyAnswersScreenOne.php?survey_id=1&question_id=1
$query = mysql_query("SELECT * from survey_Answers where survey_Id='".$survey_id."' AND question_Id='".$question_id."' ");
A dot is missing!
URL and QUERY BOTH ARE WRONG
http://myserver.com/emrapp/surveyAnswersScreenOne.php?survey_id=1&question_id=1
AND
$question_id . "' "
$query = mysql_query("SELECT * from survey_Answers where survey_Id='".$survey_id."' AND question_Id='".$question_id"'");
you forgot the point for concatenating the string after $question_id -> this should fix your issue:
$query = mysql_query("SELECT * from survey_Answers where survey_Id='".$survey_id."' AND question_Id='".$question_id."'");
anyway,also consider sanitizing your url-inputs -> http://xkcd.com/327/