I have a basic query in Joomla! and I really, really can't figure out why it doesn't return anything:
$database =& JFactory::getDBO();
$query = "SELECT * FROM my_table";
$database->setQuery($query);
$result = $database->loadObjectList();
var_dump($result);
die();
Query is very, very basic, I know.
It returns $result as null. Thing is, I run this query in a separate .php scrip file (localhost/myscript.php). All other queries in the rest of my website seem to run just fine (including some in other script files like this one).
I've run this query in a terminal and returns what I want. Please, I need some idea :)
If your trying this in a separate php file (localhost/myscipt.php) as you say, you need the proper classes. See this post. The last answer has some details.
However this is not recommended. You should be using module or plugin development all within the framework.
Alternatively, you could use Jumi which allows you to write any code you want and include it as part of a module. Makes life a lot easier.
I think your query is missing a table prefix, you can echo the prefix using $database->getPrefix();
also try changing
$query = "SELECT * FROM my_table";
to
$query = "SELECT * FROM `#__my_table`";
NOTE: Joomla uses a placeholder for the prefix, the “#__” will be replaced with the correct prefix.
Related
I'm in the process of looking at a WordPress plugin (never used WordPress before) and am running into an issue trying to execute a very basic SELECT statement against the database.
Ex:
$sql = "SELECT * FROM wp_posts";
$result = $wpdb->get_results( $sql );
var_dump($result);
echo($result);
This doesn't display anything even though my wp_posts table has a couple hundred rows. Can someone tell me what I'm missing? Thanks.
This may or may not be the issue, but often, prefixes cause trouble and it is not recommended to hardcode tables in.
Secondly, can you output the value of $wpdb? If not, and this is most likely the problem, you don't have access to $wpdb in your scope.
Use global $wpdb if you are executing this within a function.
$sql = "SELECT * FROM $wpdb->posts";
$result = $wpdb->get_results( $sql );
Just a small question for anyone out there that uses smarty. I am trying to pass PHP directly into my code, but when I do, the cached version cuts out the PHP and just prints it directly like so.
<div class="dashboard-card-content">
<?php
$con = mysqli_connect(Nice,Try,Fly,Guy);
$company_id = $_smarty_tpl->tpl_vars['auth']->value['user_id'];
$company_id = mysqli_query($con,"SELECT company_id FROM cscart_users WHERE user_id = $company_id")->fetch_object()->company_id;
$company_id = mysqli_query($con,"SELECT goal FROM cscart_companies WHERE company_id = $company_id")->fetch_object()->goal;
echo "Your current goal is: ".$company_id;
?>
This just prints all of it out on my webpage, so I tried using the following:
{Literal}
{Include_php}
{php}
And I just can't find a way to get my PHP code to go into my TPL how I want it. This is becoming really frustrating and all I want is for my cache files to leave the PHP code alone. Sorry if this is a dumb question but I have been researching this for a while. How do I implement SmartyBC so that I can still use PHP injections. And if using SmartyBC is a bad idea, can someone give me a dumbed down version of how to use a seperate PHP function page to set variables to show in the Template?
Smarty is a template engine for presentation logic only. You cannot put application logic inside a template. It was possible in older versions of Smarty but fortunately not anymore. Just execute those funcions in a php file and pass the result to the template.
And yes, you can use SmartyBC: http://www.smarty.net/docs/en/bc.tpl, but that's supposed to be used for compatibility with existing projects. It's a really bad idea and shouldn't be used for new projects.
Why do you want to use php in Smarty?
Put your logic into a class or function, and pass the data via the controller: Registry::get('view')->assign('smarty_variable', $data), and you are good to go.
You can create PHP function which gets necessary data from database. E.g.
function fn_get_company_goal($user_id)
{
$company_id = db_get_field("SELECT company_id FROM ?:users WHERE user_id = ?i, $user_id");
$goal = db_get_field("SELECT goal FROM ?:companies WHERE company_id = ?i, $company_id");
return $goal;
}
Put it to your addon. Then you can use it in the Smarty template in the following manner:
{$goal = $user_id|fn_get_company_goal}
I'm using the following query to pull records from a database:
$query = "SELECT password, salt, 'jobseeker' as type
FROM ip_jobseekers
WHERE ipJ_username = '$username'
UNION
SELECT password, salt, 'company' as type
FROM ip_companies
WHERE ipC_username = '$username';";
$result = mysql_query($query);
//No Such User
if (mysql_num_rows($result) < 1) {
header('Location: login.php?login=fail'); exit;}
to create a new session:
//login successful
else {$_SESSION['user'] = $username;}
I want to include the type of user in the session so that I can specify what content is displated to the user. Is it possible to make use of the "'company' as type" and "'jobseeker' as type" parts of that query to do this? I've make a few attempts at doing it but I've had no success.
Any help would be greatly appreciated.
Thanks.
Kai.
There is absolutely no difference between "regular" and aliased fields. You can use the latter as well as any other.
to find any error in your query you can run the query like below
$result = mysql_query($query) or trigger_error(mysql_error()." ".$query);
for testing you can use above and you ca redirect to custom error page in production environment.
My personal suggestion is to use PDO. make use of Object oriented Programs to run your code faster and to have standard structure.
First of all, you're using the mysql_ set of functions, which are highly dangerous, and you should move over all your code from ASAP.
Next, you should NOT be stringing up queries like this, because your application will grow into this behemoth of SQL everywhere (in the HTML, in the Views, EVERYWHERE), you should probably be using some kind of class/object system where you wrap around the SQL.
And, you should know about the SQL AND clause (I feel horrible linking w3schools, but, that's all I can find right now).
need some enlightment here, and this is my first post here.
I would like to call and use my previously created mysql view using php... is it possible?
or in another words,
i'm wondering....can we OR how do we call mysql view, that we already created using php? to reduce long query coding
standard generic codes as follow :
$sql= " SELECT shipping.tarrif1, city.city_name
FROM shipping JOIN city
ON shipping.id_city = city.id_city";
$db->QueryArray($sql);
while ($row = $db->Row()) {
echo $row->city_name. " - " . $row->tarrif1 . "<br />\n";
}
now for the MYSQL VIEWS :
$sql= " CREATE VIEW shipsumarry AS SELECT shipping.tarrif1, city.city_name
FROM shipping JOIN city
ON shipping.id_city = city.id_city";
Pure MYSQL command :
query: SELECT * FROM shipsummary
IN PHP :
$sql = i'm badly stuck here...please help
How do we access it using php.
thanks before
Addition 1:
ok... let me rewrite the example :
$sql1= " CREATE VIEW shipsumarry AS SELECT shipping.tarrif1, city.city_name
FROM shipping JOIN city
ON shipping.id_city = city.id_city";
$sql2= "SELECT * FROM shipsummary";
$db->QueryArray($sql2);
$sql2 can not see shipsummary VIEW, coz it's already in a different var
how to utilise and then execute $sql1 ? & $sql2?
The process is the same in PHP - a MySQL view is seen by the client (PHP) as a regular table, so querying it as
mysql_query("SELECT * FROM shipsummary");
// Or for whatever framework you're using:
$db->QueryArray("SELECT * FROM shipsummary");
should work correctly. If it does not work correctly, the MySQL user with which you are accessing the view may have broken permissions. (Seems unlikely though).
UPDATE
After you edited your question, I can see the problem quite clearly.
$sql1= " CREATE VIEW shipsumarry AS SELECT shipping.tarrif1, city.city_name
FROM shipping JOIN city
ON shipping.id_city = city.id_city";
$sql2= "SELECT * FROM shipsummary";
// Here, you need to execute $sql1 before $sql2 is useful.
$db->QueryArray($sql1);
// Now execute $sql2
$db->QueryArray($sql2);
We don't know what database class or framework you are using, but if there is a comparable method to QueryArray() that doesn't return a result set, but just executes a statement, use it to create the view instead.
Now, all that being said...
Unless the definition of the view must change every time this code executes, and unless you have a reason to then DROP VIEW shipsummary at the end of this script's execution each time, it makes far, far, far, far .... more sense to simply create the view in the database, where it will stay forever, rather than to keep re-creating it with PHP. Views, once created, stay created.
Don't think of them as a temporary query time/code saver. Create the views you will need ONCE in your database (using PHPMyAdmin or mysql CLI, or however you created your tables), and access them with PHP.
Why not just send that
SELECT * FROM shipsummary
To mysql query, it should work, unless i'm not understanding your question...
I have the website written in PHP (Kohana) and MySQL. I want to create the installer which would run all the environment tests and, what is the most important (and the most misterious) for me, will deploy the database. What is more, I would like to choose the prefix of the tables in the database and specify the username and password for the admin. Would you please give my some hints how to do it? Or some link to some tutorial? I was trying to google that but when I searched for terms like "PHP website installer" I have found only how to install PHP.
The most important for me is the process of deploying database with user-defined tables prefix. How should I store the database structure in the file. Should I name the tables using some keyword for the prefix, for example:
%%prefix%%_tableName
or
__prefix__tableName
And what then? Change all the keywords using regular expresions? Is it correct way or is it any better?
A simple way would be to store the SQL queries in PHP files and have PHP inject the prefix into the SQL and return the string.
Like, if you had a PHP file like this for each of your CREATE TABLE queries:
<?php
/** get_myTable.php **/
return <<<SQL
CREATE TABLE `{$prefix}myTable` ( ... )
SQL;
?>
You could do this in your main code:
<?php
$prefix = 'dbprefix_';
$create_queries = array();
$create_queries[] = include('get_myTable.php');
$create_queries[] = include('get_otherTable.php');
foreach($create_queries as $_query) {
mysql_query($_query) or trigger_error(mysql_error(), E_USER_WARNING);
}
?>
Wordpress has its famous '5-minute install'. It's a great benchmark for simplicity, usability, and power and it does most, if not all, of the things you outlined in your question.