SQL being a jerk again - sql

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>";
}

Related

How can i read datas on sqlite3 with php variables?

$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.

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

SQL Select with Inner Join

I know that this question has been asked before but I am finding it difficult to apply the principle to sort out my issue. I have 2 tables that I want to select data from.
I have tried using the inner join sql query below:
$query1 = "SELECT `entries.id`, `code_id`, FROM `entries` INNER JOIN `codes` ON `code_id` = `codes.id`";
$result = mysql_query($query1);
And My display is as follows:
while($row = mysql_fetch_array( $result ))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['msisdn'] . "</td>";
echo "<td>" . $row['created'] . "</td>";
echo "<td>" . $row['code_id'] . "</td>";
echo "</tr>";
}
I am trying to get the actual code from the table codes, and display it to a website, but I keep getting the id(number) instead of actual code.
Can someone please assist. I have checked around the internet and even got some ebooks, but I am still a newbie and would like someone to guide me through the process. All input appreciated.
You have to assign the fields a new name if there are fields with the same name in different tables like "id":
$query1 = "SELECT `entries.id` as entriesid, entries.name, entries.msisdn, entries.created, `codes.id` as codesid, FROM `entries` INNER JOIN `codes` ON `entries.id` = `codes.id`";
$result = mysql_query($query1);
...
echo "<td>" . $row['entriesid'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['msisdn'] . "</td>";
echo "<td>" . $row['created'] . "</td>";
echo "<td>" . $row['codesid'] . "</td>";
You can do the following:
$sql = mysql_query("select entries.* , codes.* from entries,codes where entries.code_id=codes.id");
while($fetch = mysql_fetch_array($sql)) {
// Do Your Display
}
Hope this helps . Also mysql functions are degrading. So change that to mysqli / PDO

Drupal 7 Multiple Select Joins Syntax

Having real trouble getting my head around multiple joins. Can anyone tell me why this is wrong please?
*EDIT*
$query = db_select('node', 'n');
$query->join('file_usage', 'fu', 'fu.id = n.nid');
$query->join('field_data_field_featured_image', 'ffimage', 'ffimage.entity_id = n.nid');
$query->join('file_managed', 'fm', 'fm.fid = n.nid');
$query
->fields('fu', array('id'))
->fields('fm', array('filename'))
->fields('ffimage', array('field_featured_image_title'))
->fields('n', array('nid', 'title'))
->condition('n.type', 'work')
->orderby('n.nid', 'DESC')
->range(0, 20);
$result = $query->execute();
foreach ($result as $record) {
echo '<li>';
echo '<div class="home_text"><h1>'.$record->title.'</h1></div>';
echo '<a href="...some_node/';
echo $record->id;
echo '">';
echo '<img src="...some_files/';
echo $record->filename;
echo '" width="960px" height="615px" />';
echo '</a>';
echo '</li>';
}