php checking array values - php

Hey all i am trying to figure out a way to see what all values are within the array:
$sql = "select * from product where type = ? limit 1";
$query = self::$__CONN__->prepare($sql);
$query->execute(array($type)) or die(print_r($query->errorinfo()));
I am new at PHP and im not sure what all that is doing to populate the query string. I am guessing that the first query is connecting to the server and sending out the query but how do i see what its returning back in the array above?
Any help would be great!

Just dump the $type variable
var_dump($type);
Or if you are interested in the array wrapper itself
var_dump(array($type));

Related

Creating an associative array to query one SQL in PHP to get several results

I am new to PHP and would appreciate any assistance in creating an associative array to query a table using SQL.
I want to use one select statement to get multiple results depending on the 'WHERE' key.
$a = $DB->getOne("SELECT DEF_VAL FROM JDEFAULTS WHERE DEF_KEY = :0", array('ip'));
$stmt1 = $DB->getOne("SELECT DEF_VAL FROM JDEFAULTS WHERE DEF_KEY = :0", array('ftpUserid'));
$c = $DB->getOne("SELECT DEF_VAL FROM JDEFAULTS WHERE DEF_KEY = :0", array('ftpPasswd'));
These codes return different values (as expected). I want to merge all of these into one line of SQL and get the values in a list depending on the DEF_KEY value.
how to create associative array from sql table
I have tried following the solutions from this but haven't been able to execute the code.
#Hashibul Hussain i havent understand your questions completly, but as far as i understood you want one query with three conditions. you can try this. this may work.
$stmt1 = $DB->getOne("SELECT DEF_VAL FROM JDEFAULTS WHERE DEF_KEY = :0 or WHERE DEF_KEY = :0 or WHERE DEF_KEY = :0", array('ip'), array('ftpUserid'), array('ftpPasswd'));
please elabrate your question further so that i can come up with better solution.

How to use strings from php array in postgresql query?

need help with using php variables in postgresql query.
I am trying to make several queries to postgresql database using php array elements, but it does't work. Can anybody help?
// $nums array with strings like - "983458999893440"
for ($i=0; $i<count($nums); $i++){
$tm = pg_escape_string($nums[$i]);
$sql = "SELECT SUM(sp.summ) FROM tk.sp_tran sp WHERE sp.long_pan ='{$tm}'";
$panSum = dbCon::conn($sql);//return result of pg_fetch_all();
//Code above don't work
//Code below is ok
$tm = pg_escape_string("983458999893440");
$sql = "SELECT SUM(sp.summ) FROM tk.sp_tran sp WHERE sp.long_pan ='{$tm}'";
$panSum = dbCon::conn($sql);//return result of pg_fetch_all();
//Please help to understand what is wrong?
I find a space symbol before first letter of the string in csv file. So code is ok. It is just my an inattention(((

php array to string conversion odbc_exec count

i'm still new towards php but after searching through multiple topics i can't seem to figure this one out.
$query2 = "SELECT COUNT(MAP_CODE2) FROM TCODE_MAPPING WHERE MAP_CODE2 = 'ABC123'";
$result2 = odbc_exec($connect,$query2);
echo $result2;
i have a query above that i'd like to use to get the total number of rows within the query that i've set, however for some reason i keep getting hit by the error
Array to string conversion in /var/www/html/xxx.php on line 85
Would highly appreciate if anyone could help out on what i'm doing wrong. Thank you!
Problem is:-
You are trying to echo a result-set object of array type.
Solution (check the code comments):-
$query2 = "SELECT COUNT(MAP_CODE2) AS MYCOUNT FROM TCODE_MAPPING WHERE MAP_CODE2 = 'ABC123'"; // given a name to the count
$result2 = odbc_exec($connect,$query2); //prepare and execute query
while (odbc_fetch_row($result2)) { //iterate over result-set object
echo odbc_result($result, "MYCOUNT"), "\n"; // echo count
}
What is happening here is you are echoing an array object type that is also a resource type and php echo only string and other primitive type variables.
so you need to use a loop to access the row or to get row count and access row just use a foreach($results as $result)
to get row count visit this sqlsrv-num-rows . on this official php link you can get more example how to fetch data.

Creating Staff Listing

I'm pretty sure I'm missing something incredibly simple. I want to create a table of my staff members using PHP/MySQL and display in an HTML table. I've gotten as far as writing the query and trying to fetch the array, but it keeps returning a bool variable. Where have I gone wrong?
<? $memberQuery = "SELECT * FROM members;";
$memberArray = mysql_fetch_array(mysql_query($memberQuery) or die("error retrieving members"));
var_dump($memberArray); ?>
The var_dump() returns bool(false), so I'm a bit lost...
You have an error in your code:
var_dump($memberArray);
instead of
var_dump($member array);
Try :
$sqlArray = mysql_query($memberQuery) or die(mysql_error ());
it will give some information about your problem.
If there is no error you will be able to visit your array with
$memberArray = mysql_fetch_query ($sqlArray);

How to assign a MySQL statement to a PHP variable

I have looked all over the web and cannot seem to find a proper way to assign the result of a MySQL query to a PHP variable. The code that I currently have returns a "resource id #3" error. Here is what it looks like.
//Select the 'aQID' of the question that has it's BOOL set to "true"
$currentQ = mysql_query("SELECT aQID FROM approvedQuestions WHERE status='1'");
$cuQ = mysql_result($currentQ,1,"status");
echo $cuQ;
I know that the query will only ever return one record (the active question). But I cannot seem to figure out what function to use.
$currentQ = mysql_query("SELECT aQID FROM approvedQuestions WHERE status='1'");
$rs = mysql_fetch_array($currentQ);
var_dump($rs);

Categories