I have a php file called choose.php where inside i echo some HTML i.e. a select element.
I am using PDO to populate the select element from a mysql database.
the code i have written works perfectly but when i put it into a function and try to call it i get an error telling me that i cannot declare said method again.
the code is thus:
echo '<select>';
$sql = "SELECT name FROM people";
$res = $conn->prepare($sql );
$res ->execute();
while ( $row = $res ->fetch() )
{
echo '<option value = "' . $row['name '] . '">' . $row['name '] . '</option>';
}
echo '</select>';
in other words the function would look like this:
function getnames()
{
$sql = "SELECT name FROM people";
$res = $conn->prepare($sql );
$res ->execute();
while ( $row = $res ->fetch() )
{
echo '<option value = "' . $row['name '] . '">' . $row['name '] . '</option>';
}
}
Why cant i call the method inside the echoed select element?
echo '<select>';
getnames();
echo '</select>';
Also how would i accomplish this by placing the method in another php file to keep it tidy?
Why cant i call the method inside the echoed select element?
Because the method body references $conn, which is supposedly a global variable and not in scope inside the method body. You can verify that this is the problem (and "fix" it) with
function getnames()
{
global $conn;
// the rest as before
}
Now, although this will make the problem go away, what you propose here is not a good way to organize things. There are several issues:
getnames uses a global variable ("invisible argument") -- note that you would not have had reason to ask this question if this had been corrected!
The name of the method is misleading -- it doesn't "get" something, it prints HTML.
The method is unusable for anything else other than its specific purpose -- if you wanted to do something else with the names (e.g. print a table) you would have to write another method.
You are interleaving straight HTML output (the <select> tag) with business logic (querying the database). It's better to do all the business logic up front (keep the results you need in variables) and then do the HTML all in one go.
All of the above are serious deficiencies of the chosen approach and none of them would be present in a well built application. I suggest that instead of making the problem go away you would be better served by refactoring the code to address these, and the problem will fix itself on the way.
Code Review would be an excellent place to ask a question along the lines of "I have this code and this recommendation -- how would I implement it properly?" if you need extra help.
You are trying to access $conn variable which is not available in your function scope.
To access $conn variable inside your function use global, like below:
global $conn;
How are you loading the file in which the getnames function is defined? Try using require_once and making sure it's not being included more than once - already defined means it's be defined and the file is being called again, hence trying to define it again
If you're calling that same code multiple times on your page it will get very heavy to load. I would recommend just running it at the top of the page and putting the data to a variable, then echoing that variable at each location that you need it
So your code in the top of your page
$sql = "SELECT name FROM people";
$res = $conn->prepare($sql );
$res ->execute();
$outputData = '';
while ( $row = $res ->fetch() ){
$outputData .= '<option value = "' . $row['name '] . '">' . $row['name '] . '</option>';
}
Then
echo '<select>'.$outputData.'</select>';
Related
New to OOP so I am trying to figure out best practice. This code is based off an existing script I am adding to.
Most of the threads with this question tell the poster to code as so:
function ($arg1, $arg2){
//some code
}
and call:
function($a1, $a2);
I have an OOP-based function (that works) but it doesn't quite look right and when I try to call it as the suggested method, I get:
Array to string conversion .... on line .. Array
Here's my (working) function that gathers the output:
public function getMail($type, $id = 0) {
$query = $this->db->query("SELECT * FROM km_mail WHERE id = '" . (int)$id . "' AND `type` = '" . $this->db->escape($type) . "'");
foreach ($query->rows as $result) {
$mail_data[$result['title']] = $result['value'];
}
return $mail_data;
}
This is the working (but ugly) part - this returns the database column requested (but looks wrong?):
$this->model_setting_mail->getMail('order')['update_link'];
When I try to request the column like so, the array to string conversion error occurs:
$this->model_setting_mail->getMail('order','update_link');
In my example, order = $type, update_link = $result['value'] and $id = 0 is default, unless an $id is passed.
The first example you show is a shorthand way of selecting an array element from value returned by a function.
$this->model_setting_mail->getMail('order')['update_link'];
Is the same as:
$result = $this->model_setting_mail->getMail('order');
print $result['update_link'];
Second example is passing two values to a function.
They are completely different.
I'm trying to migrate an application written in PHP 5.2, but I'm having trouble creating the correct syntax for custom functions. This works perfectly in the old syntax, but I get a fatal error with this.
Essentially, I'd like to create a function to make it easy to get an email address from a database table that's associated with a unique id. I wrote the code below based on what works in PHP 5.2.
function getemail($id) {
$email_query = $con->query("SELECT email FROM admin WHERE id='$id'");
$rs = $email_query->fetch(PDO::FETCH_ASSOC);
return $rs;
}
Then I could use something like below to call this function.
foreach($con->query('SELECT * FROM admin') as $row) {
echo $row['id'] . ' ' . $row['name'] . ' ' . getemail($row['id']) . '<br>';
}
Any direction to help with this issue is greatly appreciated.
Ok, I have a tool for such a transition, which combines the simplicity of old mysql functions with safety of PDO (if used properly) - Simple yet efficient PDO wrapper
Having set it up, all you need is a code like this:
function getemail($id) {
return DB::run("SELECT email FROM admin WHERE id=?", [$id])->fetchColumn();
}
called like this
echo getemail(1); // admin#example.com
I'm trying to create a drop down list that populates a <select> with options pulled from a DISTINCT argument. Code looks like this:
function cityData() {
$db =& JFactory::getDBO();
$query = "SELECT DISTINCT MSTCITY FROM " . $db->nameQuote('#__mls') . " ORDER BY MSTCITY;";
$db->setQuery($query);
$tbl = $db->loadObjectList();
echo $tbl;
}
Now, I have two views: one is RAW for an AJAX call and the other is the default view. I figured the simplest way would be to just use the default view and do it in PHP, since the default view wasn't really being used for much anyway. So I added a function:
function dropList($tpl = null){
$model = &$this->getModel();
$array = $model->cityData();
$this->assignRef('array', $array );
parent::display($tpl);
}
And then a call in the page
<?php
$thing = $this->array;
echo $thing;
?>
Nothing is being displayed for the echo $thing;. In the past, when I used PHP to build content instead of AJAX, this worked fine. I don't know if it's using loadObjectList() that's not giving me anything or what. I know the mySQL query works, as it's be tested in the cmd and I get the result I expect.
In order to debug and see the array values you need to use print_r.
In your case edit the default.php file to
<?php
$thing = $this->array;
print_r($thing);
?>
You can also use var_dump
I would like to create a simple select drop down, to be populated by a table in my MYSQL database.
Here is my code:
$q = 'SELECT * FROM Shipmethods';
$result = mysqli_query($connection, $q);
echo '<select name="shipmethod">';
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC));
{
echo '<option value="' . $row['shipmethodid'] . '">' . $row['shipmethoddesc'] . '</option>';
}
echo '</select>';
mysqli_free_result($result); // free up the results
}
There is no connection error, no typos in table name or column names. The HTML output shows that a select option exists, but it is empty. I would like to use mysqli for this.
There seem to be countless topics already on this subject, but as far as I can see, my code matches the correct answers of those already answered questions.
Thanks in advance.
Remove ; from this line, rest code seems fine to me, let me know if it works or not --
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC));
To
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
First off, based on the evidence we have, the most likely issue, other than an empty result set, is the issue that swapnesh identified, which is clearly a logic error. I'm providing this illustration, which simulates the problem to make it clear that it is NOT a syntax error to do what was accidently done here. The misplaced semicolon in essence causes the following block to be entered only after the while loop has been exhausted.
Consider this example:
<?php
$r = array(1,2,3,4);
function fetchit(&$r) {
return array_pop($r);
}
while ($val = fetchit($r))
{
echo $val;
}
This would be the intended use. Now what happens if the script has this code instead?
<?php
$r = array(1,2,3,4);
function fetchit(&$r) {
return array_pop($r);
}
while ($val = fetchit($r));
{
echo $val;
}
Trying these you will see that both scripts run without error. Although unnecessary, php does not care if you randomly include an uneeded block somewhere in your script { }, and it also does not care if an if, while, for etc, is followed by a block. It's in fact common practice for people to shorten up code with:
if (//somecondition)
statement;
Many people frown on that practice but it's legal and programmers use it all the time.
I'm going to write PHP script, that uses table prefix for MySQL tables. Should I write all of requests like for example
$db_query='select * from '.$tbl_prefix.'sometable;';
or it's possible to set some variable, that will add this prefix to all queries?
For example, I'm performing request
$db_query='select * from sometable;';
and MAGIC adds prefix to table itself, so for MySQL query will look like
select * from pref_sometable;
Your first sample is the way to go; you'll need to prepend the prefix each time. It may make it easier to make this into a function though (incase you ever need to update anything, it would be far better to only have to update one location instead of multiple):
function _table($table) {
global $tbl_prefix;
return $tbl_prefix . $table;
}
$db_query = 'SELECT * FROM ' . _table('sometable') . ';';
There's no MySQL command to do that, you must implement it in your code just like you do at first. So:
$db_query='select * from '.$tbl_prefix.'sometable;';
Is fine.
If you don't want to add a prefix on each query, you can create an object like this:
class database {
function select($qry,$database) {
$db_query="select " . $qry . " from myprefix_" . $database;
// $res = query($db_query);
return $res;
}
}
$db_query=database->select("*","sometable");
// echo $db_query;
$db_query=database->select("playernick","sometable");
// echo $db_query;
That's not 100% PHP, it's just and idea so you can get it.