PHP count(): warning - php

I'm making a simple CRUD single page application using PHP connected to a msqli database. All parts of the page work except for the "EDIT" function. It returns the warming "count(): Parameter must be an array or an object that implements Countable line 8"
<?php
include('server.php');
if (isset($_GET['edit'])) {
$id = $_GET['edit'];
$update = true;
$record = mysqli_query($db, "SELECT * FROM eBook_MetaData WHERE id=$id");
if (count($record) == 1 ) {
$n = mysqli_fetch_array($record);
$creator = $n['creator'];
$title = $n['title'];
$type = $n['type'];
$identifier = $n['identifier'];
$date = $n['date'];
$language = $n['language'];
$description = $n['description'];
}
}
?>
any help with this would be appreciated.

Use mysql_num_rows for checking numbers of row

I have a feeling its your sql query too. Try this instead $record = mysqli_query($db, "SELECT * FROM eBook_MetaData WHERE id='$id'");

Related

Fatal Error uncaught error call to a member function fetch_assoc()on null in

Image for reference I just dont get where I am wrong. Can somebody help me with this? I feel stupid right now
function total_price() {
$total = 0;
global $con;
$ip = getIp();
$select_price = $con->query("SELECT * from shoppingcart WHERE IP_address='$ip'");
while($row = $select_price->fetch_assoc()) {
$pro_id = $row['Product_id'];
$quan = $row['Quantity'];
$query = $con->query("SELECT * FROM products WHERE prod_id='$pro_id'");
while($row2 = $query->fetch_assoc()) {
$row2['product_price'] *= $quan;
$productprice = array($row2['product_price']);
$values = array_sum($productprice);
$total += $values;
}
}
echo "₱: ".$total;
}
I think there your $con->query(...) fails and so you $select_price is FALSE and not an object with some member functions.
Check Out the documentation of $mysqli::query
http://php.net/manual/en/mysqli.query.php
If you want to get the sql error print the Result of $con->error.
For example:
$select_price = $con->query("SELECT * from shoppingcart WHERE IP_address='$ip'");
if($select_price == FALSE) {
echo "SQL-Error:".$con->error
exit;
}
while($row = $select_price->fetch_assoc()) {
...
if fetch_assoc fails there is a problem with your result set.
Problem ist that:
$query
or
$select_price
is not a valid Result-Set.
Reason could be that the Querys to the database fails.
you can check this by doing:
print_r($select_price);
print_r($query);
Should Output: Ressource ID #XX -> if not, check your Query direct agains SQL.
hope this helps

Query was Empty error in php using PDO [duplicate]

I am trying to build a cart using MySQL. I keep getting this error 'Query was empty' when I run this code. Please help I've tried several things such as putting the variables inside the string instead of concatenating it.
<?php ob_start(); ?><?php require_once("../include/membersite_config.php"); ?>
<?php
require('../products_reloaded/config.php');
session_start();
$user = $_REQUEST['user'];
$user = mysql_real_escape_string($user);
$itemNum = $_REQUEST['itemNum'];
$itemNum = mysql_real_escape_string($itemNum);
$quantity = $_POST['quantity'];
$quantity = intval($quantity);
$CheckForExistence = mysql_query("select * from cart where user = '$user' and p_number = '$itemNum'" );
$alreadyExistsChecker = mysql_num_rows($CheckForExistence);
if($alreadyExistsChecker >= 1)
{
$quantity +=1;
echo "this is equal to $alreadyExistsChecker";
}
if($alreadyExistsChecker == 0)
{
$getQuery = mysql_query("select * from product where p_number = '$itemNum'");
while($row = mysql_fetch_array($getQuery))
{
$name = $row['p_name'];
$image = $row['p_url'];
$price = $row['p_price'];
}
$name = mysql_real_escape_string($name);
$image = mysql_real_escape_string($image);
$price = intval($price);
$query = mysql_query('insert into cart values('.$user.','.$itemNum.','.$name.', '.$image.','.$quantity.', '.$price.')');
$result = mysql_query($query);
if (!$result) {
print "An error occured: " . mysql_error() . "\n";
}
}
header('http://www.definitionxjm.com/shopping/viewCart.php');
?>
What do you try to achieve with this line?
$result = mysql_query($query);
Just delete it and change the line above to
$result = mysql_query('insert into cart values('.$user.','.$itemNum.','.$name.', '.$image.','.$quantity.', '.$price.')');
[Edit]
Btw. you are forgetting the " (quotes) inside the query which causes an sql error to occur, leading to $query = false (see manual). $query (false) then gets converted to string, resulting in '' (an empty string) which you pass to mysql_query and that generates an "Query was empty"-Message, as it should because you tried to send an empty query.
It can also mean the table which is in consideration is empty.This condition arises when the "DELETE FROM table_name where ;" is used. Here if the table "table_name" has no data, it cannot delete anything and hence it shows an error stating that the query is empty.

How to declare a variable

I have problem undefined variable. The thing is, I found a tutorial to make a type micro blog Twitter, had some problems, I decided, but this is taking me patience.
Error Message:
Notice: Undefined variable: extra in caminho\do\arquivo\functions.php on line 58
function:
function show_users($user_id=0){
if ($user_id > 0){
$follow = array();
$fsql = "SELECT user_id FROM following WHERE follower_id='$user_id'";
$fresult = mysql_query($fsql);
while($f = mysql_fetch_object($fresult)){
//array_push($follow, $f->user_id);
$follow[] = $f->user_id;
}
if (count($follow)){
$id_string = implode(',', $follow);
$extra = " AND id IN ($id_string)";
}else{
return array();
}
}
$users = array();
$sql = "SELECT id, username FROM users WHERE status='active' $extra ORDER BY username";
$result = mysql_query($sql);
while ($data = mysql_fetch_object($result)){
$users[$data->id] = $data->username;
}
return $users;
}
The line in question is:
$sql = "SELECT id, username FROM users WHERE status='active' $extra ORDER BY username";
Any suggestions?
There are two ways :
1. function show_users($user_id=0){
$extra='';
2. function show_users($user_id=0,$extra=''){
Actually you are declaring $extra only when you have $user_id > 0 BUT you must be having $user_id equals to or less than 0 for the current situation and thus your query couldn't find $extra.
I hope you get an idea.
function show_users($user_id=0){
$extra = ''; //if no extra, you'll get just an empty string
//etc...
}
function show_users(...) {
$extra = ''; // <--just add this line
blah blah blah blah blah
}
In your code, $extra gets defined ONLY if both if() tests succeed. Obviously they're not, so you end up with an undefined $extra in your sql string.
function show_users($user_id=0,extra='') // use this
{
if ($user_id > 0){
$follow = array();
$fsql = "SELECT user_id FROM following WHERE follower_id='$user_id'";
$fresult = mysql_query($fsql);
while($f = mysql_fetch_object($fresult)){
//array_push($follow, $f->user_id);
$follow[] = $f->user_id;
}
if (count($follow)){
$id_string = implode(',', $follow);
$extra = " AND id IN ($id_string)";
}else{
return array();
}
}

Query was empty PHP error

I am trying to build a cart using MySQL. I keep getting this error 'Query was empty' when I run this code. Please help I've tried several things such as putting the variables inside the string instead of concatenating it.
<?php ob_start(); ?><?php require_once("../include/membersite_config.php"); ?>
<?php
require('../products_reloaded/config.php');
session_start();
$user = $_REQUEST['user'];
$user = mysql_real_escape_string($user);
$itemNum = $_REQUEST['itemNum'];
$itemNum = mysql_real_escape_string($itemNum);
$quantity = $_POST['quantity'];
$quantity = intval($quantity);
$CheckForExistence = mysql_query("select * from cart where user = '$user' and p_number = '$itemNum'" );
$alreadyExistsChecker = mysql_num_rows($CheckForExistence);
if($alreadyExistsChecker >= 1)
{
$quantity +=1;
echo "this is equal to $alreadyExistsChecker";
}
if($alreadyExistsChecker == 0)
{
$getQuery = mysql_query("select * from product where p_number = '$itemNum'");
while($row = mysql_fetch_array($getQuery))
{
$name = $row['p_name'];
$image = $row['p_url'];
$price = $row['p_price'];
}
$name = mysql_real_escape_string($name);
$image = mysql_real_escape_string($image);
$price = intval($price);
$query = mysql_query('insert into cart values('.$user.','.$itemNum.','.$name.', '.$image.','.$quantity.', '.$price.')');
$result = mysql_query($query);
if (!$result) {
print "An error occured: " . mysql_error() . "\n";
}
}
header('http://www.definitionxjm.com/shopping/viewCart.php');
?>
What do you try to achieve with this line?
$result = mysql_query($query);
Just delete it and change the line above to
$result = mysql_query('insert into cart values('.$user.','.$itemNum.','.$name.', '.$image.','.$quantity.', '.$price.')');
[Edit]
Btw. you are forgetting the " (quotes) inside the query which causes an sql error to occur, leading to $query = false (see manual). $query (false) then gets converted to string, resulting in '' (an empty string) which you pass to mysql_query and that generates an "Query was empty"-Message, as it should because you tried to send an empty query.
It can also mean the table which is in consideration is empty.This condition arises when the "DELETE FROM table_name where ;" is used. Here if the table "table_name" has no data, it cannot delete anything and hence it shows an error stating that the query is empty.

Data insert problem within foreach loop

The problem is that only some of the XML data is being Inserted into the my mysql database. 10 results are supposed to be entered into the database but it varies between 2 and 8 results. I have no idea why it is doing this and I have tried adding a sleep function to slow the script down, but the data that is inserted into the data base is never as much as when I echo it out on screen. Any help would be much appreciated..
function post_to_db($xml,$cat_id){
if ($xml->Items->Request->IsValid == 'True'){
$xml = $xml->Items->Item;
foreach($xml as $item){
$asin = (string)$item->ASIN;
$title = (string)$item->ItemAttributes->Title;
$content = (string)
$item->EditorialReviews->EditorialReview->Content;
$sku = (string)$item->ItemAttributes->SKU;
$brand = (string)$item->ItemAttributes->Brand;
$feature = (string)$item->ItemAttributes->Feature;
$model_no = (string)$item->ItemAttributes->Model;
$review = (string)$item->ItemLinks->ItemLink[5]->URL;
$check = "SELECT * FROM `products` WHERE `asin` = '$asin'";
$checked = mysql_query($check);
$numrows = mysql_num_rows($checked);
if ($numrows == 0){
$query = "INSERT INTO `products`".
"(`cat_id`,`asin`,`sku`,`brand`,".
"`model_no`,`title`,`content`,`feature`) ".
"VALUES".
"('$cat_id','$asin','$sku','$brand',".
"'$model_no','$title',".
"'$content','$feature')";
$result = mysql_query($query);
$post_id = mysql_insert_id();
$review_page[] = array($post_id=>$review);
}
}
}
return $review_page;
}
My guess would be some of your variables from XML are creating an invalid query (do they contain quotes?)
Instead of this for each variable:
$asin = (string)$item->ASIN;
Do this instead:
$asin = mysql_real_escape_string((string)$item->ASIN);
If the problem still persists, change your mysql_query line to this for debugging:
$result = mysql_query($query) or die(mysql_error());

Categories