How can i read datas on sqlite3 with php variables? - pdo

$result = $vt->prepare('SELECT * FROM streamdetails WHERE idFile ='.$idFile.' AND iStreamType =0');
$result->execute();
foreach($result as $row)
echo "Çözünürlük: ".$row['iVideoWidth'] . " x " .$row['iVideoHeight'] . "<br>";
}
What is wrong?
$idFile variable type is text but idFile is integer on streamdetails.

Related

How to Direct access through the database query to product name , product price and permalink and product photo columns

How do I get Direct access through the database query to product name , product price ,permalink , and image url of product in WordPress WooCommerce ?!
what's the sql query ?
global $wpdb;
$metas = array(
'_thumbnail_id', '_regular_price'
);
foreach ($metas as $i=>$meta_key) {
$meta_fields[] = 'm' . $i . '.meta_value as ' . $meta_key;
$meta_joins[] = ' left join ' . $wpdb->postmeta . ' as m' . $i . ' on m' . $i . '.post_id=' . $wpdb->posts . '.ID and m' . $i . '.meta_key="' . $meta_key . '"';
}
$request = "SELECT ID, post_title, " . join(',', $meta_fields) . " FROM $wpdb->posts ";
$request .= join(' ', $meta_joins);
$request .= " WHERE post_status='publish' AND post_type='product' AND $wpdb->posts.post_title like '%sample%'";
$product_details = $wpdb->get_row($request, OBJECT);
if(!empty($product_details)){
$qry = "SELECT guid FROM $wpdb->posts WHERE ID=" . $product_details->_thumbnail_id;
$uri_obj = $wpdb->get_row($qry, OBJECT);
$product_details->_thumbnail_id = $uri_obj->guid;
}
echo '<pre>';print_r($product_details);exit;

SQL being a jerk again

I have the script I use that displays things from a table in my database, but it keeps giving me this error:
Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\raynerblogger1\posts.php on line 21
someone please help
$sql = "SELECT * FROM blogs;";
$result = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_assoc($result)) {
echo "<div id='message'>";
echo $row['author']; echo(':') . "<br>";
echo $row['post'] . "</div>" . "<br>" . "<br>" . "<br>";
}
Try this, I edited line 1 and 6:
$sql = "SELECT * FROM blogs";
$result = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_assoc($result)) {
echo "<div id='message'>";
echo $row['author'].":". "<br>";
echo $row['post'] . "</div>" . "<br>" . "<br>" . "<br>";
}

how to get wordpress post with by category with images

I have installed wordpress and opencart in same database. Trying to get wordpress posts table inside opencart module. got the mysql query to fetch all information except image. I dont why images are different from the post in loop of result. Kindly guide, following is the code.
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "tablename";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {die("Connection failed: " . $conn->connect_error);}
$sql = "SELECT * FROM wp_posts WHERE post_type = 'attachment' ORDER BY ID DESC LIMIT 3";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo '<div class="col-sm-4">';
echo "Post ID: " . $row["ID"] . " / ";
echo "Post Title: " . $row["post_title"] . " / ";
echo "Post Title: " . $row["post_date"] . " / ";
echo '<img src="' . $row['guid'] . '" class="img-responsive">';
echo '</div>';
}
} else {
echo "0 results";
}
Run your query in phpMyAdmin and check if result you got is the same as you want (you get those pictures that you want).
Then I advise you to set the checkpoint inside the loop and look at the data. Debugging is a very powerful thing in finding errors, it's time to start using it

Create multiple variable from foreach loop

I have this script that gets weather data from a API.
But I need to have the temperatures of every day in seperate strings.
$day1Max, $day1Min, $day2Max, $day2Min, etc...
Is it possible to get these variables from a foreach loop?
<?php
$conditions_week = $forecast->getForecastWeek($latitude, $longitude);
echo "Forecast this week:\n";
foreach($conditions_week as $conditions) {
?>
<tr>
<th><?php echo $conditions->getTime('Y-m-d') . ': ' . $conditions->getMaxTemperature() . "\n"; ?></th>
<th><?php echo $conditions->getTime('Y-m-d') . ': ' . $conditions->getMinTemperature() . "\n"; ?></th>
</tr>
<?php
}
?>
This is the output:
Here is your solution :
<?php
class Conditions{
function __construct($min,$max){
$this->max = $max;
$this->min = $min;
}
function getMaxTemperature(){
return $this->max;
}
function getMinTemperature(){
return $this->min;
}
}
$conditions_week = array(new Conditions(5,10),new Conditions(-5,3));
echo "Forecast this week:\n";
foreach($conditions_week as $k=>$conditions) {
$varMin = "day".($k+1)."min";
$varMax = "day".($k+1)."max";
$$varMin = $conditions->getMinTemperature();
$$varMax = $conditions->getMaxTemperature();
?>
<tr>
<th><?php echo "Max : ". $conditions->getMaxTemperature() . "\n"; ?></th>
<th><?php echo "Min : ". $conditions->getMinTemperature() . "\n"; ?></th>
</tr>
<?php
}
echo "day1min : " . $day1min;
echo '<br>';
echo "day1max : " . $day1max;
echo "<hr>";
echo "day2min : " . $day2min;
echo '<br>';
echo "day2max : " . $day2max;
You to generate your php variables $day1min, $day1max... inside the foreach and then you can use it outside the loop.
Here's the full example : https://3v4l.org/vX5bW

How can I convert this to use PDO?

I would like to use PDO for selecting (searching) a database.
The search 'form' has MULTIPLE fields that can be used.. 1 or many can be filled in to help refine the search. (or there can be many o them left blank/empty)
here is what I have been using (locally):
//localhost details
$db_username="root"; //database user name
$db_password="";//database password
$db_database="test"; //database name
$db_host="localhost";
mysql_connect($db_host,$db_username,$db_password);
#mysql_select_db($db_database) or die("Unable to connect to database.");
if(isset($_POST['submit'])) {
// define the list of fields
$fields = array('first', 'trialdate', 'wcity', 'wstate', 'plantif');
$conditions = array();
//loop through the defined fields
foreach($fields as $field){
// if the field is set and not empty
if(isset($_POST[$field]) && $_POST[$field] != '') {
// create a new condition while escaping the value inputed by the user (SQL Injection)
$conditions[] = "`$field` LIKE '%" . mysql_real_escape_string($_POST[$field]) . "%'";
}
}
//build the query
$query = "SELECT * FROM myTable ";
// if there are conditions defined
if(count($conditions) > 0) {
// append the conditions
$query .= "WHERE " . implode (' OR ', $conditions); // you can change to 'OR', but I suggest to apply the filters cumulative
}
$result = mysql_query($query);
if(isset($_POST['submit'])) {
while($row = mysql_fetch_array($result)) {
echo $row['first'] . "<br />"; //individual value
//build panels that displays everything from row..etc
}
}
}
this has been working fine... but I'd like convert to using the PDO approach.
I gave it a few tries...but am missing something here..
heres what I've tried so far..
//localhost details
$db_username="root"; //database user name
$db_password="";//database password
$db_database="test"; //database name
$db_host="localhost";
//PDO DB connection
$conn = new PDO('mysql:host='.$db_host.'dbname='.$db_database.'charset=utf8', $db_username, $db_password);
if(isset($_POST['submit'])) {
$stmt = $conn->prepare('SELECT * FROM myTable WHERE first LIKE :first OR trialdate LIKE :trialdate OR wcity LIKE :wcity OR wstate LIKE :wstate OR plantif LIKE :plantif');
//build query placeholders (*note: use bindValue for $_POST values)
$stmt->bindValue(':first', '%' . $_POST['first'] . '%');
$stmt->bindValue(':trialdate', '%' . $_POST['trialdate'] . '%');
$stmt->bindValue(':wcity', '%' . $_POST['wcity'] . '%');
$stmt->bindValue(':wstate', '%' . $_POST['wstate'] . '%');
$stmt->bindValue(':plantif', '%' . $_POST['plantif'] . '%');
$stmt->execute();
foreach ($stmt as $row) {
// do something with $row
echo $row['first'] . "<br />"; //individual value
}
}
I could use help on getting the PDO example working with a displayed result/row/value?