I've created this thing to get a TEXT value from my database.
$query1 = "SELECT * FROM `ma_apps` WHERE `assignedto`='$_SESSION[username]'";
$result1 = mysql_query($query1) or die(mysql_error());
$Appdata = mysql_fetch_array($result1) or die(mysql_error());
and then:
$app_content = $Appdata['1'];
echo $app_content;
but it echos and says nothing, although it's the right name of the key. And I already checked if I can get another value from the database from a key that is considered as "TINYINT", and it works well. But I fail to echo/get the "TEXT" keys from my database.
Thanks.
i think you want to access to the second value in the array.
try this
$Appdata[1];
otherwise use mysql_fetch_assoc to create a array with the field name as key
If your string resembles an HTML tag then it's going to be treated like an HTML tag and not show as a string on the screen. It's worth checking view source on your browser to see if your string starts with a <.
$query1 = "SELECT * FROM `ma_apps` WHERE `assignedto`='{$_SESSION['username']}'";
Also,
$Appdata[1];
try echo $Appdata[0]; because you are asking for a column with name '1', vs asking for the index 1
You should do
$query1 = "SELECT * FROM `ma_apps` WHERE `assignedto`='".$_SESSION[username]."'";
and
$app_content = $Appdata[0];
var_dump($Appdata) check the values inside the variable and use it properly. Index is without '.
you can also just echo everything from the result set with:
<?php
mysql_data_seek($result1,0);
while ($row = mysql_fetch_array($result1)){
foreach ($row as $key => $value){
echo $key." - ".$value;
}
}
?>
Then you will see the key value pairs
Related
I have this text string varchar type i get from database echo.
how to extract it and count the value :
in the database it save as string ["1","2","3"] in varchar.
how to count this as 3 item using php?
my sql code is :
$sql2 = "SELECT * FROM il_dcl_stloc1_value WHERE record_field_id = '$exc_prt_id'";
$result2 = $conn->query($sql2);
while($row2 = $result2->fetch_assoc()) {
$exc_prt_id1 = $row2['value'];
echo "$exc_prt_id1"; // this will result ["1","2","3"]
}
There are tons of ways you can do this, but if you just care about the count, not the values, and they will always come in this format, you can just use
$count = count(explode(",", $exc_prt_id1));
You can also do a
$count = count(json_decode($exc_prt_id1));
If you want the values, run the above code without the count.
$arr = json_decode($exc_prt_id1);
This will result in a PHP array.
While the 2nd option is generally preferred and is better practice, the 1st one might be of use in certain cases. If there is anything unclear, just ask :)
Try this way
$exc_prt_id1= str_replace("[","",$exc_prt_id1);
$exc_prt_id1= str_replace("]","",$exc_prt_id1);
$exc_prt_id1= str_replace('"','',$exc_prt_id1);
$invoiceArr = explode(",",$exc_prt_id1);
$count = count(explode(",", $invoiceArr ));
As you can see from the code below im very new to PHP, so please excuse the newbie question but ive been struggling with this all afternoon and just cant figure out what exactly is the problem.
Basically Ive got 3 dropdown menus for team1, team2 and venue.
When user selects teams and venue an isset function is triggered and I extract the result from the game out of my database.
As you can see in the code below I use 3 echo statements to test if correct values from dropdown menus has been captured. When I open the page the echo statements confirm the correct results are captured from the drop-downs, I go on and query the database with the variables I echoed above.
The Problem
In the while loop, I want to echo the results from the query BUT nothing gets displayed. I tried doing a var_dump to see the contents of the variables but NOTHING gets displayed. What am I doing wrong?
foreach($_REQUEST["team1"] as $teamone){
$team1 = $teamone;
}
foreach($_REQUEST["team2"] as $teamtwo){
$team2 = $teamtwo;
}
foreach($_REQUEST['venue'] as $venue){
$venue = $venue;
}
//These echo statments are a test to see if the correct dropdown values has been captured
echo $team1.'<br>';
echo $team2.'<br>';
echo $venue;
//Use results from dropdown menu to query database
$result = mysql_query('SELECT *
FROM `results`
WHERE `hometeam` = "$team1" && `awayteam` = "$team2"') or
die(mysql_error());
while($row = mysql_fetch_array($result)){
echo $row['awayteamscore'];
echo $row['hometeamscore'];
}
If anyone can point me in the right direction it would be greatly appreciated.
I suspect you're receiving sql syntax error for two reasons at least
First when you want to use the variable value within string you must use double embracing quotes
$sql="select * from mytable where col1='$my_var'";
And second: SQL expects string values ebraced with simgle quotes as I pointed above. In your case you use "" quotes.
And one more recommendation. When debugging php app it might be useful to enable extended output by inserting
error_reporing(E_ALL);
somewhere in the beginning of php script
you mixed between double and single quotes . while variables shouldnt be rounded by double quotes.
$result = mysql_query(" SELECT *
FROM `results`
WHERE `hometeam` = '$team1' && `awayteam` = '$team2' ") or
die(mysql_error());
while($row = mysql_fetch_array($result)){
echo $row['awayteamscore'];
echo $row['hometeamscore'];
}
You put your SQL query in single quotes and variables do not work in there. Try this:
<?php
$var = "stuff";
echo '$var';
echo "$var";
?>
What you can do instead is to concatenate strings like this:
$result = mysql_query('SELECT *
FROM `results`
WHERE `hometeam` = "'.$team1.'" && `awayteam` = "'.$team2.'"')
My Friend Your code is just fine you just need to check that your submiting the dropboxes with the form so first check your html and when you var_dump($_REQUEST) it should be an array containing all the values that you trying to gather
Your foreach loops are overriding the values. It would be best to put it in an array like so
$team1 = array();
foreach($_REQUEST["team1"] as $teamone){
$team1[] = $teamone;
}
if you need to make the values of $team1 into a string then this would be more appropriate
$team1 = "";
foreach($_REQUEST["team1"] as $teamone){
$team1 .= $teamone;
}
Fixing the above first might help. If not then maybe the problem is in your db queries.
//Using double quotes
"SELECT * FROM `results` WHERE `hometeam` = '$team1' && `awayteam` = '$team2'"
//Using single quotes
'SELECT * FROM `results` WHERE `hometeam` = "'.$team1.'" && `awayteam` = "'.$team2.'"'
Then display the result
while($row = mysql_fetch_array($result)){
echo $row['awayteamscore'];
echo $row['hometeamscore'];
}
I am having a few issues concatenating a string and then outputting the value.
In the database I have a column titled 'featured_price'. In that column is specified another column that will hold the value I am trying to acquire, however, I need to append '_term' to the end and then get the column from the database.
So I need to have two outputs:
1) featured_price
2) specified_column_term
This is my PHP:
$rows = $wpdb->get_results("SELECT * FROM specified_details WHERE id='$id'");
foreach ($rows as $result) {
echo $result->featured_price;
$featuredprice = $result->featured_price;
$featuredterm = $featuredprice. "_term";
echo $result->$featuredterm;
}
When I var_dump() I just get this array(0).
You may have a mistake there on line
echo $result->$featuredterm;
Shouldnt it be
echo $rows->$featuredterm;
?
Just wondering if anyone can help me with this problem...
I want to be able to check if a certain ID (user for example is logged on) The ID's of these users will be pulled from a table, and then fed into an array and then checked if it is in there.
However when I pull the data the if inarray() no longer works as it would if I just typed it in straight within the code instead of pulling it.
I want to pull approved ID's through so they can access a certain link essentially!
Any help would be appreciated! Thanks!
<?php
mssql_select_db("$ins", $con);
$result = mssql_query("SELECT ID FROM Event WHERE EventPublic LIKE 'Yes' AND EventDate >= GETDATE() -1 ORDER BY EventDate ASC ");
while($row = mssql_fetch_array($result))
{
$test = "". $row['ID'] .",";
}
$tests = explode(',', $test);
if (in_array("2, 48", $tests)) {
echo "WOO";
}
else
{
echo "BOO";
}
mssql_close($con);
?>
in array should be an actual array
in_array(array('2', '48'), $tests)
also, it would be better to put
$tests = array();
before the while loop, then change the code inside the loop to read
$tests[] = $row['ID'];
then you can do away with the '$tests = explode(...' line.
Your having the issue with your in_array test because you are comparing an array and with a string in the following line:
if (in_array("2, 48", $tests))
The above line checks if the string "2, 48" exist in the array. But the array will have 2 as one element and 48 as a separate element. To fix this you can search for each element individually with something like:
if(in_array('2',$tests) || in_array('48',$tests))
I have this code.
<?php
require('connection/conn.php');
mysql_select_db($db_name,$ligação);
//$rsArticle = mysql_query("CALL get_article(1,518)");
$rsArticle = mysql_query("SELECT * FROM tblarticles WHERE ArticleID = 518");
while($rowArticle = mysql_fetch_array($rsArticle)){
echo $rowArticle;
}
?>
And instead of getting the text that exists in the database I just get the word: Array
The line that is commented its for calling a stored procedure. In a desperate measure I made a simple select, in the next line
Can anyone explain me what I'm doing wrong??
Thanks
The reason that you are getting the word Array is because what you are echoing is an Array. Use something like echo $rowArticle['column-name']; to echo the data from a specific column of your query.
You can't echo an array. try print_r() instead
if you want the values individually, do something like this:
while ($row = mysql_fetch_array($rsArticle, MYSQL_NUM)) {
//echo the first column of the record (index 0)
echo $row[0];
}
look in the php.net documentation for more info
mysql_fetch_array returns an array with elements for every field in your database table.
Either print the whole array with print_r() or use echo $rowArticle[COLOUMN_NAME] to echo certain values from your resultset.
mysql_fetch_array() returns you the array, when you use echo is actually return the object type in case of array.
use print_r($rowArticle); instead of echo.