MySQL , PHP : Hide specific Value from table - php

I am working on a ' Show Online user' script where the script show everybody who is online.
Now i want to remve the entry which matches the session user name i.e "if (Online user = Session User name ) then do not display it , just like on facebook.com chat where your friends id is shown and not your own Id
my code is as follows :
<?php
mysql_connect("localhost","root","12345");
mysql_select_db("accounts");
$user = $_SESSION['user_name'];
$result = mysql_query("SELECT * FROM online * WHERE ($chat<>$user)");
while($row=mysql_fetch_array($result)) {
$chat=$row["emp_name"];
$chlk = ("<a href=javascript:void(0) onclick=javascript:chatWith('$chat')>$chat</a>");
$chs = ("<a>$chat</a>");
if ($chat <> $user) {
echo $chlk;
}
else {
echo $chs;
}
echo $chlk;
}
?>
I am getting the following error :
Notice: Undefined variable: chat in localhost/accounts/removeuser.php on line 7
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given localhost/accounts/removeuser.php on line 9
Any help is highly appreciated.

Correction in query.
"SELECT * FROM online WHERE ($chat<>$user)"
OR
Replace $chat in query with your table field name.
there is extra * before WHERE that is invalid.
and $chat is not defined in query.

<> is not PHP as far as I know. You need to use !==.
(and probably read up on some PHP basics...)

Related

PHP Notice: Trying to get property 'message' of non-object

and thank you for dropping in to help me.
currently i am trying to bring an old mafia game up to date for my own personal use as no one uses them now, and am stuck using objects which i have never used before.
The error i get is :
Notice: Trying to get property 'message' of non-object in C:\wamp64\www\mafia\Send.php on line 67
Here is the code
$query=mysqli_query($connection,"SELECT * FROM messages WHERE f='$ownusername' ORDER by id DESC LIMIT 3");
$info = mysqli_fetch_object($query);
$fetch=mysqli_fetch_object(mysqli_query($connection,"SELECT * FROM autospam LIMIT 1"));
if ($info->message == $message && $fetch->c == 1){
mysqli_query($connection,"UPDATE users SET mute='1', mutedby='Spamming' WHERE username='$ownusername'");
echo "You have been muted!";
}
This has been fixed thanks to Dmitry, for pointing me in the right direction, i found that the id in the sql insert was the error, where it was trying to insert an empty value into an auto increment table.
I removed the ID from the sql insert and all is working perfectly

Echo data from database using PHP no errors but data will not show

I am currently working on a page for a local football team. I want to be able to insert the player information into a my database and then echo the data back onto the page for each player. I have created this code however there is no sign of any of my HTML coding being echoed back to me on the page. If anyone could help with this it'd be greatly appreciated.
<?php
$query = mysql_query("SELECT * FROM tbl_u14team");
while($rows = mysql_fetch_array($query)):
$name = $rows['name'];
$position = $rows['position'];
$age = $rows['age'];
$height = $rows['height'];
$biography = $rows['biography'];
$image = $rows['image'];
echo '<div id="teammember">
<img src="'.$image.'" />
p>Name:'.$name.'</br>
Position:'.$position.'</br>
Age:'.$age.'</br>
Height:'.$height.'</br>
Biography:'.$biography.'</p>
</div>';
endwhile;
?>
[04-Sep-2014 04:50:54 America/Denver] PHP Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /home2/localbu6/public_html/dafcyouth/u14s.php on line 14
This means your query has failed.
For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.
Since the error is complaining about a boolean, (and FALSEbeing a boolean and only returned if the query failed) - your query has failed.
Replace your mysql_query line with the following and take the necessary actions
$query = mysql_query("SELECT * FROM tbl_u14team") or die(mysql_error());
Does your MySQL user have SELECT privileges on the database you're connected to?
And SELECT privileges on the table?
Does the table tbl_u14team exist?
Has your MySQL connection been disconnected before this code hits?
Try putting var_Dump(mysql_fetch_array($query)); before the while to see it the content array is as expected. Or do a var_Dump($rows) within the while.

php error Trying to get property of non-object

I have an admission form which is getting value from database table. Scenario is I have list of courses a student select a course and I pass course ID in URL then use this code to get corresponding Course name in the field: (The form is designed in joomla using breezing forms.)
$this->execPieceByName('ff_InitLib');
$course_id= JRequest::getVar('CID');
global $database, $rec;
$database->setQuery("SELECT * FROM course_list WHERE record = '$course_id' AND name = 'CourseName'");
$row = $database->loadObjectList();
$rec = $row[0];
ff_setValue('ProsCourse', $rec->value);
Unfortunately I get this error:
* EXCEPTION CAUGHT BY FACILEFORMS *
PHP error level : E_NOTICE
PHP filename : /home/web10385/public_html/**/components/com_breezingforms/facileforms.process.php(1219) : eval()'d code
PHP linenumber : 7
Last known pos : Before form custom piece code at line 1
Error message : Undefined offset: 0
* EXCEPTION CAUGHT BY FACILEFORMS *
PHP error level : E_NOTICE
PHP filename : /home/web10385/public_html/**/components/com_breezingforms/facileforms.process.php(1219) : eval()'d code
PHP linenumber : 8
Last known pos : Before form custom piece code at line 1
Error message : Trying to get property of non-object
The above code is the before form piece.
Thanks
You are not constructing the database object....
You have declared $database but you haven't initialized it with an object...That's the problem..It seems you have forgotten it.
Since, before initializing it, it's a non-object type. And you have tried to use it two times, hence you got two errors.
global $database, $rec;
^^ uninitialized....
$database->setQuery("SELECT * FROM course_list WHERE record = '$course_id' AND
^^ but you are using it here....
$row = $database->loadObjectList(.....
^^ here....
From the information seen this is what may be the solution:
EDIT:
$database=JFactory::getDbo();
Here's what I have done:
In Form pieces > Before form add this script:
$this->execPieceByName('ff_InitLib');
$course_id= JRequest::getVar('CID');
//intialize BF utilities
$this->execPieceByName('ff_InitUtilities');
global $database, $rec;
$database = ff_select('SELECT * FROM course_list WHERE record = '.$course_id.' ');
$rec = $database[0];
Then add this to the VALUE field of the TEXT field (course name):
<?php global $rec; return $rec->name; ?>
That should do it.
Peace.

Should this php function run recursively?

Im having problems with a method in a class file:
public static function getPageLeft($pid)
{
self::_dbConnect();
echo "now " .$pid;
$pid--;
echo " after ". $pid;
$data = mysql_query("SELECT id FROM evening_pages WHERE id='".$pid."'") or die(mysql_error());
$rows = mysql_num_rows($data);
if($rows == 1){
echo " in ";
return $data;
}elseif($pid != self::getMinPage()){
self::getPageLeft($pid);
echo " Current: " . $pid . " min: " .self::getMinPage();
}
}
I have the echos in there for debug only, and im getting:
now 22 after 21now 21 after 20 Current: 21 min: 1
This code is to find the next page left in a database driven CMS in case the client deletes a row from the database, it finds the next lower value.
So page id is 22 that your on, looks for id 21 in database, if its not there it should return 0 for the rows and move on and try again but on id 20, but there IS an entry for id = 20.
Anyone spot any issues with the code?
Also getting this error when it tries to find a page which doesnt exist before the current page, eg on page 22 but there is no ID for 21:
Warning: mysql_fetch_array() expects parameter 1 to be resource, null given in /home/prelas/public_html/index.php on line 37
the code there is (line 37 is while):
$getPageLeft = sql::getPageLeft($pid);
while($row = mysql_fetch_array($getPageLeft)){
$pageleft = $row['id'];
}
Many thanks.
Just a quick edit: " in " never gets echoed, but yet when there is no deleted pages the navigation works fine.
Further edit: The code which makes use of the code (by like 37):
$navigation = '<div id="direction">
<span class="inNav"> << < </span><span class="black">•</span><span class="inNav"> > >></span>
</div>'
So you can see it uses $pageleft in there however when its dealing with blank pages the value is nothing (empty).
why don’t you write the SQL statement to get the next lower ID? For example:
SELECT `id`
FROM `evening_pages`
WHERE `id` < ?
ORDER BY `id` DESC
LIMIT 1
Or:
SELECT MAX(`id`)
FROM `evening_pages`
WHERE `id` < ?
That way, you only have to check if something has been returned by the query. Less code, less overhead for database requests.
The mysql extension is being phased out. Learn about using mysqli or PDO, and read up on the dangers of SQL injection.

problem with get url using php and mysql

Sorry if this question may seem easy to some but i cannot seem to figure it out. i was told that i could come here because the guys here are very helpful. i am having problem with the following code. when the uid is call into the url for example page.php?uid=5 i get "some code" if i do page.php?uid=letters i get redirected to page.php?uid=1. that works fine. but if a user should enter page.php?uid=1letters i get this error..Unknown column '1gh' in 'where clause'
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\wamp\www\page.php on line 173.
i only get this error if the user enters unwanted characters at the end of get url id. How can i prevent this from happen how can i have it redirect to page.php?uid=1... see code below
$id = mysql_real_escape_string(#$_GET['uid']);
$query = mysql_query("SELECT user.* FROM user WHERE id = '$id'");
if ((mysql_num_rows($query)==0)) {
header("location:page.php?uid=1");
die();
}
else {
while($rows = mysql_fetch_array($query)){
$foo = $row['foo'];
echo "some code";
mysql_query returns false on error. You should check what it returns:
$query = "SELECT user.* FROM user WHERE id = '$id'"
$result = mysql_query($query);
if ($result === false) {
die($query.'<br/>'.mysql_error());
}
Then you can understand why it failed. I've added the query to the die() statement so you can try the query manually as well.
The error given is unknown column of a string, which suggests you are not enclosing the value with single quotes in the SQL query, even though the code in the question does.
In production you should be taking all steps you can to ensure errors are handled gracefully. In this case you will probably want the same behaviour as not finding a user:
header("location:page.php?uid=1");

Categories