Wordpress - How to write simple query on wordpress ? - mysql

I am a newbie and currently learning wordpress , i am struggling to write a query in wordpress. I have written it something like this but seesm like its coming null.
$querystr = "SELECT *
FROM $wpdb->seeker_saved_mark_job ";
$movie_names = $wpdb->get_results($querystr);
var_dump($movies_names);
The seeker_saved_mark_job is placed in meta table. I want to get all the results from this table, and they do exist but Its coming null

Try below query
global $wpdb;
$querystr = "SELECT * FROM ".$wpdb->base_prefix."postmeta WHERE `meta_key`='seeker_saved_mark_job'";
$movie_names = $wpdb->get_results($querystr);
var_dump($movies_names);

Related

Get content from another DB in wordpress

I have found out how to make a page for WordPress, where I can write PHP code into.
However, I can not figure out how to retrieve content from another database.
I have found the connect code:
$wpdb2 = new wpdb($dbname, $dbpass, $dbuname, $dbhost);
but I cannot figure out how to retrieve content from the database.
Can someone give an example?
If, for example, have this query
SELECT number
FROM qr_statistic
WHERE date = '$today'
please see this:
wpdb::get_results( string $query = null, string $output = OBJECT )
now you can write your query:
$result = $wpdb->get_results("SELECT number FROM $wpdb->qr_statistic WHERE WHERE date = '$today'");
also see this link

Active Record in CodeIgniter 2

I have this code:
$places = $this->db
->select('*')
->from('places')
->where('postal_code',$search)
->limit($limit, $start)
->get()->result();
I want to know if is there something wrong, and which is the way to get a query with a simple where inside the query.
Like this..
$places = $this->db->get_where('places',array('postal_code'=>$search),$limit,$start)->result();//outputs result in object format
$places = $this->db->get_where('places',array('postal_code'=>$search),$limit,$start)->result_array();//outputs result in array format
See more docs https://www.codeigniter.com/userguide3/database/query_builder.html
At the end I do it in other way, as I can see is there some issues to do a where and limit sentence in CodeIgniter in the same query.
So I do it directly in SQL
$query = "SELECT * FROM places WHERE locality = '".$search."' LIMIT ".$start.",".$limit."";
$places = $this->db->query($query)->result();
Not nice, but it works.

In CakePHP 3.x how can I count the number of rows in a MySql table?

I have a MYSQL table called 'devices'. I've successfully done the bin/cake bake all. In fact I have the auto-built DevicesController.php fully working. But I can't figure out how to count the rows in a table. I've tried:
$conn = ConnectionManager::get('default');
$numRows = $conn->execute('select count(*) from devices');
and
$this->DeviceSetups = TableRegistry::get('Devices');
$numRows = $this->Devices->query('select count(*) from devices'); // both like this
$numRows = $this->Devices->query('select count(*) from devices')->execute(); // and like this
and
$this->DeviceSetups = TableRegistry::get('Devices');
$numRows = $this->Devices->find('count');
Going thru mysql_query() isn't really a good idea because I have all the access info already setup in app.php for CakePHP to use. I tried something else using AnyModel that didn't work.
The former 2 attempts return a Cake\Database\Statement\MysqlStatement not an integer with the number of rows in the table. I've consulted this answer and this answer and read the CakePHP docs. Nothing seems to tell me how to count up a table nor how to execute a raw My SQL command string and then access the result.
TableRegistry::get('DeviceSetups')->find()->count();
See http://book.cakephp.org/3.0/en/orm/retrieving-data-and-resultsets.html#getting-a-count-of-results.
If you want to count in table then it should be like below.
$count = $this->find()->count();
echo $count;

Find text with like

I want to find text in a MySql database
$result = mysqli_query($con,"SELECT * FROM blog
WHERE text_post LIKE = 't%';
")
or die(mysqli_error());
while($row = mysqli_fetch_array($result)) {
echo $row['titol_post'] . "<br>";
}
It gives me the error:
mysqli_error() expects exactly 1 parameter, 0 given...
If I substitute
WHERE text_post LIKE = 't%';
by
WHERE text_post = 'test';
It works well. I do not undestand. Why the LIKE does not work?
Here like after = is probelm please replace
with
WHERE text_post LIKE = 't%';
to
WHERE text_post LIKE 't%';
mysqli_error(), like all other mysqli_xx() functions, requires that you pass the connection variable to it, so that it knows which DB connection you want to know the last error for.
...
or die(mysqli_error($con));
...
Once you've done this, you will get a more useful error message that will help you diagnose the problem with your SQL code.
When you do get the real SQL error message, you will find that the problem is with the = sign.
The reason for this is that LIKE is an operator in the same way as =. You can only use one operator here, so if you're using LIKE, then you don't need = as well.
Your SQL code would therefore change to look like this:
SELECT * FROM blog
WHERE text_post LIKE 't%
I just changed like = to like and it works
SELECT * FROM blog WHERE text_post LIKE 't%'"
Try to pass conn in the error function:
or die(mysqli_error($con));
The MySQL syntax says:
string mysqli_error(mysqli link);
Returns the last error message for the most recent MySQLi function
call that can succeed or fail.
Also you need to remove the = sign after the LIKE keyword
$result = mysqli_query($con,"SELECT * FROM blog
WHERE text_post LIKE 't%';
")
or die(mysqli_error($con));
This is the right way to write sql query in mysql. Please replace your query with this.
$result = mysqli_query($con,"SELECT * FROM blog
WHERE text_post LIKE 't%';
")

Propel ORM - Custom where clause

I'm trying to match md5(ID) to an id.
SELECT *
FROM `user` u
WHERE
MD5(`user_id`) = '66f041e16a60928b05a7e228a89c3799'
this is ID = 58
I tried something like this. I know I'm close I just don't know what I'm missing
$criteria = new Criteria();
$criteria->addAnd('md5('.User::USER_ID.')', $_REQUEST['fs'], Criteria::CUSTOM);
$user = UserPeer::doSelectOne($criteria);
Any ideas?
First of all, directly using Criteria objects is deprecated not recommended. You should use Active Query classes.
Using these classes, you will be able to write stuff like this :
UserQuery::create()
->where('md5(User.Password) = ?', $_REQUEST['fs'], PDO::PARAM_STR)
->findOne();
You'll notice that I use the PhpName both of the table and the column in the query.
EDIT : For raw conditions, the parameter type has to be specified. You'll find more information on this issue.
After lenghty T&E process I managed to get it done like this
$c = new Criteria();
$c->add(UserPeer::USER_ID, "md5(user.user_id) = \"".$_REQUEST['fs']."\"", Criteria::CUSTOM); // risk of SQL injection!!
$saved_search = UserPeer::doSelectOne($c);
For some reason PropelORM though that $_REQUEST['fs'] was name of the table rather than the value. \"" solved the problem.