php mysql affected_rows showing 2 - php

I am having a bit of trouble. This was working until I added a second database class to run some test methods. After removing this I am now getting this error and can't understand why.
Warning: extract() expects parameter 1 to be array, null given in /home/bitandpi/public_html/temp/build/build.php on line 49
Here is my code:
$urltag = urldecode($contentPageVar);
$sql = "SELECT * FROM shopproducts
WHERE urltag = '$urltag' AND urltag != ''
AND pd_active > 0 AND pd_visible > 0";
$result = $database->fetch_array($sql);
echo $database->affected_row()."<BR>";
print_r($result);
exit;
if($database->affected_row() > 0) {
// run code
}
I have printed the $sql var and ran it straight into phpmyadmin query and it returns 0 results.
However if I run the above code it prints the following on my screen:
2
Array ( )
Why is it telling me it is affecting rows when it is not?
Thanks

You have use num_rows.
$urltag = urldecode($contentPageVar);
$sql = "SELECT * FROM shopproducts WHERE urltag = '$urltag' AND urltag != '' AND pd_active > 0 AND pd_visible > 0";
$result = $database->query($sql);
echo $row_cnt = $result->num_rows;
echo "<br/>";
if($row_cnt>0){
// run code
$result1 = $database->fetch_array($sql);
}

Related

how to identify which is a non-object in the code if it shows error as `trying to get property of non-object`?

After making some modifications to my existing script, I created an error:
Notice: Trying to get property of non-object...
I am trying to figure how to identify if the variable $result is an object so I don't get this error (I am getting the error on this particular line if ($result-> num_rows > 0) { ). Here is what have:
<?php
$sql = "SELECT * FROM input WHERE id =".$_GET["id"];
$result = mysqli_query ($conn,$sql);
if ($result-> num_rows > 0) {
while($row -> $result->fetch_assoc()) {
$myid = $row["id"] ;
$sql2 = "SELECT * FROM output WHERE question_id = $myid ORDER BY date DESC";
$result2 = $conn->query($sql2);
$sql3 = "SELECT COUNT(*) as rowCount FROM output WHERE question_id = '".$myid."'";
$result3 = $conn->query($sql3);
$rowCount= $result3->fetch_assoc();
?>
How can I tell beforehand if this is an object or not?
First of all, do you have the variable $conn initialized any where in the code before that?
Second, you want to make your while loop like this:
while($row = $result->fetch_assoc()) { ... }
You can use to find content is array or object :
is_object() to find object
is_array() to find array
$sql = "SELECT * FROM compt WHERE id =1";
$result = mysqli_query ($conn,$sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
}
}

get minimum value from sql table using PHP

I want to get the minimum/smallest ID from SQL table where condition matches using PHP, I tried the below code but an error has occurred, ie
Trying to get property of non-object in /var/www/html/site/sitename/project-name.php on line 14
Any suggestion is highly appreciated, thanx
$query = "select MIN(id) FROM project_view WHERE project_id = '$projectId' and email = '$email_in_cookie'";
$result = $con->query($query);
if( $result->num_rows > 0 )
{
$row = $result->fetch_assoc();
$sectionID = $row['id'];
}
echo $sectionID;

Getting just one INT from the database with PHP

How to fetch an integer from a mysql database using PHP and phpmyadmin.
This is my code right now:
session_start();
require_once("connect.php");
$query = "SELECT klantID FROM klant ORDER BY klantID DESC LIMIT 1;";
$result = mysqli_query($con, $query);
//echo $query."<br>";
while( $row=mysqli_fetch_array($result, MYSQLI_ASSOC) ) {
echo $row['klantID'];
mysqli_close($conn);
}
I'm trying to get the highest 'klantID' in the table 'klant'. When I run the PHP file, it shows nothing. It's just blank.
How do I debug this to find out what is wrong?
The PHP script returns nothing because the SQL successfully returns no rows. You skip over the while loop and the program exits.
One way to help find out what is wrong is to read the result from mysqli_num_rows like this:
require_once("connect.php");
$sql = "SELECT `klantID` FROM `klant` ORDER BY `klantID` DESC LIMIT 1; ";
$query = mysqli_query($con, $sql);
if ( mysqli_num_rows($query) > 0 )
{
//we got a result
$result = mysqli_fetch_object($query);
echo "ID Found : ".$result->klantID."<br />";
} else {
echo "Nothing found!"
}
After that fetch, the result of mysqli_num_rows($query) > 0 evaluates to false and the program prints "Nothing found!"

Converting from mysql to mysqli (mysql_fetch_array) [duplicate]

This question already has answers here:
How to change mysql to mysqli?
(12 answers)
Closed 1 year ago.
I have some php code that was like this:
$row = mysql_fetch_array ( mysql_query("SELECT * FROM `tblFacilityHrs` WHERE `uid` = '$uid'"));
I'm now trying to convert it to mysqli_fetch_array as shown here http://php.net/manual/en/mysqli-result.fetch-array.php (Example #1 Object oriented style)
I'm not sure what the example means by "$result".
This is what I've converted my code to so far:
<?php
include('../config.php');
if (isset($_GET['uid']) ) {
$uid = $_GET['uid'];
$id = $_GET['id'];
if (isset($_POST['submitted'])) {
foreach($_POST AS $key => $value) { $_POST[$key] = mysqli_real_escape_string($value); }
//Query for tblFacilityHrs
$sql = " UPDATE tblFacilityHrs SET `title`='{$_POST['title']}',`description`='{$_POST['description']}' WHERE `uid` = '$uid' ";
$result = $mysqli->query($sql) or die($mysqli->error);
//Query for tblFacilityHrsDateTimes
$sql2 = "UPDATE tblFacilityHrsDateTimes SET `startEventDate`='{$_POST['startEventDate']}',`endEventDate`='{$_POST['endEventDate']}', `startTime`='{$_POST['startTime']}',`endTime`='{$_POST['endTime']}',`days`='{$_POST['days']}',`recurrence`='{$_POST['recurrence']},`finalDate`='{$_POST['finalDate']}' WHERE `id` = '$id' "; print $sql2;
$result2 = $mysqli->query($sql2) or die($mysqli->error);
echo ($mysqli->affected_rows) ? "Edited row.<br />" : "Nothing changed. <br />";
echo "<a href='list.php'>Back</a>";
}
$row = $result->fetch_array($mysqli->query("SELECT * FROM `tblFacilityHrs` WHERE `uid` = '$uid'"));
$row2 = $result2->fetch_array($mysqli->query("SELECT * FROM `tblFacilityHrsDateTimes` WHERE `id` = '$id'"));
?>
There is probably a lot wrong with as I am learning mysqli, but right now it errors on
Fatal error: Call to a member function fetch_array()
UPDATE queries do not return result objects, it returns TRUE (assuming success). You're then trying to call TRUE->fetch_array(), which obviously won't work.
Now, for doing what you actually want to do, try this:
$row = $mysqli->query("SELECT.....")->fetch_array();
Looks like you are trying to use an old result object (result of your UPDATE) to get results for a new query. You need to run the new query first, assign its result to an object, and then fetch the results from object. Check out the last three lines of your script re-written:
$facilityHoursQueryResult = $mysqli->query("SELECT * FROM `tblFacilityHrs` WHERE `uid` = '$uid'");
$facilityHoursDateTimesQueryResult = $mysqli->query("SELECT * FROM `tblFacilityHrsDateTimes` WHERE `id` = '$id'");
$facilityHoursRow = $facilityHoursQueryResult == FALSE ? NULL : $facilityHoursQueryResult->fetch_array();
$facilityDateTimesRow = $facilityHoursDateTimesQueryResult == FALSE ? NULL : $facilityHoursDateTimesQueryResult->fetch_array();
?>
Here is a helpful page with other result object methods:
http://www.php.net/manual/en/class.mysqli-result.php

num_row manipulation in mysql?

i have this function that brings me back a value by query a table in the database!
the code:
function recycle_check($recycle_id){
$query = "SELECT recycle_id FROM notes WHERE user_id = '".$_SESSION['user_id']."' and recycle_id ='$recycle_id'";
if(mysql_num_rows($query)== 0)
return 0;
else
return 1;
}
but its giving me an error saying:
Warning: mysql_num_rows() expects parameter 1 to be resource
i just want the function to either give me a zero or a number 1!
0 for exists and 1 for deosnt exist!
you need to run mysql_query before calling to mysql_num_rows
like :
$result = mysql_query("SELECT * FROM table1", $link);
$num_rows = mysql_num_rows($result);
what you need to pass to the function is:
The result resource that is being
evaluated. This result comes from a
call to mysql_query().

Categories