Notice: Array to string conversion. without array in code - php

the problem=
in my code i get the id of a person out of the database, then i fetch it and then i put it on another table in the database.
so first this i get the id out of the database:
$patientid = mysqli_query($connection, "SELECT id FROM patient WHERE `naam` = '$naam' AND `adres` = '$adres'");
then i fetch it:
$patientid1 = mysqli_fetch_assoc($patientid);
then i put it in another table:
$toevoegen = mysqli_query($connection, "INSERT INTO factuur (`soort`, `hoelang`, `titel`, `omschrijving`, `datum_aangemaaktfactuur`, `patient_id`, `artsid`)
VALUES ('$soort', '$hoelang', '$titel', '$omschrijving', '$datum', '$patientid1', '$artsid1')");
then when i run the code i get this notice: Notice: Array to string conversion.
how do i solve this? i have looked at other questions but they have the same error but different code. i dont even have an array in my code!
thanks
ps: if i replace $patientid1 with an number it works perfectly.

mysqli_fetch_assoc returns an associative array with selected fields as keys and column values as values.
You should modify code as follows
$patientRow = mysqli_fetch_assoc($patientid);
$patientId = $patientRow['id'];
Remember that for non unique results, you should iterate over whole result set like
while ($patientRow = mysqli_fetch_assoc($patientid)) {
$patientId = $patientRow['id'];
// other actions here
}
Last but not least, pay attention to $naam and $naam: use prepared statement instead will be definitely safer

Related

How can I get column names of a mysql query after the query result is empty

I want to generate an array of colnames after performing a query.
$sql = "SELECT prename, lastname, CONCAT (prename, ' ', lastname AS name)
FROM mytable WHERE ... ";
$result = mysqli_query($con,$sql);
$row = mysqli_fetch_assoc($result);
foreach ( $row as $key => $value ) $colNames[] = $key;
I know that I can get the colnames of a table using
$sql = "SHOW COLUMNS FROM mytable;
But how can I get colnames, that are created using MySql functions like "CONCAT ... " if the query-result is empty?
Therefore my question:
Is it possible to get the column names of a query that returns an empty result?
Is it possible to get the column names of a query that returns an empty result?
No, it is not possible because when you get the results you receive an associative array with keys<=>values. So if you receive an empty result there are no values, and if there are no values, there is no associative array to get the keys from.
If you are trying to write a minimalist block of code, you might want to try another approach. But you all ready wrote the answer: $sql = "SHOW COLUMNS FROM mytable; and it probably takes 0.0001 secs so...
FROM php.net/...mysql_fetch_assoc()
mysql_fetch_assoc
Return Values:
Returns an associative array of strings that corresponds to the fetched row, or FALSE if there are no more rows.

How can I insert a list of numbers into a MySQL table?

I need to insert a list of comment IDs into a column liked_comments.
For example if a user likes comments with IDs 72, 839, 37, then they will be stored in there.
$id = mysqli_real_escape_string($conn,$_POST['id']); // a number posted using ajax from another file
$username = mysqli_real_escape_string($conn,$_SESSION['username']);
if (!empty($_POST) && isset($_SESSION['username'])){
mysqli_query($conn,"UPDATE users SET liked_comments=liked_comments+$id WHERE username='$username'");
}
At the moment this just adds the IDs as though they were numbers rather than strings, but I need it to be 71,789,173 or 71 789 173 separated by a space or comma. Adding +' '+ or +','+ didn't seem to work.
You cannot concatenate using + in mysql, you have to use concat like
$Query = "UPDATE users SET liked_comments=concat(liked_comments, ',', '$id') WHERE username='$username'";
Also, Instead of storing comma separated values in column, you can move comments part to separated table, where each row contain PostID and UserID, that would be much neater and flexible.
The best way to do this would be to store arrays inside the database. For example; if we have this array:
$liked_comments_array = array(71, 789, 173);
You can store it in the array like so:
$liked_comments_array = serialize($liked_comments_array);
// now insert this into the database
Then when you fetch this array from the database, simply unserialize it like so:
$liked_array_comments = unserialize($field_from_database);

how to get one record from a mysql database with php

i'am trying to get one cell from my mysql database.
this cell is unique because of the date and hour.
so if date and time is selected with a WHERE it should come back with one number.
I checked everything but it still doesnt work. i get zero returns.
does anyone see something wrong with my code?
$result2 = mysql_query("SELECT * FROM `chairs` WHERE `date` = '$date' AND `hour` = '$hour' ");
$row = mysql_fetch_array($result2);
$dbchairs = $row[$chairs];
echo $dbchairs;
You have undefined variable chairs in
$dbchairs = $row[$chairs];
If in DB is column called chairs, use:
$dbchairs = $row['chairs']; // key of $row array is column name
change this
$dbchairs = $row[$chairs];
to
$dbchairs = $row['chairs'];
You are using undefined variable $chairs as your index in $row array.
syntax: $row['name_of_the_column_in_database']

MYSQL Statement with Array Variable

So I have the following mysql statement saving to an array:
$sql = "SELECT did FROM did WHERE id = '$did_id'";
$result = mysqli_query($link, $sql, MYSQLI_STORE_RESULT);
while($row = $result->fetch_row()){
$did[] = $row;
}
That part works great. But now I need to take the values in the $did array and perform another lookup using the values in it. The way it works is we have users assigned to certain did's. So I find the did's that the user is assigned to (the $did array) and only show them results from another table based on those did values. I have no idea how this part works, but this is what my next statement needs to do:
SELECT * FROM log WHERE did_id = "the values in $did array"
Hope someone can help. I really appreciate it. I haven't really been able to find anything on it.
You can use php's join with mysql's IN to make comma separated strings you can also use implode , join() is an alias of implode();
SELECT * FROM log WHERE did_id IN( ".join(',',$did).")
One thing to mention here that your $did should contains ids in manner like
array("1","2","3"....)
So in your loop fetch first index that holds did column value
$did[] = $row[0];
Note: i assume did , did_id type is int or bigint

How to query all fields in a row

I know this is very simple, but I haven't used PHP/MySQL in a while and I have been reading other threads/php website and can't seem to get it.
How can I query a single row from a MySQL Table and print out all of the fields that have data in them? I need to exclude the NULL fields, and only add those that have data to an html list.
To clarify, I would like to display the field data without specifying the field names, just for the reason that I have a lot of fields and will not know which ones will be NULL or not.
What you've outlined requires 4 basic steps:
Connect to the database.
Query for a specific row.
Remove the null values from the result.
Create the html.
Step 1 is quite environment specific, so that we can safely skip here.
Step 2 - SQL
SELECT * from <tablename> WHERE <condition isolating single row>
Step 3 - PHP (assuming that $query represents the executed db query)
//convert the result to an array
$result_array = mysql_fetch_array($query);
//remove null values from the result array
$result_array = array_filter($result_array, 'strlen');
Step 4 - PHP
foreach ($result_array as $key => $value)
{
echo $value \n;
}
Just SELECT * FROM table_name WHERE.... will do the trick.
To grab data from specific fields, it would be SELECT field_1,field_2,field_3....
you have to make a string which represent mysql query. Then there is function in php named mysql_query(). Call this function with above string as parameter. It will return you all results. Here are some examples
You need to do it like this...
First connect to your sql... Reference
Now make a query and assign it to a variable...
$query = mysqli_query($connect, "SELECT column_name1, column_name2 FROM tablename");
If you want to retrieve a single row use LIMIT 1
$query = mysqli_query($connect, "SELECT column_name1, column_name2 FROM tablename LIMIT 1");
If you want to fetch all the columns just use * instead of column names and if you want to leave some rows where specific column data is blank you can do it like this
$query = mysqli_query($connect, "SELECT * FROM tablename WHERE column_name4 !=''");
Now fetch the array out of it and loop through the array like this..
while($show_rows = mysqli_fetch_array($query)) {
echo $show_rows['column_name1'];
echo $show_rows['column_name2'];
}
If you don't want to include the column names in the while loop, you could do this:
while($show_rows = mysqli_fetch_array($query)) {
foreach( $show_rows as $key => $val )
{
echo $show_rows[$key];
}
}

Categories