mysqli_fetch: returns one row - php

I am making a private message system and I'm using the mysqli_fetch() function in a while statement to return all the rows associated with the query. However, PHP only returns the last row in MYSQL.
Here is my code:
<?php
$Connect = new mysqli("localhost", "root", "", "Data");
error_reporting(E_ALL ^ E_NOTICE);
session_start();
$Val = $_POST['ID'];
$Get = 'SELECT * FROM CMessages WHERE PID="'.$Val.'"';
$Username = $_SESSION['Username'];
$Admin = $_SESSION['Admin'];
if($Result = $Connect->query($Get))
{
while($Row = $Result->fetch_assoc())
{
$User = $Row['Username'];
$Msg = $Row['Msg'];
$Date = $Row['Date'];
$ID = $Row['ID'];
if($User == $Username)
{
$MText['T'] = '<div id="Msg">' . $User . ' : ' . $Msg . ' - ' . $Date .' - <a class="TLink" href="MDelete.php?ID='.$ID.'">Delete</a></div>';
}
elseif(isset($Admin))
{
$MText['T'] = '<div id="Msg">' . $User . ' : ' . $Msg . ' - ' . $Date .' - <a class="TLink" href="MDelete.php?ID='.$ID.'">Delete</a></div>';
}
else
{
$MText['T'] = '<div id="Msg">' . $User . ' : ' . $Msg . ' - ' . $Date .'</div>';
}
}
}
echo json_encode($MText);
?>

It returns all rows but , in the while loop, you are always overwriting the $MText variable. Therefore only the last one will be displayed with json_encode.
Maybe you meant to write $MText['T'][] instead of $MText['T'].

Related

How to add link to mysql database record as a <html> href?

if($rezultat = $polaczenie->query($sql))
$ilu_userow = $rezultat->num_rows;
while($ilu_userow>0){
$wiersz = $rezultat->fetch_assoc();
$id = $wiersz['id'];
$like = $wiersz['likes'];
$price = $wiersz['price'];
echo '<div class="pozycja">
<span>
<h1><font color="blue"/> '
. $id . ' </h1></font><font color="red"/> '
. $like . ' </font><h2><strong><font color="green"/> '
. $price . '</h2></strong></font></span></div>';
$ilu_userow = $ilu_userow - 1;
}
This is my code, and - as in the topic - i want to create html adress to every row i get from my database, and the question is - how?
if($rezultat = $polaczenie->query($sql)) $ilu_userow = $rezultat->num_rows; while($ilu_userow>0){
$wiersz = $rezultat->fetch_assoc();
$id = $wiersz['id'];
$like = $wiersz['likes'];
$price = $wiersz['price'];
echo '<div class="pozycja">
<span>
<h1><font color="blue"/> '
. $id . ' </h1></font><font color="red"/> '
. $like . ' </font><h2><strong><font color="green"/> '
. $price . '</h2></strong></font></span>';
//added line start
echo ''.$id.'';
echo '</div>';
//added line end
$ilu_userow = $ilu_userow - 1; }

Uknown column error i have tired multpul things

Im getting a Unknown column error and i cant seem to find a topic that seems to help me.. here the code but when i seem to remove the WHERE username = $user in the $query or replace $user with a quote it seems to work just fine. (FYI Im like a total noob at php)
<?PHP
$id = "";
$username = "";
$email = "";
$nick = "";
$isMod = "";
$rank = "";
$joinDate = "";
$ip = "";
$coins = "";
$curHead = "";
$curFace = "";
$curNeck = "";
$curBody = "";
$curHands = "";
$curFeet = "";
$curPhoto = "";
$curFlag = "";
$curColor = "";
$db = mysql_connect("localhost","root","");
mysql_select_db("opencp", $db);
$user = $_GET['user'];
$query = "SELECT * from game_users WHERE username = ". $user. "";
$result = mysql_query($query);
if($result === FALSE) {
die(mysql_error());
}
echo "<?xml version=\"1.0\"\n";
echo "<products>\n";
while($line=mysql_fetch_array($result)){
echo "<item>" . $line['id'] . "</item>\n";
echo "<item>" . $line['username'] . "</item>\n";
echo "<item>" . $line['email'] . "</item>\n";
echo "<item>" . $line['nickname'] . "</item>\n";
echo "<item>" . $line['ismoderator'] . "</item>\n";
echo "<item>" . $line['rank'] . "</item>\n";
echo "<item>" . $line['joindate'] . "</item>\n";
echo "<item>" . $line['ips'] . "</item>\n";
echo "<item>" . $line['coins'] . "</item>\n";
echo "<item>" . $line['curhead'] . "</item>\n";
echo "<item>" . $line['curface'] . "</item>\n";
echo "<item>" . $line['curneck'] . "</item>\n";
echo "<item>" . $line['curbody'] . "</item>\n";
echo "<item>" . $line['curhands'] . "</item>\n";
echo "<item>" . $line['curfeet'] . "</item>\n";
echo "<item>" . $line['curphoto'] . "</item>\n";
echo "<item>" . $line['curflag'] . "</item>\n";
echo "<item>" . $line['colour'] . "</item>\n";
}
echo "</products>";
mysql_close($db);
?>
Change it to,
$query = "SELECT * from game_users WHERE username = '". $user ."'";
$result = mysql_query($query);
You should start with PHP Strings.
Note: Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.
username is a string so it should be in single quotes.
$user = $_GET['user'];
$query = "SELECT * from game_users WHERE username = '". $user. "'";
$result = mysql_query($query);
There are a couple answers here already but I thought I'd point out that since your string is in double quotes you can make it less confusing by not using the concatenation (period) style:
$query = "SELECT * from game_users WHERE username = '$user ' ";
Variables are replaced inside double quotes in php, but not within single quoted strings.

cannot login to appropriate page

I have created the below login function and called the same at another page as given below. But don't know why it is not working for me.
function auth_check_user1($login, $password, $agent )
{
$query = 'SELECT * FROM ' . USERS_TABLE . ' WHERE login = "' . $login . '" AND password ="' . $password . '" AND user_category ="' . $agent . '" LIMIT 1';
$r = $db->query ($query);
if ($db->numrows($r)==1)
{
$logged = TRUE;
}
else
{
$logged = FALSE;
}
return $logged;
}
if (auth_check_user1($session->fetch('login'), $session->fetch('password'), $session->fetch('agent')) )
{
$query= 'SELECT first_name,id FROM ' . USERS_TABLE . ' WHERE approved = 1 AND login = "' . $session->fetch('login') . '" LIMIT 1';
$r = $db->query( $query);
<?php echo URL; ?>/login_user.php"><?php echo $lang['Menu_User_Login']; ?>
<?php echo URL; ?>/index.php?req=logout"><?php echo $lang['Logout']; ?>
}
<?php echo URL; ?>/login_user.php"><?php echo $lang['Menu_User_Login']; ?>
<?php echo URL; ?>/index.php?req=logout"><?php echo $lang['Logout']; ?>
Why do you have <?php ?> inside code ? There should be:
echo URL.'/login_user.php">'.$lang['Menu_User_Login'];
echo URL.'/index.php?req=logout">'.$lang['Logout'];
And I am not sure about the > in your output.. that mess up HTML, if your URL is only URL and not some HTML code
if ($db->numrows($r)==1) {
$logged = TRUE;
Shouldn't it be > num_rows?
function auth_check_user1($login, $password, $agent )
{
$query = 'SELECT * FROM ' . USERS_TABLE . ' WHERE login = "' . $login . '" AND password ="' . $password . '" AND user_category ="' . $agent . '" LIMIT 1';
$r = $db->query ($query);
if ($db->numrows($r)==1) {
$logged = TRUE;
} else {
$logged = FALSE;
}
return $logged;
}
if (auth_check_user1($session->fetch('login'), $session->fetch('password'), $session->fetch('agent')) ) {
$query= 'SELECT first_name,id FROM ' . USERS_TABLE . ' WHERE approved = 1 AND login = "' . $session->fetch('login') . '" LIMIT 1';
$r = $db->query( $query);
echo URL . "/login_user.php" . $lang['Menu_User_Login'];
echo URL . "index.php?req=logout" . $lang['Logout'];
}
Aside from these edits, I can't speak for your functions, numrows could be incorrect.

Using multiple tables in a search engine?

To clean and tidy-up my code, I want to add multiple tables with the fields:
id
title
description
keywords
link
but I also want them in sections so in MySql I want different tables with categories such as: "News", "Social Networking" and "Shopping", but how can I get that? This is my code:
<?php
if( count($terms) == 0){ // If no terms entered, stop.
echo "No Search Terms Entered.";
}else{
// connect
$connect = mysql_connect("XXX", "XXX", "YYY") or die('Couldn\'t connect to MySQL Server: ' . mysql_error());
mysql_select_db("theqlickcom_774575_db1", $connect ) or die('Couldn\'t Select the database: ' . mysql_error( $connect ));
/* Query Statement Building - Terms together */
$query = " SELECT * FROM scan WHERE ";
$terms = array_map('mysql_real_escape_string', $terms);
$i = 0;
foreach ($terms as $each) {
if ($i++ !== 0){
$query .= " AND ";
}
$query .= "keywords LIKE '%{$each}%'";
}
$query = mysql_query($query) or die('MySQL Query Error: ' . mysql_error( $connect ));
$numrows = mysql_num_rows($query);
if ($numrows > 0) {
while ($row = mysql_fetch_assoc($query)) {
$id = $row['id'];
$title = $row['title'];
$description = $row['description'];
$keywords = $row['keywords'];
$link = $row['link'];
echo '<h2><a class="ok" href="' . $link . '">' . $title . '</a></h2>' . PHP_EOL;
echo '<p class="kk">' . $description . '<br><br><span class="keywords">' . PHP_EOL;
echo '<p><a class="okay" href="' . $link . '">' . $link . '<br><br><span class="keywords">' . PHP_EOL;
}
} else {
/* Query Statement Building - Terms Separate */
$query = " SELECT * FROM scan WHERE ";
$terms = array_map('mysql_real_escape_string', $terms);
$i = 0;
foreach ($terms as $each) {
if ($i++ !== 0){
$query .= " OR ";
}
$query .= "keywords LIKE '%{$each}%'";
}
$query = mysql_query($query) or die('MySQL Query Error: ' . mysql_error( $connect ));
$numrows = mysql_num_rows($query);
if ($numrows > 0) {
while ($row = mysql_fetch_assoc($query)) {
$id = $row['id'];
$title = $row['title'];
$description = $row['description'];
$keywords = $row['keywords'];
$link = $row['link'];
echo '<h2><a class="ok" href="' . $link . '">' . $title . '</a></h2>' . PHP_EOL;
echo '<p class="kk">' . $description . '<br><br><span class="keywords">' . PHP_EOL;
echo '<p><a class="okay" href="' . $link . '">' . $link . '<br><br><span class="keywords">' . PHP_EOL;
}
} else {
echo "No results found for \"<b>{$k}</b>\"";
}
}
//disconnect
}
?>
If you have to search multiple table with one query
1.use mysqli
2.use UNION ALL in query
Join table
use mysql procedure

Displaying PHP results?

I need a little help here with some php.
just a little explanation:
(im trying to display 5 results using this code
$n_id = mysql_real_escape_string ($_GET['id']);
$path = '';
if(isset($n_id) && $n_id != "") {
$sql = 'SELECT * FROM test2 WHERE id="' . $n_id . '"';
$news = mysql_query($sql);
if($result = mysql_fetch_array($news)) {
$title = mysql_result($news,0,"title");
$date = mysql_result($news,0,"date");
echo '<b>' . $title . ' | ' . $date . '</b>
<br/>
<img src="images.php?id='. $n_id .'>';
} else {
header("Location: vendi.php");
}
echo '<br />Back to Archive';
}
It does display but i want that 1st result to be (image+title of the news and other results to be just title).
Hope i wrote it clearly what i needed help with.
Thank you
Your SQL statement is only fetching a single row. This isn't a complete solution, but should get you closer:
if (isset($_GET['id']) && is_numeric($_GET['id'])) {
$n_id = (int)$_GET['id'];
$path = '';
$count = 0;
$sql = 'SELECT * FROM test2 WHERE id BETWEEN ' . $n_id ' AND ' . ($n_id + 5);
$news = mysql_query($sql);
while ($result = mysql_fetch_array($news)) {
$title = $result['title'];
$date = $result['date'];
echo '<b>' . $title . ' | ' . $date . '</b>';
if ($count < 1) {
echo '<br/><img src="images.php?id='. $n_id .'>';
$count++;
}
}
if ($count == 0) { header("Location: vendi.php"); }
echo '<br />Back to Archive';
}
This should do it.
$n_id = mysql_real_escape_string ($_GET['id']);
$path = '';
if(isset($n_id) && $n_id != "") {
$sql = 'SELECT * FROM test2 WHERE id="' . $n_id . '"';
$news = mysql_query($sql);
$first = TRUE;
if($result = mysql_fetch_array($news)) {
$title = mysql_result($news,0,"title");
$date = mysql_result($news,0,"date");
if($first == TRUE) {
echo '<b>' . $title . ' | ' . $date . '</b>';
$first = FALSE;
}
else {
echo '<b>' . $title . ' | ' . $date . '</b>
<br/>
<img src="images.php?id='. $n_id .'>';
}
}
else {
header("Location: vendi.php");
}
echo '<br />Back to Archive';
}
}
While looping through your results, you can check if you are at the first result:
$counter = 0;
while ($row = mysql_fetch_array($news)) {
$title = mysql_result($news,0,"title");
$date = mysql_result($news,0,"date");
echo '<b>' . $title . ' | ' . $date . '</b>';
if ( $counter == 0 ) {
echo '<br /><img src="images.php?id='. $n_id .'>';
}
}

Categories