I'm trying to display the users nickname into a html form input text field. It was working, but now quit showing up randomly. There is data in the database for the user. The fields are just blank and show nothing. I have plenty other fields working, but this does not and idk why. Feel free to edit the title I did not know how to word it.
When I try to implode the $nickname
$string_nickname = implode(',', $nickname);
I get the error
Warning: implode(): Invalid arguments passed in
C:\xampp\htdocs\Client-Projects\Crossfire\CoinSubmission.php on line
26
So, I tried parsing it to just a string since it's not an array, like..
$string_nickname = $nickname['nickname'];
But when I do this the field in the form is just completely blank and it gives no error.
Here is the input field where I insert nickname.
<label>NICKNAME:</label>
<input type="text" name="nickname" id="nickname" value="<?php echo $string_nickname; ?>" readonly>
and where I select and set nickname.
$result = mysqli_query($con,"SELECT adminlogin.nickname FROM adminlogin INNER JOIN coinsub ON coinsub.profileid=adminlogin.profileid") or die(mysqli_error($con));
$nickname = mysqli_fetch_assoc($result);
$string_nickname = implode(',', $nickname);
Please let me know if you need anymore info.
$string_nickname = '';
if(!empty($nickname)){
$string_nickname = implode(',', $nickname);
}
Do it with object orientated mysqli
$arr = array();
while ($row = $result->fetch_assoc()) {
$arr[] = $row['nickname'];
}
$string_nickname = implode(',', $arr);
You are fetching one value, I think you need more than one. Just create a new array, push to it and you are done. Even if you have one value in the database, this works The above array contains all the nicknames we get from the database. Once you implode them, they are like
nickname1, nickname2, nickname3, nicknamen
So your whole code becomes something like this
$result = $con->query("SELECT adminlogin.nickname FROM adminlogin INNER JOIN coinsub ON coinsub.profileid=adminlogin.profileid") or die(mysqli_error($con));
$arr = array();
while ($row = $result->fetch_assoc()) {
$arr[] = $row['nickname'];
}
$string_nickname = implode(',', $arr);
If you want to implode try this :
$string_nickname = implode(', ', (array)$nickname);
For select mysqli try this :
$selectquery = $con->prepare("SELECT adminlogin.nickname FROM adminlogin
LEFT JOIN coinsub ON coinsub.profileid=adminlogin.profileid");
$selectquery->execute();
$result = $selectquery->get_result();
$user = $result->fetch_assoc();
$nickname = $user['nickname'];
You can also use while statement as follow :
while($user=$result->fetch_assoc()){
echo $user['nickname'];
}
Related
Good day! I am trying to get the user ids based on the names submitted on the input field. But the loop only returns the user id of the first selected user. Can somebody please help me?
Here's the code:
if(isset($_POST['proponent'])){
$proponent = mysqli_real_escape_string($con,$_POST['proponent']);
$myArray = explode(',', $proponent);
$posts = array();
foreach($myArray as $item) {
$query = "SELECT user_id FROM user WHERE CONCAT(fname, ' ', lname) = '$item'";
$get = mysqli_query($con, $query);
array_push($posts, mysqli_fetch_assoc($get));
print_r($posts);
}
}
Using mysqli_fetch_assoc only once will only get you the first row if it exists. So you will need to add this in a loop, say while loop like below in order for the mysqli_function to move it's pointer over the entire result set.
while($row = mysqli_fetch_assoc($get)){
$posts[] = $row;
}
Please see database's image also
I want to show my checkbox values from database.
Example : my value is 25,26.
So, How can i display it one by one ?
such as,
-- 1st value is : 25.
--2nd Value is : 26.
my code:
$db = new mysqli("localhost","paroshic_paroshic","kxmcwQzLTrTR","paroshic_matri2018jl");
$sql = "select * from tbldatingusermaster order by userid desc";
$result = $db->query($sql);
while($data = $result->fetch_object()){
echo $data->education; //My value is 25,26
}
Thanks.
You can write html tags to page with echo, you know that. So basicly it will be something like this:
while($data = $result->fetch_object()){
echo '<input type="checkbox" name="check" value="'.$data->educationid.'">'.$data->educationid.'<br>';
}
I think the piece of the puzzle that you were looking for is perhaps explode to create an array of integers from the given string $data->educationid (ie: 25,26 )
$db = new mysqli("localhost","paroshic_paroshic","kxmcwQzLTrTR","paroshic_matri2018jl");
$sql = "select * from tbldatingusermaster order by userid desc";
$result = $db->query( $sql );
if( $result ){
while( $data = $result->fetch_object() ){
/* explode the string into little integers */
$ids=explode( ',', $data->educationid );
/* iterate through the pieces and generate an input[checkbox] element */
foreach( $ids as $id )printf('<input type="checkbox" name="UNKNOWN[]" value="%s" />',$id);
}
}
Good day. So I have a program that will fetch a set of rows from MySql depending on the select option that I clicked in a select option in HTML and display it into a container in HTML. The code works on the first one, but somehow on the 2nd one it does not fetch the rows I requested. I already checked if the column names are correct and if the value for the select option matches the "if statement" in my PHP code.
In the first snippet of codes, it works properly,
if($_POST["FilterDoc"]=="document_type")
{
$result=mysqli_query($conn,"SELECT * FROM records ORDER BY document_type ASC");
$data = "";
while($rows = mysqli_fetch_assoc($result)){
$document_type = $rows['document_type'];
$date_received = $rows['date_received'];
$application_no = $rows['application_no'];
$hei = $rows['hei'];
$school_name = $rows['school_name'];
$from_co = $rows['from_co'];
$other_govt = $rows['other_govt'];
$contact_person = $rows['contact_person'];
$comment = $rows['comment'];
$program = $rows['program'];
$year_level = $rows['year_level'];
$academic_year = $rows['academic_year'];
$transaction_no = $rows['transaction_no'];
$output = "<table><tr><td>$document_type</td><td>$date_received</td><td>$application_no</td><td>$hei</td><td>$school_name</td><td>$from_co</td><td>$other_govt</td><td>$contact_person</td><td>$comment</td><td>$program</td><td>$year_level</td><td>$academic_year</td><td>$transaction_no</td></tr></table><br>";
$data .= ''.$output.'';
}
echo $data ;
}
But on the 2nd code even though it has the same code structure, doesn't work as I'd hoped to be,
else if($_POST["FilterDoc"]=="other")
{
$result=mysqli_query($conn,"SELECT * FROM records ORDER BY other_govt ASC");
while($data = mysql_fetch_assoc($result))
$data = "";
while($rows = mysqli_fetch_assoc($result)){
$document_type = $rows['document_type'];
$date_received = $rows['date_received'];
$application_no = $rows['application_no'];
$hei = $rows['hei'];
$school_name = $rows['school_name'];
$from_co = $rows['from_co'];
$other_govt = $rows['other_govt'];
$contact_person = $rows['contact_person'];
$comment = $rows['comment'];
$program = $rows['program'];
$year_level = $rows['year_level'];
$academic_year = $rows['academic_year'];
$transaction_no = $rows['transaction_no'];
$output = "<table><tr><td>$other_govt</td><td>$document_type</td><td>$date_received</td><td>$application_no </td><td>$hei</td><td>$school_name</td><td>$from_co</td><td>$contact_person</td><td>$comment</td><td>$program</td><td>$year_level</td><td>$academic_year</td><td>$transaction_no</td></tr></table><br>";
$data .= ''.$output.'';
}
echo $data;
}
Here's the HTML code for my select option,
<select name = "FilterDoc" onchange = "filterby(this);">
<option selected disabled>Filter By</option>
<option value="document_type">Document Type</option>
<option value="date">Date</option>
<option value="hei">HEI</option>
<option value="other">Other Govt.</option>
<option value="person">Person</option></select>
The column name from my database which is other_govt isn't the primary key and has duplicate values. I would really appreciate your help. Thank you!
Run the query SELECT * FROM records ORDER BY other_govt ASC from the MySQL CLI or PHPMyAdmin to make sure it works. Check the error codes from mysqli_query().
This loop is removing all of your result rows silently:
while($data = mysql_fetch_assoc($result))
$data = "";
So that's an error.
You may find it easier to use the extract() function to pull all of the $row values into local variables. Or use mysqli_fetch_ojbect($result) and then use ...<td>{$rows->document}</td>... to generate the output.
Also usually a good idea to escape strings you output to HTML with htmlspecialchars(). Assuming associative array:
Your code will generate a table for every row, probably not want you want. Put the <table> tags outside of the loop, the <tr> inside the loop for each row, then you can iterate over the column values:
foreach ($rows as $col => $val) {
$output .= '<td>'.htmlspecialchars($val).'</td>';
}
You can get the columns in the desired order for the table by specifying them in the SELECT instead of using SELECT *.
All your functional codes are looking fine. But, You have called
while($data = mysql_fetch_assoc($result)) in the second code.
I think mysql_fetch_assoc can't be called twice.
Remove that unwanted line and check it.
You have an extra line of code just right after your query:
while($data = mysql_fetch_assoc($result))
Try the following
$mQuery = "";
$Filter = $_POST["FilterDoc"];switch ($Filter) {
case "document_type":
$mQuery = "your query for document type";
break; case "other":
$mQuery = "your query for other type";
break; } if (!empty($mQuery)) { {
$result = mysqli_query($conn, $mQuery);
$data = "";
while ($rows = mysqli_fetch_assoc($result)) {
$document_type = $rows['document_type'];
$date_received = $rows['date_received'];
$application_no = $rows['application_no'];
$hei = $rows['hei'];
$school_name = $rows['school_name'];
$from_co = $rows['from_co'];
$other_govt = $rows['other_govt'];
$contact_person = $rows['contact_person'];
$comment = $rows['comment'];
$program = $rows['program'];
$year_level = $rows['year_level'];
$academic_year = $rows['academic_year'];
$transaction_no = $rows['transaction_no'];
$output = "<table><tr><td>$document_type</td><td>$date_received</td><td>$application_no</td><td>$hei</td><td>$school_name</td><td>$from_co</td><td>$other_govt</td><td>$contact_person</td><td>$comment</td><td>$program</td><td>$year_level</td><td>$academic_year</td><td>$transaction_no</td></tr></table><br />";
$data.= '<a href = "editdoc.php?v=' . $transaction_no . '" name="documents">' . $output . '</a>';
}
echo $data;
}
else {
echo "Unknown Filter Request";
}
I have been working on this for hours on end trying to split the resultant mysql query into their usable variables and I just don't know what I am doing incorrectly. I no longer get the resource ID# but now all I get is "array" from the variables when I print_r. I am trying to get the 3 fields from the select statement in their own array and use the values. I have been pulling my hair out trying to get this to create usable data. Any help is greatly appreciated as I feel I could spend many, many more hours tinkering and just seem to be on the wrong track.
$Query = "SELECT guardian.Email, child.CFName, guardian.FName
FROM child
INNER JOIN uaccount ON child.ANum = uaccount.ANum
INNER JOIN guardian ON guardian.ANum = uaccount.ANum
WHERE uaccount.ANum = '$ANum';";
$result = mysql_query($query);
$test = mysql_fetch_array($result, MYSQL_BOTH);
print_r('this is the test '.$test."<br/>");
while($row = mysql_fetch_assoc($test, MYSQL_BOTH))
{
print_r('this is the row '.$row."<br/>");
foreach($row as $rows)
{
$info = mysql_fetch_assoc($rows, MYSQL_BOTH);
$Email = $info['0'];
$FName = $info['1'];
$CFName = $info['2'];
}
}
Thank you very much for your assistance thus far. With a combination of everyone's help I have been able to pull the values from the first level of the array with the following code :
$query = "SELECT guardian.Email, guardian.FName, child.CFName
FROM guardian
INNER JOIN uaccount ON child.ANum = uaccount.ANum
INNER JOIN guardian ON guardian.ANum = uaccount.ANum
WHERE uaccount.ANum = '$ANum';";
$result = mysql_query($query);
$info = mysql_fetch_array($result);
$Email = $info['0'];
$FName = $info['1'];
$CFName = $info['2'];
However, that is only returning me 1/3rd of the data as each account has 3 guardian email addresses related to it. Where I should be getting about 2000 rows returned, I am only getting 670. That is why I was nesting another layer inside the orginal posting and why I was attempting to pull a fetch_assoc from itself. If you cannot pull an array from itself, how do you "de-nest" so to speak? Any and all help is greatly appreciated.
$emQuery = "SELECT guardian.Email, child.CFName, guardian.FName
FROM child
INNER JOIN uaccount ON child.ANum = uaccount.ANum
INNER JOIN guardian ON guardian.ANum = uaccount.ANum
WHERE uaccount.ANum = '$ANum'";
$result = mysql_query($query);
$Email = array();
$FName = array();
$CFName = array();
$test = mysql_fetch_array($result, MYSQL_BOTH);
print_r('this is the test '.$test."<br/>");
while($row = mysql_fetch_assoc($test, MYSQL_BOTH))
{
echo 'this is the row ';
print_r($row);
echo '<br/>';
//$info = mysql_fetch_assoc($rows, MYSQL_BOTH);
$Email[] = $info['Email'];
$FName[] = $info['FName'];
$CFName[] = $info['CFName'];
}
To the best of my understanding this is what you are trying to do.
Some highlights:
print_r can only get a reference to a valid php array, no strings included.
mysql_fetch_assoc fetches a line out of a result set referenced by a variable, and then moves the reference to point to the next row.
You cannot call this function on a the result of it self, as it is not valid syntax.
In general, you'll be better off using PDO or mysqli_ functions at least, as it is by far more secure, by allowing you to use parameter binding instead of just using use input as part as your SQL.
You should just loop through the rows of your result once like so:
$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result)) {
// print_r($row); Debug here if you want.
$Email = $row['Email'];
$FName = $row['FName'];
$CFName = $row['CFName'];
}
Note: You were providing a second parameter to mysql_fetch_assoc() which only takes one parameter (the result from a query). See the doc here.
mysql_fetch_array() takes another parameter that specifies what type of array to return.
Generalized PDO example out of a project of mine ($sql would be your $Query string):
$qry = $pdo->query($sql);
$all=array();
$idx=0;
while($fields=$qry->fetch( 'PDO::FETCH_ASSOC' )){
$all[key($fields)][$idx]=$fields[key($fields)];
while(next($fields)!==false){
$all[key($fields)][$idx]=$fields[key($fields)];
}
$idx++;
}
The $all variable will hold an array which in turn holds an array for each of the columns you've selected.
Like that you can do neat things like array_combine($all['Email'],$all['Fname']) and you'd get an array where the key is the Email column and the value would consist of the Fname column.
I have a basic PHP function that I am working with. Sometimes, it is passed an array of variables, and other times it is just passed one variable. So, currently, I have something like this:
<?
function do_this($user_id_array) {
$user_ids = array();
foreach ($user_id_array as $single_user_id) {
$sql = 'SELECT username FROM users
WHERE id = $single_unit';
while($row = mysql_fetch_assoc($result)) {
array_push($user_ids, $row['id'];
}
}
return $user_ids;
}
?>
My issue is this: If I call the function and send it only one variable (and not an array), it (obviously) gives me the following error: Invalid argument supplied for foreach()
My question is: How can I change this function in the most efficient way with the least amount of code? Do I have to use an if is_array() statement and just create 2 SELECT statements, one for each case (array and non-array)?
Many thanks!
I see several options:
Pass an array even if it's one element long
Test for is_array() and act accordingly
Add another argument which states whether to check for an int or an array.
I'd go with options 1 or 2, as option 3 is error prone.
Also, there might be a better solution to your problem, you shouldn't have a single query for every user, you should instead use the IN keyword in MySQL, something like this:
$users = (is_array($user_id_array)) ? implode(',',$user_id_array) : $user_id_array;
$query = "SELECT `username` FROM `users` WHERE `id` IN({$users})";
wow. that's a lot of queries. What about to delete foreach and do something like
if (is_array($user_id_array)){
$sql = 'SELECT username,id FROM users
WHERE id IN ('.implode(",", $user_id_array).')';
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result)){
$users[$row['id']] = $row;
}
}
You can write:
$user_id_array = (array)$user_id_array;
if (!is_array($user_id_array))
$user_id_array = array($user_id_array);
function do_this($user_id_array) {
$ids = array_map('intval', (array)$user_id_array);
$sql = 'SELECT username FROM users
WHERE id IN(' . implode(',', $ids) . ')';
$result = mysql_query($sql);
$usernames = array();
while ($row = mysql_fetch_assoc($result)) {
$usernames[] = row['id'];
}
return $usernames;
}
First line makes sure that you have an array ((array)$user_id_array) and that all values are valid integers. Then a single SQL query is executed for all user ids.