I fetched data dynamically from from MySQL using a drop down menu through Ajax which was successful but when I echo the array values, instead of giving me the list of emails it was showing just symbols.
Take a look at the image below:
From the Email List, those are the symbols that were echo out.
here is my php code
if(isset($_POST["confirm_no"])){
$d = $_POST['confirm_no'];
$query = mysqli_query($mysqli, "select * from jobseeker WHERE confirm_no LIKE '$d%'");
//Display list
if(mysqli_num_rows($query) > 0){
$row = mysqli_fetch_array($query);
foreach ($row as $r) {
$emailArr[] = $r["mails"];
}
$emails = implode(";", $emailArr);
echo $emails;
}else{
echo 'No email for this selection.';
}
}
And the jQuery
$(document).ready(function(){
$('#smode').change(function(){
var confirm_no = $(this).val();
$.ajax({
type:'POST',
data:"confirm_no="+confirm_no,
url:'get_email.php',
success:function(data){
$('#emaillist').val(data);
}
});
});
});
Why is it echoing out this symbols?
Rebuild your code as:
//Display list
if(mysqli_num_rows($query) > 0){
// You have many results - fetch them all iteratively
// use `fetch_assoc` to have ability to use `mails`
while ($row = mysqli_fetch_assoc($query)) {
$emailArr[] = $row["mails"];
}
$emails = implode(";", $emailArr);
echo $emails;
}else{
echo 'No email for this selection.';
}
You are fetching the data incorrectly, you only fetch the one row (the call to mysqli_fetch_array()
$row = mysqli_fetch_array($query);
foreach ($row as $r) {
$emailArr[] = $r["mails"];
}
Could be better written as
while( $row = mysqli_fetch_assoc($query)) {
$emailArr[] = $row["mails"];
}
Or...
$emailArr = mysqli_fetch_all($query, MYSQLI_ASSOC);
$emailArr = array_column($emailArr, "mails");
Related
I am creating a result page using php to pull data from database.
I have been able to connect the database to my webspace and displayed the data.
Now , I would like to have the page not show all the data at once. I want to add an input field example "Origen= Los Angeles" "Destination = London" and then show the results based on that criteria.
This is my first time doing something like this so if this sounds like I should know this I'm sorry. I hope I can get some help.
<?php
// include connection settings
require_once "connect.php";
// display a list of flights
if(!empty($_GET) && !empty($_GET['name'])){
$query = "SELECT * FROM Flight_Information WHERE name LIKE ''".
$_GET['name']."".$_GET['name']."'";
} else {
$query = "SELECT ID, airlineName, departureAirport, departureDate, destinationAirport FROM Flight_Information";
} // END if
$result = $db->query($query);
if ($result) {
// ... display results in while loop
while ($row = $result->fetch_assoc()) {
echo '<li>'.$row['airlineName'].' '.$row['departureAirport'].''.$row['departureDate'].''.$row['destinationAirport'].'</li>';
} // END while
} else {
// if there was an error with your query
// this will display it
echo "SQL Error: " . $db->error;
} // END if
?>
</ul>
<?php
if (!empty($_GET) && !empty($_GET['Flight_Information_ID'])) {
$Flight_Information_ID = $_GET['Flight_Information_ID'];
$query = "SELECT * FROM Flight_Information WHERE ID =" . $Flight_Information_ID;
// perform the query
if ($result = $db->query($query)) {
// check we have a result
if ($result->num_rows > 0) {
// loop through and print out the results
while ($row = $result->fetch_assoc()) {
// output a title for each result
echo '<hr/><p>You have selected FriendShipper' .$Flight_Information_ID. ':</p> <ul>';
// loop through and output details for each item
foreach ($row as $key => $value) {
echo '<li><em>'.$key.'</em> = <strong>'.$value.'</strong></li>';
}
// output a horizontal rule under the result
echo '</ul><hr/>';
}
// num_rows = 0, no results found
} else {
echo "<p>No Flights Found " .$Flight_Information_ID. "</p>";
}
// if there was an error with your query, this will display it
} else {
echo "SQL Error: " . $db->error;
}
}
?>
<?php
session_start();
$conn =new mysqli("localhost","root","","registration");
$userid=isset($_POST['userid'])?$_POST['userid']:'';
//$re['success']=false;
$sql="call regtask2('$userid')";
$res=mysqli_query($conn,$sql);
$array = array();
if($res) {
while($row = mysqli_fetch_assoc($res))
{
$array[]=$row ;
$re['success']=true;
$re['userObj']['firstname'] = $row['firstname'];
}
}
else {
$re['success']=false;
}
if(isset($_SESSION['username']))
{
$sem=isset($_POST['sem'])?$_POST['sem']:'';
$fname=isset($_POST['fname'])?$_POST['fname']:'';
$year=isset($_POST['date'])?$_POST['date']:'';
$query = mysqli_query($conn,"select * from studentdetails inner join studentmarks on studentdetails.studentid=studentmarks.studentid where firstname='$fname' and sem='$sem'");
$re = array();
while ($row = mysqli_fetch_assoc($query))
{
print_r($row);
//$options['userObj'][]=$row;
}
}
echo json_encode ($re);
return;
?>
This is my full PHP code in this I need two json responses,
1> when I refresh the page
$sql="call regtask2('$userid')";
This query has to work and pass the response to the ajax, then I am using click button. When I use click button this query has to work and pass the response
$query = mysqli_query($conn,"select * from studentdetails inner join studentmarks on studentdetails.studentid=studentmarks.studentid where firstname='$fname' and sem='$sem'");
I this is poosible?
3 options:
Just split your php code. On refresh, load script1.php and for your other ajax call, load script2.php.
You will need to set identifiers for your calls. In your ajax, add an "is_submit=true" to the query. In your php, check that value.
Assign your return value to $return and return that.
It's simple just add second query result to your previous json !, also consider adding some validation into user input to prevent sql injection
getting userid from $_POST is really bad idea
<?php
session_start();
$conn =new mysqli("localhost","root","","registration");
$userid=isset($_POST['userid'])?$_POST['userid']:'';
//$re['success']=false;
$sql="call regtask2('$userid')";
$res=mysqli_query($conn,$sql);
$array = array();
$re = array();
if($res) {
$re['success']=true;
while($row = mysqli_fetch_assoc($res))
{
$array[]=$row ;
$re['userObj']['firstname'] = $row['firstname'];
}
}
else {
$re['success']=false;
}
if(isset($_SESSION['username']))
{
$sem=isset($_POST['sem'])?$_POST['sem']:'';
$fname=isset($_POST['fname'])?$_POST['fname']:'';
$year=isset($_POST['date'])?$_POST['date']:'';
$query = mysqli_query($conn,"select * from studentdetails inner join studentmarks on studentdetails.studentid=studentmarks.studentid where firstname='$fname' and sem='$sem'");
while ($row = mysqli_fetch_assoc($query))
{
$re['userObj'][]=$row;
//$options['userObj'][]=$row;
}
}
echo json_encode ($re);
return;
?>
I'm currently making a webpage which is meant to show all it's content from the database. So I made an SQL command which selects the data needed for only 1 particular field on the webpage.
Is it possible to make the SQL command so that it get's all the content for the page at once and that ill still be able to display it separately?
If so, how? Thanks
function dbGet() {
global $conn;
global $return;
$sql = SELECT * FROM testTable;
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$return = $row["text"];
return $return;
}
}
else {
echo "0 results";
}
// $conn->close();
}
You can use in this way through which you can identify records has been there or not.
function dbGet() {
global $conn;
// I am not sure what is the datatype of $return here.
// if it's having a predefined format,
// please push the records into $return instead of creating as an array,
// which will be taken care by framework if any you are using.
// global $return;
$return = array('status' => false, records => []);
$sql = "SELECT text FROM testTable";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$return['status'] = true;
while($row = $result->fetch_assoc()) {
$return['records'][] = $row["text"];
}
}
// $conn->close();
return json_encode($return);
}
Just echo the results inside while loop instead of returning in whatever format you wish
If you are using php, you can try something like
echo $row["text"];
Hope it helps
Let me know if you require any further help
First you should fix your return code as #AbraCadaver mentioned :
$return[] = $row["text"];
Then you can use foreach in your html :
<?php
foreach($return as $r){
echo $r['text'];
}
?>
I think a Json encoding : json_encode will work well.
Try this: http://php.net/manual/pt_BR/function.json-encode.php
I have a column called favid. I am trying to pull and compare the data in that column to an existing value:
<?php $query = mysql_query("SELECT * FROM ajaxfavourites WHERE favid=$favid");
while ($row = mysql_fetch_assoc($query)) {
echo $row['favid']; };?>
I also have an existing value:
$x
But when I do something like this it doesn't work:
<?php if($row['favid'] == $x){?>
Do this...
<?php } else { ?>
Do nothing...
<?php}?>
I realize the data in the column somehow isn't pulled out. What should be done for this to work?
Try this, I assume you already connected to DB.
<?php
$x = 1;
$query = mysql_query("SELECT * FROM ajaxfavourites WHERE favid='$favid'") or die(mysql_error());
if (mysql_num_rows($query) > 0)
{
while ($row = mysql_fetch_assoc($query))
{
if ($row["existing_column_name"] == $x)
{
echo "Yes";
} else
{
echo "No";
}
}
} else
{
echo "Nothing was found";
}
?>
<?php
$x = 100500; // integer for example
$CID = mysql_connect("host","user","pass") or die(mysql_error());
mysql_select_db("db_name");
$query = mysql_query("SELECT * FROM ajaxfavourites WHERE favid='{$favid}'", $CID);
while ($row = mysql_fetch_assoc($query)) {
if (intval($row["some_existing_column_name"])==$x){
print "Is equals!";
} else {
print "Is different!";
}
}
?>
Please be informed that mysql_connect and other functions with the prefix of mysql_ is deprecated and can be removed in the next versions of PHP.
can help me?
I've a site url like:
http://www.example.com/episodio/ID
ID = row number from Database.
I want to display the page title.
I've this:
.htaccess file:
RewriteEngine on
RewriteRule ^episodio/(\w+)/?$ episodio.php?id=$1
php file:
$sql = "SELECT * FROM "episodios" WHERE serie = ".$id." AND temporada = ".$i." ORDER BY "episodio" ASC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
if ($row['episodio'] == 0) {
echo '<li><strong>Episodio doble:</strong> '.$row["nombre"].'<img src="http://www.example.net/img/play.png" class="img-play"></li>';
}else{
echo '<li><strong>Episodio '.$row['episodio'].':</strong> '.$row["nombre"].'<img src="http://www.example.net/img/play.png" class="img-play"></li>';
}
?>
<?php } ?>
You're using $_GET in your ID anywhere before this code?
I mean, $_GET['id'].
Most PHP versions (since 4...) doesn't catch the var just for its name.
I'd recommend getting all your variables from the DB first, and then printing out the HTML (perhaps from a template file). We assume that $row contains more than just an ID? Perhaps an "título del episodio", also?
if ($result->num_rows > 0) {
$episodios = array();
$x = 0;
while($row = $result->fetch_assoc()) {
$episodios[$x]['id'] = $row['id'];
if ($x == 0) {
$PageTitle = $row['title'];
}
$x++;
}
Then your template can use $PageTitle (from the first episode), and you can loop through the $episodios array to construct the links for the other episodes.