i'm still new towards php but after searching through multiple topics i can't seem to figure this one out.
$query2 = "SELECT COUNT(MAP_CODE2) FROM TCODE_MAPPING WHERE MAP_CODE2 = 'ABC123'";
$result2 = odbc_exec($connect,$query2);
echo $result2;
i have a query above that i'd like to use to get the total number of rows within the query that i've set, however for some reason i keep getting hit by the error
Array to string conversion in /var/www/html/xxx.php on line 85
Would highly appreciate if anyone could help out on what i'm doing wrong. Thank you!
Problem is:-
You are trying to echo a result-set object of array type.
Solution (check the code comments):-
$query2 = "SELECT COUNT(MAP_CODE2) AS MYCOUNT FROM TCODE_MAPPING WHERE MAP_CODE2 = 'ABC123'"; // given a name to the count
$result2 = odbc_exec($connect,$query2); //prepare and execute query
while (odbc_fetch_row($result2)) { //iterate over result-set object
echo odbc_result($result, "MYCOUNT"), "\n"; // echo count
}
What is happening here is you are echoing an array object type that is also a resource type and php echo only string and other primitive type variables.
so you need to use a loop to access the row or to get row count and access row just use a foreach($results as $result)
to get row count visit this sqlsrv-num-rows . on this official php link you can get more example how to fetch data.
Related
I have this table named tblDate which contains the columns dateID, dateDay, and dateMonth along with 30 records for the month of November. Using sqlsrv_fetch_array(), I can only retrieve the first record. I have read here that in order to retrieve the multiple records, I need to use sqlsrv_next_result(), but how exactly do I use it? As I understand it, this function makes the next record "active," which would then allow me to echo the next record and so on. But when I try to run this code:
$tsql = " SELECT * FROM tblDate;";
$stmt = sqlsrv_query( $conn, $tsql);
$row = sqlsrv_fetch_array($stmt);
echo "Date: ".$row["dateID"]." - ".$row["dateMonth"]." ".$row["dateDay"]."\n";
$next_row = sqlsrv_next_result($stmt);
$row = sqlsrv_fetch_array($stmt);
echo "Date: ".$row["dateID"]." - ".$row["dateMonth"]." ".$row["dateDay"]."\n";
I get the error:
Trying to access array offset on value of type bool in ...
I get that sqlsrv_next_result($stmt) returns the boolean false when there are no further records, but there are more records. I expected this code should fetch me the first two records, did I misunderstand something? Also, I don't get "consume the first result"; how does that and insert records come into play? I've tried just straight up copy-pasting and replacing the variables but I cant seem to get it to work. Though honestly, this is my very first week working with php so I have no idea what I'm doing. Ultimately, I just want to display all records of a query[SELECT * FROM tblDate among others], how do I do that?
Try using this (untested!):
while (($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_BOTH, SQLSRV_SCROLL_NEXT)) !== NULL)
{
// Your business logic using $row.
}
I'm new to PHP and i want to know how i can subtract a specific amount from the results from counting the total amount of rows in a table. In this case i'd like to minus the value 3 from whatever the value of the total rows is. But i keep getting an error. Below is my code.
$cartwork = $con->query("SELECT count(*) FROM table");
$vs = '3';
$camount = $cartwork - $vs;
echo "$camount";
When the code runs i get the error "Object of class mysqli_result could not be converted to int" what can i do to fix this and get it to work properly.
The query returns a result set. You need to parse through the result set(s) in order to access the values returned. That's basically what the error states.
Please see here for documentation on the PHP function for fetching rows:
http://php.net/manual/en/function.mysql-fetch-row.php
So basically you would need
$row=$cartwork->mysql_fetch_row();
$cartWork_value = $row[0];
$vs = '3';
$camount = $cartwork_Value - $vs;
echo "$camount";
Note - this assumes that you get back exactly one result row (which should be the case with your query).
You can simply change your query to:
$cartwork = $con->query("SELECT count(*)-3 FROM table");
It doesn't smell particularly good though.
So I'm trying to build an image uploading website and wish to access a mysql table using a query ..now I wish to store all the arrays obtained from these queries into one single array. I then wish to access all the elements of this array. How should I do it?
This is what I tried:
$allimages = array();
$sql="SELECT uploaderId FROM foostable WHERE foo='bar'";//a query which fetches the image uploader's id from the foostable ..you don't need to worry about this part just know that this query returns more than one uploaderIds
$result=mysqli_query($conn,$sql);
while($row=mysqli_fetch_assoc($result)){
$uploaderId=$row['uploaderId'];
$sql1="SELECT uploader FROM imagestable WHERE uploaderId='$uploaderId' ORDER BY datetime DESC";
$result1=mysqli_query($conn, $sql1);
$row1=mysqli_fetch_assoc($result1);
$allimages=$allimages+$row1;
}
foreach ($allimages as $ai) {
echo $ai['uploader'];
}
When I run this code, I get the following error:
Warning: Illegal string offset 'uploader' in...
I'm definitely doing something wrong but am not able to figure out what that is.
I've been looking everywhere for this but am not able to find it therefore I posted this question! I'm really new to this and any help would be really really appreciated! Thank you! :)
You're adding new elements to your array the wrong way.
Change
$allimages=$allimages+$row1;
to
$allimages[] = $row1; // Short for array_push($allimages, $row1)
Read more about array_push() in the manual
SELECT i.uploader
FROM foostable f
JOIN imagestable i
ON i.uploaderid = f.uploaderId
WHERE f.foo = 'bar'
ORDER
BY i.datetime DESC
You could write a single query to obtain the desired results. Please try the following code:
$allimages = array();
$sql = "SELECT i.uploader, i.datetime
FROM imagestable i INNER JOIN foostable f
ON i.uploaderId = f.uploaderId
AND f.foo = 'bar'
ORDER BY i.datetime DESC";
$result=mysqli_query($conn,$sql);
while($row=mysqli_fetch_assoc($result)){
$allimages[] = $row['uploader'];
}
print_r($allimages);
You should just be able to do:
$allImages[] = $row1;
This should just push the new array to the end of allImages array everytime the loop runs.
I am trying to print out the column headers for any query entered. I have other code that connects to the database and actually prints the results, but I am having trouble with the line
'$result .= $heading->name;'
I keep getting this error:
'Catchable fatal error: Object of class mysqli_result could not be converted to string in...'
Could someone explain what the problem is? According to the php manual this should give me the right information. I am fairly new to php though, so if you improve the code could you please explain how you did it?
I have the following code so far that gets a result from a query:
$result = mysqli_query($conn,$query);
if($result){
if( mysqli_num_rows($result)>0){
$columnNo = mysqli_num_fields($result);
for($i=0;$i<$columnNo;$i++){
$heading = mysqli_fetch_field_direct($result,$i);
$result .= $heading->name;
}
}
}
$result is an object being used by mysqli. Then you try to append a string onto the end of it.
Just use a different variable name besides $result which will be a string in which you will collect the names of your columns. Or you might save the names in an array like this:
$var[] = $heading->name;
since $result is an object you are using for executing the query, i won't be able to store any other data coming from db. you have to take another variable (say $variable) to store the data coming from db. Follow the below code:
$result = mysqli_query($conn,$query);
$variable=''; // add this
if($result){
if( mysqli_num_rows($result)>0){
$columnNo = mysqli_num_fields($result);
for($i=0;$i<$columnNo;$i++){
$heading = mysqli_fetch_field_direct($result,$i);
$variable .= $heading->name; //modify here
}
}
}
I hope this will solve the issue..
I am using the sqlsrv driver for IIS so I can connect to a MS SQL server in PHP.
I've managed to convert a lot of my original mysql_ code and all going well, until I tried to SELECT some DateTime fields from the database. They were coming back as Date objects in PHP rather than strings, I found the fix which is adding this to the connection array:
'ReturnDatesAsStrings'=>1
Since doing that though my code is broken when trying to populate my recordset:
function row_read($recordset) {
if (!$recordset) {
die('<br><br>Invalid query :<br><br><bold>' . $this->sql . '</bold><br><br>' . sqlsrv_error());
}
$rs = sqlsrv_fetch_array($recordset);
return $rs;
}
The error is: sqlsrv_fetch_array(): 16 is not a valid ss_sqlsrv_stmt resource
There is such little amount of help on that error in Google so this is my only shot! I just don't get it.
row_read is called from within a While: while ($row = $db->row_read($rs)) {
Any ideas?
Just to add more code and logic - I do a simple SELECT of all my orders, then as it loops through them, I do another 2 SELECT's on the orders table then the customer table. It's falling down when I try these extra 2 'gets':
$this->db->sql = "SELECT * FROM TicketOrders";
$rs = $this->db->query($this->db->sql);
$this->htmlList->path("skin/search.bookings");
if ($this->db->row_count != 0) {
while ($row = $this->db->row_read($rs)) {
// Load the order row
$this->TicketOrders->get($this->db, $row['Id']);
// Load the customer row
$this->Customers->get($this->db, $row['CustomerId']);
Did you pass this resource variable by another function? If yes, you can try by executing the sqlsrv_query and executing sqlsrv_fetch_array in one function; don’t pass the ss_sqlsrv_stmt resource by another function. Hope that it will help.
Does your program involves a nested query function?
If so, the next question is: are you opening the same database in the inner query function?
Try these changes:
comment out the lines that open the database, including the { and } that enclose the function,
change the name of connection and array variables between the outer loop and the inner loop.
In other words, the outer loop may have:
$tring = sqlsrv_query($myConn, $dbx_str1);
while( $rs_row1 = sqlsrv_fetch_array($tring, SQLSRV_FETCH_ASSOC))
and the inner loop would have:
$tring2 = sqlsrv_query($myConn, $dbx_str2);
while( $rs_row2 = sqlsrv_fetch_array($tring2, SQLSRV_FETCH_ASSOC))
sqlsrv_fetch_array need a ss_sqlsrv_stmt resource. There must be something wrong with your SQL.