I'm having an issue with converting an array to individual strings and outputting the strings. I'm trying to store each converted string into it's own variable and output each variable to the string using an echo statement. I have already set up a mysqli connection. $connection is the mysqli connection. I've already tried using serialize() and implode() functions. There are four database colums (id, name, email, phonenumber). I commented out some code so you can see what I've been trying. I still can't seem to figure it out. I've included my code below. Thanks.
function get_random_info($connection)
{
$temp = mysqli_query($connection,"SELECT RAND() * FROM distributors LIMIT 1");
$info = $temp;
return $info;
}
//$random_distributor = serialize(get_random_info($con));
//$random_distributor = implode(get_random_info($con));
$random_distributor = $info;
//TEST WITH print_r
print_r ($random_distributor);
//$random_id = $random_distributor[0];
//$random_name = $random_distributor[1];
//$random_email = $random_distributor[2];
//$random_phonenumber = $random_distributor[3];
// TEST OUT INFORMATION. DISPLAY TO SCREEN.
echo "Random name is" . $random_name. "His id in the database is" . $random_id . ".
His email address is {$random_email} and his phone number is {$random_phonenumber}";
these are all commented out so it couldn't possibly work.
//$random_id = $random_distributor[0];
//$random_name = $random_distributor[1];
//$random_email = $random_distributor[2];
//$random_phonenumber = $random_distributor[3];
and you have a function that you never call
get_random_info
I figured it out with some help from you guys. Thanks for pointing me in the right direction. Below is the solution.
<?php
function get_random_info($con)
{
$random_distributor = mysqli_query($con, "SELECT * FROM distributors ORDER BY RAND() LIMIT 1");
return $random_distributor;
}
$row = mysqli_fetch_array(get_random_info($con),MYSQLI_ASSOC);
// TEST OUT INFORMATION. DISPLAY TO SCREEN.
echo "Random name is ". $row["name"] . ". His id in the database is ". $row["id"]. ". His email address is ". $row["email"]. " and his phone number is ". $row["phonenumber"];
mysqli_close($con);
?>
Related
I am trying to send a response back to my front end with the amount of days in a customers billing period. I query this number but would like to append " Days" after it. So far I have:
<?php
require "../../inc/dbinfo.inc";
$projectnum =$_POST['projectnum'];
$sql = $conn->prepare("SELECT (SELECT TermsofPayment FROM tblCustomers WHERE CUSTOMERID = ProjectCustomer) AS NetTerms FROM tblProjects WHERE PROJECTNOID = ?");
if($sql){
$sql->bind_param("i", $projectnum);
$sql->execute();
$hold = $sql->get_result();
$obj = $hold->fetch_assoc();
$addOn = " Days";
$obj->NetTerms = $obj->{'NetTerms'. $addOn};
echo json_encode($obj);
}
$sql->close();
exit();
?>
After trial and error it appears as though the second last line has no effect on the result. I've looked through the web to find no solutions to this (maybe my search didn't contain the right keywords).
Current Response: {"NetTerms":30}
Desired Response: {"NetTerms":30 Days}
You have/had two problems there:
1st you wanted to concatenate a variable to another one and had a wrong syntax.
$addOn = " Days";
$obj->NetTerms = $obj->{'NetTerms' .$addOn};
// this would try to get a value of `$obj->NetTerms Days`, which doesn't exist.
should have been
$obj->NetTerms = $obj->NetTerms . $addOn;
// or
$obj->NetTerms .= $addOn;
All of this threw an error, because $obj is an array (as return from fetch_assoc()), not an object.
So treat it as an array and it should work:
$obj['NetTerms'] = $obj['NetTerms'] . $addon;
I'm trying to pass a value for a query that takes in a variable from an earlier Sql query and then compares the result against a field from another table. But I can't seem to figure out my syntax.
$topName = $row_rsAdminDetails['fullName'] ;
$TESTqueryTwo =
"SELECT * FROM participants, admin WHERE admin.over_id = participants.fk_over_id AND participants.dr_over_names LIKE '%$topName%'";
$TESTresult2 = mysql_query($TESTqueryTwo) or die(mysql_error());
the php output I'm looking to do:
<?php
// Print out the contents of each row
while($row_TESTresultTwo = mysql_fetch_array($TESTresultTwo)){
echo $row_TESTresultTwo['userName']. " - ". $row_TESTresultTwo['Participant_Name'];
echo "<br />";
}
?>
Problem could be on this line:
while($row_TESTresultTwo = mysql_fetch_array($TESTresultTwo)){
should be
while($row_TESTresultTwo = mysql_fetch_array($TESTresult2)){
// as you have no $TESTresultTwo variable...
}
And also try with the query ... with LIKE '%".$topName."%'"
I have MYSQL table for the setting in my script , and i fetch this table in php object , i know how to print the full object by using while() , but i want to print a single value by using key .
This is MYSQL table :
setting_name setting_value
site_name Blue Box
site_email abdullah#gmail.com
template_dir defualt
language_dir english
date_format d.m.y
time_format h:m
site_logo logo.png
and this is how i'm fetching the table in object :
$query_set = "SELECT * FROM setting";
$result_set = mysql_query($query_set)
or die ("Error in query: $query_set. " . mysql_error());
$row_set = mysql_fetch_object($result_set);
i can print whole table by this code:
while($row_set = mysql_fetch_object($result_set))
{
echo "<br />";
echo $row_set->setting_name;
echo " ";
echo $row_set->setting_value;
echo "<br />";
}
but i need to print single value from the table by using key .
note : i know that i can filter from my query , but i want to select all my table to print group of values in different locations in the same page .
If you need just one value, then you could change your SQL query. Something like,
$query_set = "SELECT * FROM setting WHERE setting_name='site_name'";
If you have to use more than one values, you can try this
$data = array();
while ($result = mysql_fetch_assoc($resource)) {
$data[$result["setting_name"]] = $result["setting_value"];
}
//Now you will be able to use it like this.
echo $data["site_name"];
Doing it with objects,
$data = stdClass();
while ($result = mysql_fetch_object($resource)) {
$data->$result->setting_name = $result->setting_value;
}
// For using it.
echo $data->site_name;
You need this query, but googling is so hard nowadays. Don't forget to filter your input etc.
SELECT s.setting_name, s.setting_value
FROM settings s
WHERE s.setting_name IN ('my_key', 'my_other_setting', 'some_other_setting')
If you want only the value, remove "s.setting_name, "
I'm running a PHP query which returns several rows / columns. (Im returning the columns: name, quantity, unit, producer, notes) * X rows (depending on how many rows were found in the database).
$sql = "SELECT products.name, products.unit, lists.quantity, lists.producer, lists.notes FROM lists,products WHERE lists.familyid ='$familyid' AND lists.productid = products.id ";
$sqlmessage=mysql_query($sql);
Now i would like to arrange this response into a STRING, in order to email it using mail($to,$subject,$message,$headers).
Im trying to use the following function, however im not getting the correct list but rather alot of " fetchColumn(name) "
The Broken function:
for ($i=0; $i<mysql_num_rows($sqlmessage); ++$i){
while ($row = mysql_fetch_array($sqlmessage)){
$name = $row->fetchColumn($i);
$message .= "$name";
$message .= ", ";
}
}
What do i need to change to get the correct information out ? Been searching for a day now and trying different things without any success.'
You are using two loops (i dont know why) and object to mysql_fetch_array() .Do you mean something like this?:
while($row = mysql_fetch_array($sqlmessage))
{
$name = $row['name'];
$message .= $name;
$message .= ", ";
}
I'm trying to code an array that displays a certain set of products depending on the gender of the logged in user. The arrays not really the problem but the parts where I'm going to have to check the database then create the conditional statement from the results is the main problem i think.
Here is my code:
<?php
include"config.php" or die "cannot connect to server";
$gender=$_POST['gender'];
$qry ="SELECT * FROM server WHERE gender ='$gender'";
$result = mysql_query($qry);
$productdetails;
$productdetails1["Product1"] = "£8";
$productdetails1["Product2"] = "£6";
$productdetails1["Product3"] = "£5";
$productdetails1["Product4"] = "£6";
$productdetails1["Product5"] = "£4";
$productdetails2["Product6"] = "£8";
$productdetails2["Product7"] = "£6";
$productdetails2["Product8"] = "£5";
$productdetails2["Product9"] = "£6";
$productdetails2["Product10"] = "£4";
if (mysql_num_rows($result) = 1) {
foreach( $productdetails1 as $key => $value){
echo "Product: $key, Price: $value <br />";
}
}
else {
foreach( $productdetails2 as $key => $value) {
echo "Product: $key, Price: $value <br />";
}
}
?>
You if statement is wrong. = is an assignment operator, you should use a comparison operator like == or ===
What happens with the current code?
Some tips:
First try echoing $gender, to make sure it is getting through. It is submitted through post, what happens if nothing is being posted? Where is this coming from? You should try to use get instead. This seems like something you'd give someone a link to therefore post doesn't make sense here. You could always have both, and just get post if it exists otherwise use get otherwise default to 'male' or 'female' depending on your audience.
Next, what is your query outputting? It might be empty at this point if gender is not giving anything back. It seems like you are querying for all rows where gender = whatever was passed, but then your if statement is asking was there anything returned? Then all you are doing is going to the arrays, but you shouldn't be doing that you should be outputting what you got from the DB. Assuming you do actually have products in the table called server you should do something like this:
$products = mysql_query("SELECT * FROM server WHERE gender ='$gender");
while($product = mysql_fetch_array($products)){
echo $product['name'] . " " . $product['price']. " " . $product['gender'];
echo "<br />";
}
On that note. You should really call your table something else, like product not just "server" unless by server you mean a table filled with instances of waiters or computer hardware.