fetch_assoc() issue with query - php

I have this function:
function myFunc()
{
$conn=new mysqli('localhost','root','','myDb');
$conn->query('BEGIN');
$query="SELECT events_participants.userid,(SELECT COUNT(*) FROM events_participants WHERE events_participants.eventid=events.id FOR UPDATE) AS totRep FROM events INNER JOIN events_participants ON events_participants.eventid=events.id WHERE events.isConfirmed=3 AND reputationsUpdated=0";
$result=$conn->query($query);
if($result!==FALSE)
{
while(($row=$result->fetch_assoc())!==NULL)
{
$query="UPDATE users SET reputation=reputation+".$row['totRep'].",presences=presences+1 WHERE id=".$row['userid'];
$result=$conn->query($query);
if($result==FALSE)
{
$conn->query('ROLLBACK');
throw new DatabaseErrorException();
}
}
$query="UPDATE events SET reputationsUpdated=1 WHERE isConfirmed=3 AND reputationsUpdated=0";
$result=$conn->query($query);
if($result!==false)
$conn->query('COMMIT');
else
$conn->query('ROLLBACK');
throw new DatabaseErrorException();
}
else
{
throw new DatabaseErrorException();
}
}
myFunc();
If i test this it tells me:
Call to a member function fetch_assoc() on a non-object..on the line where is the while loop.
But the fetch_assoc method over there actually populated my $row array (i tested echoing out $row['totRep']..so the problem is not in the query..
even if I test the same query in phpmyadmin it actually returns two rows..
where is the error?
what am I missing??
thank you again guys!

You are rewriting your $result variable numerous times.
You have to rename it.
If a variable going to be used further in the script (like $result one) it shouldn't be rewritten.
However, if it's one-time used variable, like $query one, it's possible to keep the same name for readability.

if($result) is the correct way.
if($result!=false) is also correct.
if($result!==false) is WRONG.

Related

Php postgresql error, prepared statement already exists

Am getting an error of prepared statement "my_query7" already exists, i call this function each time a user tries to update table leader_info in the database, i have gone through the documentation for pg_prepare and i don't understand what is meant by it should only be run once. code snippets will be of help. Thanks.
function add_leader_country($user_id,$l_country)
{
global $connection;
$query = pg_prepare($connection,"my_query7","update leader_info set l_country = $1 where user_id = $2 and status < 9");
$result = pg_execute($connection,"my_query7",array($l_country,$user_id));
if(!$result)
{
echo pg_last_error($connection);
}
else
{
echo "Records created successfully\n";
}
$row = pg_affected_rows($result);
return $row;
}
Prepare execute does not permit duplicate naming, so that is your error.
A query should only be prepared once, for example, in a cycle for the preparation state must be set out of the for and its execution in the for.
$result=$pg_prepare($connection,"my_query7",$query);
for($id=1;$id<3;$id++){
$result=pg_execute($connection,"my_query7",array($l_country,$user_id));
...
}
In your case using a functio that use the prepare and execute multiple times it's a problem.
What are you trying to accomplish with this function dispatches more code like where you are calling the function. This way I might be able to help you.
If you want to use functions I would use this method
Exemple from https://secure.php.net
<?php
function requestToDB($connection,$request){
if(!$result=pg_query($connection,$request)){
return False;
}
$combined=array();
while ($row = pg_fetch_assoc($result)) {
$combined[]=$row;
}
return $combined;
}
?>
<?php
$conn = pg_pconnect("dbname=mydatabase");
$results=requestToDB($connect,"select * from mytable");
//You can now access a "cell" of your table like this:
$rownumber=0;
$columname="mycolumn";
$mycell=$results[$rownumber][$columname];
var_dump($mycell);
If you whant to use preaper and execute functions try to create a function that creates the preparations only once in a session. Do not forget to give different names so that the same error does not occur. I tried to find something of the genre and did not find. If you find a form presented here for others to learn. If in the meantime I find a way I present it.

Using PHP and oci8 to handle a query

This is a past paper question for a database class that I'm stuck with. I'm just preparing for my exam, so it's okay to give away the answer.
Consider the following schema:
Borrow(userid: string, callnum: string, copynum: integer, checkout: date, return: date)
Here is the PHP function that has an error.
function countCheckedOutBookCopies($callnum){
$sql = "SELECT COUNT(*) AS bookcount FROM borrow
WHERE return = null and callnum = '".$callnum."'";
$stid = oci_parse($this->conn, $sql); //assume $this->con is correct
if($row = oci_fetch_object($stid)){
return $row->bookcount;
} else{
return -1;
}
}
There are 3 questions.
1.Find the error and fix it.
2.Another error occurs, fix it.
3.Despite everything being fixed, the function would return -1 all the time. Why is this?
I'm only familiar with procedural PHP using MySQL. But I tried running the code and was resulted with $stid returning boolean all the time because I don't know which part is right and which part is wrong.
Here are the things I've tried
1.Changing '".$callnum."' to just simply '$callnum' (because this is how I've always done it in MySQL)
2.Changing return = null to return = 'null' (but I don't think this is the case)
3.Maybe there is something wrong with the concept of getting COUNT(*) instead of just *
EDIT: Just a thought: I feel like oci8 and MySQL do pretty much the same thing, but is there a reason to prefer one over another? I'm sure MySQL is the more popular one, but my school seems to prefer using oci8 for exam questions
Thanks in advance!
Despite the "Find the error" and "Another error occurs":
1.) "return is null",
2.) this->conn is probably supposed to be this->con,
3.) the code is missing oci_execute($stid);,
4.) oci_free_statement($stid);.
function countCheckedOutBookCopies($callnum){
$sql = "SELECT COUNT(*) AS bookcount FROM borrow
WHERE return is null and callnum = '$callnum'";
$stid = oci_parse($this->con, $sql); //assume $this->con is correct
oci_execute($stid);
$ret = -1;
if($row = oci_fetch_object($stid)){
$ret = $row->bookcount;
}
oci_free_statement($stid);
return $ret;
}

Basic mysqli select

I have a select statement where I want to get all rows from a table but seem to be having a mental blockage - this should be elementary stuff but can't seem to get it working.
There are only two rows in the table 'postage_price' - and two columns : price | ref
Select statement is as follows:
$get_postage="SELECT price FROM postage_price ORDER BY ref DESC";
$get_postage_result=mysqli_query($dbc, $get_postage) or die("Could not get postage");
while($post_row=mysqli_fetch_array($dbc, $get_postage_result))
{
$post1[]=$post_row;
}
I am then trying to echo the results out:
echo $post1['0'];
echo $post1['1'];
this is not showing anything. My headache doesn't help either.
while($post_row = mysqli_fetch_array($dbc, $get_postage_result))
{
$post1[] = $post_row['price'];
}
As you see: $post_row in this line: = mysqli_fetch_array($dbc, $get_postage_result) is an array. You are trying to save the whole array value to another array in a block. :)
EDIT
while($post_row = mysqli_fetch_array($get_postage_result))
...
You have $post1[]=$post_row; and $post_row is itself an array. So you can access post data with following: $post1[NUMBER][0] where NUMBER is a $post1 array index and [0] is 0-index of $post_row returned by mysqli_fetch_array.
Probably you wanted to use $post1[]=$post_row[0]; in your code to avoid having array of arrays.
You are passing 1 and 0 as string indexes, this would only work if you had a column called 0 or 1 in you database. You need to pass them as numeric indexes.
Try:
print_r($post1[0]);
print_r($post1[1]);
or
print_r($post['price']);
print_r($post['ref']);
with all your help I have found the error - it is in the mysqli_fetch_array where I had the $dbc which is not required.
$get_postage="SELECT price FROM postage_price ORDER BY ref DESC";
$get_postage_result=mysqli_query($dbc, $get_postage) or die("Could not get postage");
while($post_row=mysqli_fetch_array($get_postage_result))
{
$post1[]=$post_row['price'];
}
instead of:
$get_postage="SELECT price FROM postage_price ORDER BY ref DESC";
$get_postage_result=mysqli_query($dbc, $get_postage) or die("Could not get postage");
while($post_row=mysqli_fetch_array($dbc, $get_postage_result))
{
$post1[]=$post_row['price'];
}
Bad day for me :(
Thanks all
If something does not work in a PHP script, first thing you can do is to gain more knowledge. You have written that
echo $post1['0'];
echo $post1['1'];
Is showing nothing. That could only be the case if those values are NULL, FALSE or an empty string.
So next step would be to either look into $post1 first
var_dump($post1);
by dumping the variable.
The other step is that you enable error display and reporting to the highest level on top of your script so you get into the know where potential issues are:
ini_set('display_errors', 1); error_reporting(~0);
Also you could use PHP 5.4 (the first part works with the old current PHP 5.3 as well, the foreach does not but you could make query() return something that does) and simplify your script a little, like so:
class MyDB extends mysqli
{
private $throwOnError = true; # That is the die() style you do.
public function query($query, $resultmode = MYSQLI_STORE_RESULT) {
$result = parent::query($query, $resultmode);
if (!$result && $this->throwOnError) {
throw new RuntimeException(sprintf('Query "%s" failed: (#%d) %s', $query, $this->errno, $this->error));
}
return $result;
}
}
$connection = new MyDB('localhost', 'testuser', 'test', 'test');
$query = 'SELECT `option` FROM config';
$result = $connection->query($query);
foreach ($result as $row) {
var_dump($row);
}

Is using mysql_num_rows (rowCount in PDO) necessary in update or insert query?

Should I be using mysql_num_rows (rowCount in PDO) in update or insert query?
Currently, my code looks likes this,
public function update_username(){
$q = "UPDATE usertable SET username = '$user_name' WHERE id = '$user_id' LIMIT 1";
$r = $db->query($q);
if($r){
$message = "Updated successfully";
return $message;
}else{
return false;
}
}
Should I change it to like this?
public function update_username(){
$q = "UPDATE usertable SET username = '$user_name' WHERE id = '$user_id' LIMIT 1";
$r = $db->query($q);
if($r){
$num = $r->rowCount();
if($num == 1){
$message = "Updated successfully";
return $message;
}else{
$message = "An error occurred";
return $message;
}
}else{
return false;
}
}
Normally, query goes through without any error, so I shouldn't worry about it too much, but which one would be a better coding practice? Or do you suggest something else?
Thanks so much in advance!
Actually the two codes do something different.
The first one will print "Update success" if the query was successfully executed. But a query can be successfully executed also without affecting any row, i.e. you have a WHERE statamenet that does not match. The second code will not print "Update success" if no rows were affected.
Of course, if you're sure that your WHERE statement has to match, you can use both codes without any difference and using the second one could help you to spot any potential bug, i.e. it doesn't match and so something went wrong (probably the id was different from the one you expected).
Generally, to answer your question, mysql_num_rows is needed only if you want to know how many lines were affected. It's not mandatory at all to use it.
So, it depends on what you want. Both are good, but they are different.
If you are 100% sure the variables are created by you and not someone else you can do it like that, but you can minimize the code more:
public function update_username(){
$q = "UPDATE usertable SET username = '$user_name' WHERE id = '$user_id'";
if($db->query($q)){
return "Updated successfully";
}
return false;
}
First, because a query is executed successfully, doesn't necessarily mean that anything has been updated. So if you need to distinct the difference between a queries validity or the update change, then yes, rowCount would be a good practice.
Second, a prepared statement would be more wise to use when assigning variables (SQL injection, etc).
public function update_username(){
$q = "UPDATE usertable SET username = :user_name WHERE id = :user_id LIMIT 1";
$r = $db->prepare($q);
$r->bindValue(':user_name', $user_name);
$r->bindValue(':user_id', $user_id);
if($r->execute()){
$message = "Updated successfully: updated ".$r->rowCount();
return $message;
}else{
return false;
}
}
To avoid code duplication, maybe you should consider avoiding writing the same execution code for a query, and move that to a method/function which does that all for you, e.g
public function validateStmt($r) {
// validate query
if($r->execute()) {
// check result set
if($r->rowCount() > 0) {
return $r;
}
else {
// if a result set IS expected, then you might consider to handle this as
// a warning or an error
}
}
else {
// query invalid
}
}
Depending on the situation, you will have to choose which part you should use. mysql_num_rows() is used to check how many rows have been affected from your query you have executed. So, it's up to you to decide whether it is really necessary to add the mysql_num_rows() function in to your code or not.

Pass result of mysql_fetch_object() to a function does not work

I have the following problem:
public function row2Partner($row){
echo $row->PartnerID;
}
public function main(){
$query = "SELECT PartnerID, PartnerName FROM Partner";
$result = mysql_query($query);
$this->row2Partner(mysql_fetch_object($result));
}
This gives me the error in row2Partner():
Trying to get property of non-object
But $row is an Object! And if I do
echo $row->PartnerID in the main function, it works.
Any ideas?
Thx,
Martin
If your result returns more than one row, your object is going to be multi-dimensional. I'm pretty sure you can do something like this if you just want to echo the first one:
public function row2Partner($row){ echo $row[0]->PartnerID; }
If you are looking for only one result, I would also limit my query to just one...
SELECT PartnerID, PartnerName FROM Partner LIMIT 1
If you want to echo out all your rows (in the case of multiple) results, you can do this:
public function row2Partner($row){
foreach($row as $result) {
echo $result->PartnerID;
}
}
Hope that helps.
PS
Just as a sidenote, I tend to like to use associative arrays when dealing with MySQL results--it just makes more sense to me. In this case, you would just do this instead:
mysql_fetch_assoc($result)
Are you sure that mysql_query() has executed the query successfully, and also that there is actually a row being returned? It might be worth checking it, e.g.
//check query executed ok
if ($result = mysql_query($query)) {
//check there is actually a row
if ($row = mysql_fetch_object($result)) {
$this->row2Partner($row);
} else {
//no data
}
} else {
//error
die(mysql_error());
}
Best thing I can think of is that you may need to pass by reference rather than by value. Change your function declaration to
public function row2Partner(&$row)
Hope that helps,
David

Categories