I want to make a list of all my users, so I used the following SQL query:
"SELECT Username FROM inloggen"
which works perfectly fine when I run it in phpMyAdmin and when I use
$list = mysql_fetch_array(mysql_query("SELECT Username FROM inloggen")); in php,
echo "<p> $list[0] </p>"; correctly returns the first user, but echo "<p> $list[1] </p>"; and higher all return an Undefined offset error, even though I have 6 users in my database.
Does anyone know why this happens?
Related
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
I am trying to create an admin panel for my website. I use this code on my dashboard to count the number of users (and similar for the blog posts etc.:
//get result
$data = mysql_query("SELECT count(user_id) AS total FROM account_users");
$info = mysql_fetch_assoc($data);
//output result
echo "Rows found :" . $info['total'];
When I enter this code and view the page, this is the result:
Rows found :
As you can see, the number is not visible.
I tried to execute the code in the SQL section of PHPMyAdmin, and the result is 1 (and there is 1 row in the database, so that is correct!)
How is this possible? (And can you help me?)
Update on first comment:
I added the code he told me to, but it isn't going better.....
Error I'm getting now:
Notice: Undefined index: auth in /home/vol9_6/epizy.com/epiz_21854614/admin.ictsupport.ga/htdocs/index.php on line 9
Warning: mysqli_query() expects at least 2 parameters, 1 given in /home/vol9_6/epizy.com/epiz_21854614/admin.ictsupport.ga/htdocs/index.php on line 19
Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, null given in /home/vol9_6/epizy.com/epiz_21854614/admin.ictsupport.ga/htdocs/index.php on line 20
Rows found :
Notice: Undefined index: username in /home/vol9_6/epizy.com/epiz_21854614/admin.ictsupport.ga/htdocs/nav.php on line 3
mysql_query is deprecated.
Use mysqli_query. Try:
$con=mysqli_connect("localhost","my_user","my_password","my_db");
$sql="SELECT count(user_id) AS total FROM account_users";
$result=mysqli_query($con,$sql);
// Associative array
$row=mysqli_fetch_assoc($result);
echo "Rows found :" . $row['total'];
I am implementing keyword search in PHP. When no. of results from DB are greater than 264 PHP echo fails to give any output. Any idea what might be the problem here?
// my algo is -
$result = mysql_query(searchQueryString) or die(mysql_error());
$row = mysql_fetch_assoc($result);
//Create a $json_array of required values
$str_to_print = json_encode($json_array);
echo $str_to_print;//this gives empty output when no. of rows from DB > 264
//Everything else is working properlycode here
Please excuse this, I wanted to comment but am unable to due to rep.
I used this statement: print json_encode(array(1, 2, 3, 4, 5)); and got the result of: [1,2,3,4,5].
Either your result from the mysql_query is wrong, or something else is.
Just noticed, you are using this line of code:
$str_to_print = json_encode($json_array);
But where is $json_array set? Is it set at all or are you passing a null pointer to the function? Error: Notice: Undefined variable: json_array
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...)
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");