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'];
Related
i am stuck in a code, which is working fine but not all the time, it works for 4 transactions after that it shows an undefined offset error, which i can not figure out why it is occurring after running for four times, here is the code
<?php
$Selectlatest=mysqli_query($connection,"Select * from pos_order order by transaction_id desc limit 0,1");
$row_Selectlatest=mysqli_fetch_array($Selectlatest);
if(mysqli_num_rows($Selectlatest)==0){
$num = 001;
$invoice_no = date('dmy').$num;
}
$new = str_split($row_Selectlatest['invoice_no'],6);
$invoice_no= date('dmy').$new[1]+1 ;
?>
it works fine but after 4 consecutive transaction it shows an error which is
Notice: Undefined offset: 1 in C:\xampp\htdocs\Point_of_sale\admin\cart.php on line 60
How to solve it or any other way of generating the invoice or kind of referance no, which increases after checking the previous number from database?
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 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?
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...)
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.