Pass PHP Variable into WHERE/AND MySQL Query - php

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."%'"

Related

Php variable increment

So i have the following code that works and outputs the information let's say 2 11. I would like to have $keyidcall increment in variable name so $keyidcall2, $keyidcall3....... with the additional variables I would like the variable to hold the correct value so I can call it later. so expected output would be $Apiammount ="2";
echo $keyidcall; would echo 2
echo $keyidcall: would echo 11
while($Apiammount > 1){
$Keyidquery = mysqli_query($connection, "SELECT ID FROM `Characterapi` WHERE UserId = '$Idcall'");
while($keyid = mysqli_fetch_assoc($Keyidquery)){
$keyidcall = $keyid['ID'];
echo $keyidcall;
}
$Apiammount--;
}
The better way to do this would be to store the values in an array.
$keyidcall[] = $keyid['ID'];
Then you can refer to them later as
echo $keyidcall[0];
echo $keyidcall[1];
in the order that they were entered in.
Or if you wanted something more specific to refer to it by, you could use
$keyidcall[$Apiammount] = $keyid['ID'];
then you would refer to them as:
echo $keyidcall[<apiamount>];
Assuming you know what that would be.
You can do:
$i = 0;
while($foo){
$name = 'keyidcall';
$i++;
$newvar = $name . $i;
echo $$newvar;
}
And $$newvar will echo the value of "keyidcallX" where X is an incrementing value. Not sure if that's what you meant.

PHP: display items from array to string NOT working

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);
?>

Print single value from object in php

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, "

"Double" evaluation in PHP

Morning all!
My MySQL query returns a PHP variable name which I'd like to "double" evaluate when I output it. It may be best for me to do this by example.
On the static resources page I have:
$page1 = "test1.php";
$page2 = "test2.php";
And my MySQL table called tbl_pages contains 2 entries under fld_pagename: $page1 and $page2. That way, I figure that I only define $page1 and $page2 once.
Now, on the page I'm working on, I have a query which returns the name of the page:
$query = mysql_query("SELECT fld_pagename FROM tbl_pages WHERE fld_show = 1 ORDER BY fld_ref");
while($row = mysql_fetch_array($query)){
$nameofpage = $row['fld_pagename'];
echo "$nameofpage",'<br />';
}
However, the double quotes around the variable only evaluate the variable to one level. Its output is:
$page1<br />$page2<br />
But I want these variables to be evaluated before output:
test1.php<br />test2.php<br />
Any thoughts much appreciated!
If I get you correctly, you can use this PHP feature (example):
$test1=1234;
$test2='test1';
echo $$test2; // echoes 1234
Or, to make it compatible with your system...
$nameofpage=substr($nameofpage,1); // remove dollar
echo $$nameofpage.'<br/>';
If you're after evaluating the pages you've referenced, then this would work:
$query = mysql_query("SELECT fld_pagename FROM tbl_pages WHERE fld_show = 1 ORDER BY fld_ref");
while($row = mysql_fetch_array($query)){
$nameofpage = $row['fld_pagename'];
include($nameofpage);
echo '<br />';
}
If you only store variable name without $ you can use variable of variable feature of PHP
echo $$nameofpage,'<br />';
can remove $ from input stored
$nameofpage=substr( $nameofpage, 1 );
echo $$nameofpage,'<br />';
it seems your database design is wrong.
There is not a single reason to store PHP variable names in the database.
Store pagenames itself and you'll be okay

parse_str problems

I’m sorting a list and using Ajax to update a database. I need help parsing the string. This is the query string that I need to parse:
images_list[]=32&images_list[]=95&images_list[]=97&images_list[]=96&images_list[]=102&images_list[]=103&images_list[]=99&images_list[]=101&images_list[]=98&john=hi
I have put john=hi to test to see if the string is actually being sent via Ajax to the processor.php file. I am able to get the variable john=hi from the URL, so the query string is being sent perfectly fine. This is my code so far, but I can't seem to access the data I need, it appears as if nothing is there:
<?php
// Connect to the database
require_once('connect.php');
parse_str($_GET['images_list']);
for ($i = 0; $i < count($images_list); $i++) {
$id = $images_list[$i];
mysql_query("UPDATE images SET ranking = '$i' WHERE id = '$id'");
echo $images_list[$i];
}
?>
$_GET['images_list'] is an array of integers. There's nothing to parse there. PHP already did it for use. So, skip the parse_str part and easily use $_GET['images_list'] instead of $images_list.
Whole code:
<?php
//Connect to DB
require_once('connect.php');
foreach ($_GET['images_list'] as $i => $id) {
mysql_query("UPDATE images SET ranking = " .
mysql_real_escape_string($i) .
" WHERE id = " .
mysql_real_escape_string($id));
echo $id;
}
?>

Categories