mysql_num_rows causes an error message - mysql-num-rows

My simplified code is:
<?php
$con = mysql_connect("aaa", "bbb", "ccc", "ddd");
$sql = "SELECT * FROM list";
$result = mysql_query($sql,$con);
echo mysql_num_rows($result);
?>
I get the following error:
"Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\test\mysqli_num_rows.php on line 4"
Can anyone tell me whats wrong with that (simplified) code?
Thanks

Does this work?
<?php
$con = mysql_connect("aaa", "bbb", "ccc", "ddd");
mysql_select_db("database", $con);
$sql = "SELECT * FROM list";
$result = mysql_query($sql,$con);
echo mysql_num_rows($result);
?>

I have a hunch that $result is returning false, hence the error about a boolean value. This could mean that either the connection or the query is wrong.
Try putting:
$result = mysql_query($sql,$con) or die(mysql_error());
This hopefully would return an error message telling you what is wrong.
Also another point, you probably should be using mysqli or PDO API as mysql is being depreciated.

Related

Trying to get property of non-object laravel?

I am a beginner.
I'm trying to fix, i think i need help.
-This is code Controller
if(Input::hasFile('image')){
$dest = 'media/images/product/';
$name = str_random(6).'_'.Input::file('image')->getClientOriginalName();
//$resize =
Input::file('image')->move($dest,$name);
}
$loaispname = Input::get('loaispname');
$loaisp = new Loaisp;
$datas = $loaisp->getidloaisp($loaispname);
$idloaisp = $datas->id;
$item = new Sanpham;
$item->loaisp_id = $idloaisp;
$item->sanpham_name = Input::get('sanpham');
$item->sanpham_img = $name;
$item->sanpham_tieude = Input::get('tieude');
$item->sanpham_gia = Input::get('gia');
$item->sanpham_chitiet = Input::get('chitiet');
$item->sanpham_vitri = Input::get('vitri');
$item->save();
return Redirect::to('admin/dsachsanpham')->with('thanhcong','Saved');
-This is code Model
public function getidloaisp($loaispname){
//return Loaisp::where('loaisp_name','=',$loaispname)->get();
return DB::table('loaisp')->where('loaisp_name',$loaispname)->first();
}
-this is Error
ErrorException (E_UNKNOWN)
Trying to get property of non-object
Open: E:\xampp\htdocs\www\daunhot\app\controllers\AdminController.php
$loaispname = Input::get('loaispname');
$loaisp = new Loaisp;
$datas = $loaisp->getidloaisp($loaispname);
$idloaisp = $datas->id; // This is error
$item = new Sanpham;
When you see this error there could be two error: You got nothing back with your query(DB::table('loaisp')->where('loaisp_name',$loaispname)->first();), Or you got back an array, if you are using ->get() instead of ->first() you will get this error.
Try to use a foreach on your query result, or try to use dd() function on your result so you can see if is empty or not. If it is empty that is your porblem.
Same as what the rest is saying, dump $datas before the error, maybe add '=' to your where clause in that 'getidloaisp' function. You should be able to use the one your quoted out though, that is if you change the '->get()' to '->first()'.
Check if you are getting 'loaispname' on Input::get by:
echo "<pre>";
print_r($loaispname);
echo "</pre>";
exit;
If you're getting your posted data then do:
echo "<pre>";
print_r($datas);
echo "</pre>";
exit;
$datas->id work with DB table is need to be the table datas and cell id, and also they are case sensitive, not use Id or other.

how can i access directly to magento order history from observer?

How can i get the value ,what i set by addStatusHistoryComment while creating order by php script.
$order = $observer->getEvent()->getOrder();
$dbOrderId = $order->getId();
$MagOrderId = $order->getRealOrderId();
Mage::log('dbOrderId : '. $dbOrderId);
Mage::log('MagOrderId : '. $MagOrderId);
I need to get like something $order->getStatusHistoryComment()
it is not working.
Need help.
The following data is not working as order is not commit yet.
$connection = Mage::getSingleton('core/resource')->getConnection('core_read');
$sql = "SELECT comment FROM sales_flat_order_status_history WHERE parent_id=' $dbOrderId' limit 1 ";
$connection = Mage::getSingleton('core/resource')->getConnection('core_read');
foreach ($connection->fetchAll($sql) as $arr_row) {
$comments=$arr_row['comment'];
Use getStatusHistoryCollection instead of getStatusHistoryComment and it should work. The method is defined in Mage_Sales_Model_Order.
Or you can use getVisibleStatusHistory if you want only the comments visible on frontend.

for listing values from db using codeigniter rest for web service

to list the values from db i have been using the followin code
model
function user_list_community($serviceName) {
$ipJson = json_encode($input);
$this->db->select('*');
$this->db->from('community');
//$this->db->where('user_id', $input['user_id']);
$query = $this->db->get();
$result = $query->result();
if (!empty($result)) {
$data['list_community']= $result;
$data['message'] = 'User details retrieved successfully.';
$status = $this->ville_lib->return_status('success', $serviceName, $data, $ipJson);
} else {
$data['message'] = 'Unable to retrieve.Please the user id';
$status = $this->ville_lib->return_status('error', $serviceName, $data, $ipJson);
}
return $status;
}
controller
function list_community_post(){
$serviceName = 'list_community';
$retVals = $this->user_model->user_list_community($input, $serviceName);
header("content-type: application/json");
echo $retVals;
exit;
}
the issue am facing is am getting the values but am getting error as follows. what was the wrong am doing here. can someone help me . thanks.
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: input
Filename: controllers/users.php
Line Number: 57
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: input
Filename: models/user_model.php
Line Number: 67
You are getting error because $input is not defined in the code. So you should either this:
function list_community_post(){
$input='';
$serviceName = 'list_community';
$retVals = $this->user_model->user_list_community($input, $serviceName);
header("content-type: application/json");
echo $retVals;
exit;
}
or
function list_community_post(){
$serviceName = 'list_community';
$retVals = $this->user_model->user_list_community($serviceName);
header("content-type: application/json");
echo $retVals;
exit;
}
Just like it says on the tin. Your $input variable is not created anywhere, just passed as a parameter to the json_encode and user_list_community functions.
the function user_list_community($serviceName) doesn't have two arguments while you calling it with $this->user_model->user_list_community($input, $serviceName);
try function user_list_community($input, $serviceName);
And ofcourse you have to pass value for $input arguement as above said.

Codeigniter query issue

I am running the following query in codeiginter, I want to return the data from the database but I'm getting a blank array as a response. My attempts to debug this issue have shown that the return value from the model may be the issue. I need a extra pair of eyes on this so if anyone can help me let me know.
Model
function test($term)
{
$sql = "select description from category where title = '$term'";
$query = $this->db->query($sql);
return $query->result();
}
Controller
function test()
{
$this->load->model('test');
$term = $this->input->post('term',TRUE);
$rows = $this->test->test($term);
echo json_encode($rows);
}
your code looks correct... try printing the last query that was made and check it out
COntroller
function test()
{
$this->load->model('test');
$term = $this->input->post('term',TRUE);
$rows = $this->test->test($term);
echo $this->db->last_query();exit; //this gives you the last query that was made.
echo json_encode($rows);
}
check if the posted value is correct... you can even try running this in mysql and see if this is returning any rows...
use echo $this->db->last_query(); and get you query and try it with your mysql editor
I notice by running echo $this->db->last_query(); the value of $term had a leading white space that cause the query to return null. by adding $term = trim($term, " "); I was able to remove the leading white space and get the correct result.
$this->load->model('tp_model');
$term = $this->input->post('term',TRUE);
$term = trim($term, " ");
$rows = $this->tp_model->test($term);

'mysql_num_rows(): supplied argument is not a valid argument' when trying to use JDatabase

$select = "SELECT * FROM `jos_users`";
$connection->setQuery($select);
$rows = $connection->getNumRows();
$rows does not work it throws the 'mysql_num_rows(): supplied argument is not a valid' error but...
$result = $connection->loadObjectList();
$result works fine.
Is this a Joomla bug?? Or what am i doing wrong?
<?
$select = "SELECT * FROM `jos_users`";
$connection->setQuery($select);
//add this:
$connection->query();
$rows = $connection->getNumRows();
?>
You're setting the query but not executing it.
would it need to call query()... $res = $db->query('select * from jos_users'); $db->getNumRows($res);

Resources