variable result from sql query not showing - php

Is there anything wrong in this scenario? the result isn't showing. maybe i'm getting the syntax wrong?
// fetch the latest tweet
$lastTweet = '';
$q2 = "SELECT tweet FROM timel ORDER BY id DESC LIMIT 1";
list($lastTweet) = $conn->get_results($q2);
if(!$lastTweet){
$lastTweet = "You don't have any tweets yet!";
}
var_dump($lastTweet);
and this on the html body:
<span class="latest"><strong>Latest: </strong><span id="lastTweet"><?.=$lastTweet?></span></span>

If you changed your first line to
$lastTweet = array();
And your third line to
$lastTweet = $conn->get_results($q2);
Then you would be able to use
<?php var_dump($lastTweet); ?>
But in your case lastTweet contains a string of text, hence you need to use echo to print it's content.
<?php echo $lastTweet ?>
Nb: i'm not sure about your project details but i'm guessing you'd need to also get the date, maybe id and user_id of the tweet, if that's the case, you might want to make lastTweet an empty array()

Use this:
<span class="latest"><strong>Latest: </strong><span id="lastTweet">
<?php echo $lastTweet; ?></span> </span>
Saludos ;)

Related

how to retrive php query from db and set on textarea with executed?

This is the code which selects from DB and sets the image tag.
<div>
<?php $query = mysql_query("SELECT * FROM company where sn='1'");
while($rows = mysql_fetch_assoc($query)){
$logo = $rows['logo'];
$password = $rows['password'];
$phone = $rows['phone'];
}
?>
<img src="<?php echo $logo ?>"/>
</div>
When we get this and set on textarea then we want this query{which save in db} executed. and output show only Logo name.
But this time this show full query which save in db.
we want get this output on textarea:
<div><img src="logoname"/></div>
You are using mysql extension, which is deprecated. You should use mysqli instead.
The loop overwrites your variables ($logo, $password, $phone) in every iteration, so it makes no sense until you're fetching single row.
But if you're fetching single row, then you don't need a loop:
<?php
if ($r = mysqli_query($connection, "SELECT * FROM company WHERE sn = 1")) {
$company = mysqli_num_rows($r) ? mysqli_fetch_row($result)[0] : null;
mysqli_free_result($r);
}
?>
<img src="<?php echo empty($company) ? 'nophoto.png' : $company['logo']; ?>" />
Replace
SELECT * FROM company where sn='1'
With
SELECT * FROM company WHERE sn=1
If you take out the apostrophes, that might solve your problem since the value stored in your database is most likely not a string. Also you should have WHERE in capital letters.
Let me know if that answered your question! :)

Loading more results from database using $_POST (or $_GET?)

I have a blog page "plugin" in my own little cms and it's displayed with return() on the index.php.
Part of the index.php:
$id = (isset($_GET['m_id'])) ? $_GET['m_id'] : 1; //menu id
$sql = "SELECT m_cim, m_tartalom, m_plugin
FROM menu
WHERE m_s_id = (SELECT s_id FROM statusz WHERE s_nev='aktiv')
AND m_id = ".$id;
$eredmeny = mysql_query($sql);
if (#mysql_num_rows($eredmeny) == 0) {
$tartalom = "<h1>404!</h1>\n";
}
else {
$sor = mysql_fetch_assoc($eredmeny);
$tartalom = "<h2>{$sor['m_cim']}</h2>
<span class=\"tart\">{$sor['m_tartalom']}</span>\n";
if(!empty($sor['m_plugin'])){
$tartalom.=include("./modul/{$sor['m_plugin']}");
}
}
<section id="content">
<?php print $tartalom; ?>
</section>
The posts come from a database and the news.php is included in the index.php.
This is the "plugin" news.php
$aktiv="(SELECT s_id FROM statusz WHERE s_nev='aktiv')";
$sql = "SELECT hir_id, hir_cim, hir_tartalom, hir_ido
FROM hirek
WHERE hir_s_id=".$aktiv."
ORDER BY hir_id DESC";
$eredmeny = mysql_query($sql);
$kimenet = "";
while ($sor = mysql_fetch_assoc($eredmeny)) {
$kimenet.= "<article class=\"hirek\">
<h3>{$sor['hir_cim']}</h3>
<span class=\"hido\">{$sor['hir_ido']}</span>
<p class=\"htart\">".substr(strip_tags($sor['hir_tartalom']),0,200)."</p>
Tovább...
</article>\n";
}
return $kimenet;
If I use a php pagination, I only saw, that the page number is posted via the ‘GET’ method, but in that case, I use the GET method for the menu id, and when I want to post something else with get, the result will be the menu item with the actual id. Is it possible to use $_GET method for this?
I think this technique can solve my problem, but I don't know if it's outdated/not-so-good/do-not-use-it or not.
When I find somewhere a solution for my actual problem, somebody always says that "do not use it, because..." "it's not the best idea, because..." "this is not the best solution,because.."
Can I trust in this?

displaying 2 private messages to same destination with SQL

So i'm trying to implement a Private messaging system and so i use this code to extract the info from an SQL table:
$query = mysql_query("SELECT * FROM `pm` WHERE `to` = '{$_SESSION['user_id']}'");
$pm = mysql_fetch_array($query);
and essentially all this code does is pulls values from columns
(INT) from, (INT) to, (VARCHAR) subject and (VARCHAR) message
and it all worked until i sent more then 1 message to the same user.
thise code only shows the first message, and i have no idea how to make it show all the messages, so any help would be appriciated.
EDIT
Forgot to mention that the table itself is named 'pm'
EDIT2
this is how i echo the output:
<h3>From: </h3><?php echo $pm['from']; ?>
<h3>Subject: </h3> <?php echo $pm['subject']; ?>
<h3>Message: </h3> <?php echo $pm['message']; ?>
You didn't really tell us how you were echoing out the PM's. But here's how you would print every one.
while ($row = mysql_fetch_array($query)) {
echo $row['message'];
}
Not sure if this is the most efficient but you can add a time_created column and a was_sent column. Have your query grab the oldest unsent message and then mark it as sent. Repeat.

Selecting one value from an array called by a query in PHP

Basically I have a query called using PHP:
<?php $result = mssql_query("SELECT * FROM Colours WHERE Type = 'type1' ");
while ($row = mssql_fetch_array($result)) {
if ($row['ColourID'] == "1") {
$sayclass1="imgactive";
}else{
$sayclass1="imginactive"; }
?>
As you can see once I execute the query I then loop it, the problem is that it returns an array, now in some instances I need to use the full array, but I would like to be able to select one entry from it for if statements and such. For example:
<img id="h" src="<?php echo $row['thumbimg']; ?>
now thumbimg is a column in my DB, and it just holds a url. However due to the fact its an array the picture doesn't display because its echoing all the values, so instead of images/image1.png for example it is echoing images/image1.png images/image2.png images/image3.png etc etc...
I hope that makes sense, and can anyone tell me how to manipulate the query/code slight to still return all the entries but to select certain values from the arrays please?
You need to use the img tag with in for each if you want to show all the images
foreach($row[thumbimg] as $img):
<img id="h" src="<?php echo $img; ?>
end foreach;
<?php $result = mssql_query("SELECT * FROM Colours WHERE Type = 'type1' ");
while ($row = mssql_fetch_array($result)) {
if ($row['ColourID'] == "1") {
$sayclass1="imgactive";
}else{
$sayclass1="imginactive";
}
echo '<img id="h" src="'.$row['thumbimg'].'">';
}
?>
Using this above code, you will get one image per line from the database. The part of code you have copy-pasted is not enough to determine where your mistake is and why $row['thumbimg'] contains a concatenated value of the result.

Using PHP (and maybe AJAX?) with jQTouch

I am having some difficulty using PHP with jQTouch. I am fairly
confident with JavaScript however my PHP skills are little to none.
I am creating an application for my final year project at University
what displays football rumours posted by different users. My problem
is as follows:
I have one screen that displays each individual rumour, using a while
loop in PHP I am able to get each rumour from the database and display
them correctly. However I want to be able to click on one rumour which
then displays this rumour in a different screen, along with options to
reply/share etc. However I do not know how to tell which rumour has
been clicked on.
Snippets of my code:
All rumours page:
<?php
$q1 = "SELECT * FROM tblrumours;";
$r1 = mysql_query($q1);
while( $row1 = mysql_fetch_assoc($r1) ){
?>
<a class="rumourTag submit" id="<?php echo $row1['rumourID']; ?>">
<div class='oneRumour'>
<div class='standardBubble'>
<p>
<?php
$userID = $row1['userID'];
$q2 = "SELECT * FROM tblusers WHERE userID = $userID;";
$r2 = mysql_query($q2);
while( $row2 = mysql_fetch_array($r2) ){
$username = $row2['username'];
$teamID = $row2['teamID'];
}
$q5 = "SELECT * FROM tblteams WHERE teamID = $teamID;";
$r5 = mysql_query($q5);
while( $row5 = mysql_fetch_array($r5) ){
echo "<img src='img/".$row5['teamPicture']."' alt=''
class='teamImg' />";
}
?>
<span class='username'>
<?php
echo $username;
?>
</span>
<br/>
<span class='rumourMsg'><?php echo $row1['rumourText']; ?></
span>
</p>
</div>
</a>
SINGLE RUMOURS PAGE:
<?php
$q1 = "SELECT * FROM tblrumours WHERE rumourID = 1;"; /* NEED
TO SELECT WHERE RUMOUR ID IS THE ONE THAT IS CLICKED */
$r1 = mysql_query($q1);
while( $row1 = mysql_fetch_array($r1) ){
?>..........
I have tried using Session variables, storing the ID's in an array,
creating a separate php file for the single rumour page, and all to no
avail. I am guessing I have to use AJAX in some way, but I have no
idea where to even begin. Any help is greatly appreciated!
Thanks!
If you need to click on a rumour to see more details about it, you could always output in the HTML a unique value used to reference that rumour in the DB.
e.g. have <span class='rumourMsg' id='rumourName'> where rumourName is a unique value stored in your database to reference that rumour. Then when a user clicks to see more details, you can make a request to the PHP page with that value and return the content.
e.g. rumourDetails?rumourName=uniqueRumourName
(make sure to escape all your data properly to avoid SQL injection vulnerabilities.)

Categories