This is my array:
When I used mysqli_query in a different script, this style worked:
while ($printTest = mysqli_fetch_array($results)) {
echo $printTest['id'] . "<br />";
echo $printTest['questions'] . "<br />";
echo $printTest['answers'] . "<br />";
}
I can't figure out how to do the same thing with a PDO created array. With PDO, I can use foreach, like this:
foreach ($queryRows as $test=>questions){
echo $test;
}
Unfortunately, that doesn't give me access to id and answers in the same loop - which is important to me.
This is how I'm getting my PDO-generated array, currently:
$queryRows = $this->dbh->query("SELECT id, questions, answers FROM qarows WHERE usr = '$this->username'");
$queryRows = $queryRows->fetchAll(PDO::FETCH_ASSOC);
How do I access all 3 columns of my array, in a loop?
It's still the same fetch rows from db. Its still the same multi dimensional structure:
$select = $this->dbh->prepare('SELECT id, questions, answers FROM qarows WHERE usr = :usr');
$select->bindParam(':usr', $this->username);
$select->execute();
$queryRows = $select->fetchAll(PDO::FETCH_ASSOC);
foreach ($queryRows as $questions){
echo $questions['id'] . '<br/>';
}
Or the look a like mysqli:
$select = $this->dbh->prepare('SELECT id, questions, answers FROM qarows WHERE usr = :usr');
$select->bindParam(':usr', $this->username);
$select->execute();
while($printTest = $select->fetch(PDO::FETCH_ASSOC)) {
echo $printTest['id'] . "<br />";
echo $printTest['questions'] . "<br />";
echo $printTest['answers'] . "<br />";
}
Related
I created a table to store the results of my sql query.
My goal is to display the first line of the table and when I click on a button it shows me the following line:
$questions = array();
$resultat = $pdo -> query("SELECT question, rep1, rep2, rep3, rep4 FROM proposition");
while($row=$resultat -> fetch(PDO::FETCH_ASSOC)){
$questions[] = array($row['question'], $row['rep1'], $row['rep2'],
$row['rep3'], $row['rep4']);
}
foreach($questions as $cle1 => $valeur1)
{
echo "personne n°:" . $cle1 . "<br />";
foreach ($valeur1 as $cle2=>$valeur2)
{
echo "Clé : ".$cle2 .", Valeur: " . $valeur2 . "<br />\n";
}
}
I have a user table that contain a lots more data, I wonder how can I improve my select code below
if ($result = $db->query("SELECT * FROM user ")) {
while ($row = mysqli_fetch_assoc($result)) {
echo $row["name"] . "<br />";
echo $row["user_id"] . "<br />";
echo $row["photo"] . "<br />";
//.. a lot more column here
}
}
I use this as a array: $user_info = mysql_fetch_assoc(mysql_query("SELECT * FROM `database` WHERE 1));
and to call it i use $user_info['column']
im having some problem here. basically, i want to compare columns. so i fetched object and the comparing results appeared just as expected. however, it does not return the compare value anymore after i added the fetch_array to view the current table hoping that the compare value would appear beside the compare value. is there any way i could run the compare code and make it appear the table? i tried a query but it would only work in MySQL and not PHP.
$query = "SELECT * FROM system_audit"; $result = mysql_query($query) or die(mysql_error());
echo " ID Type Setting Value ";
while($row = mysql_fetch_array($result)) {
echo $row['ID'];
echo $row['Type'];
echo $row['Setting'];
echo $row['Value'];
}
while ($row = mysql_fetch_object($result)) {
if($row->Setting != $row->Value) {
echo "X";
} else {
echo "O";
}
}
Your code contains a lot of echo's that have no use. I would suggest learning PHP a bit more.
Your compare is wrong, this should work :
$query = "SELECT * FROM system_audit";
$result = mysql_query($query) or die(mysql_error());
echo " ID Type Setting Value ";
while($row = mysql_fetch_array($result)) {
echo $row['ID'] . "<br>";
echo $row['Type'] . "<br>";
echo $row['Setting'] . "<br>";
echo $row['Value'] . "<br>";
if($row['Setting'] != $row['Value']) {
echo "X" . "<br>";
}
else {
echo "O" . "<br>";
}
echo "<br>";
Ive been trying to crack this for 2 hours, but something is wrong. I am very much used to doing things without mysqli but read that there is a recommended shift towards it from regular mysql commands. Hence am stuck with following:
<?php
$mysqli = new mysqli('localhost', 'admin', 'test123', 'kadmindb');
if ($result = $mysqli->query("SELECT * FROM records WHERE '$queryType' = '$keyword'")) {
while ($row = $result->fetch_object()) {
echo "<h2>Result:</h2><br>";
echo "ID: " . $row->id . "<br>";
echo "Name: " . $row->cust_name . "<br>";
echo "Invoice No: " . $row->invoice_num . "<br>";
echo "Date: " . $row->date_recorded . "<br>";
}
}
?>
This code is shown in the page where the result of the query should be displayed but nothing is displayed. I checked that both keyword and queryType variables are set and they contain the correct values. Any help would be greatly appreciated. All am trying to do is: select statement to retrieve all the details based on invoice_num submitted.
EDIT: from help I received, I was able to get this working:
$query = "SELECT * FROM records WHERE ".$queryType. " LIKE '%$keyword%' ";
if ($result = $mysqli->query($query)) {
while ($row = $result->fetch_object()) {
echo "<h2>Result:</h2><br><hr/> ";
echo "ID: " . $row->id . "<br>";
echo "Name: " . $row->cust_name . "<br>";
echo "Invoice No: " . $row->invoice_num . "<br>";
echo "Date: " . $row->date_recorded . "<br>";
echo "<hr/>";
}
}
Are you sure there's data to select? This code will only output data if there actually is.
Make sure that $queryType and $keyword are set and have sane values that will yield a result.
Use var_dump($queryType) and var_dump($keyword) immediately before the query. Now check your output. Are they both strings? Run this query directly in PHPMyAdmin and check how many rows you get.
If you can't do that try echo'ing the number of rows returned along with the query values:
if ($result = $mysqli->query("SELECT * FROM records WHERE $queryType = '$keyword'"))
{
while ($row = $result->fetch_object())
{
echo "<h1>Query WHERE '$queryType' = '$keyword' yielded {$result->num_rows} rows!</h1>";
echo "<h2>Result:</h2><br>";
...
Note, you should not have single quotes around the column ($queryType), if you insist you should use backtick quotes (`) but it's unnecessary really - if you're that pedantic you should be using prepared statements.
Also be sure to filter them for any potentially dangerous values that could allow for sql injections. See: mysqli::real_escape_string
Assuming that $queryType is the name of a column in your records table, then I believe the problem is your WHERE clause.
Rather than:
$mysqli->query("SELECT * FROM records WHERE '$queryType' = '$keyword'")
You should have:
$mysqli->query("SELECT * FROM records WHERE {$queryType} = '{$keyword}'")
Note that I've removed the single quotes around $queryType and have used complex (curly) syntax
Also, in the future you might want to try using an else block to trap errors:
$mysqli = new mysqli('localhost', 'admin', 'test123', 'kadmindb');
if ($result = $mysqli->query("SELECT * FROM records WHERE {$queryType} = '{$keyword}'")) {
while ($row = $result->fetch_object()) {
echo "<h2>Result:</h2><br>";
echo "ID: " . $row->id . "<br>";
echo "Name: " . $row->cust_name . "<br>";
echo "Invoice No: " . $row->invoice_num . "<br>";
echo "Date: " . $row->date_recorded . "<br>";
}
}
else
{
echo "Error: " . $mysqli->error;
}
Below is a function in PHP I created which returns some product information in an array. On the web page where I am calling this function I want to be able to specify exact array elements for example just the product description ($result2['itm_desc']). Please can someone point me in the right direction as to do this. I would assume you call a function like fetch array but i'm not quite sure how to execute this.
public function getAllReelImages(){
$sql = "SELECT id FROM $this->table3 WHERE itm_cat = 2 ORDER BY id ASC";
echo "<br /><br />";
$stmt = mysqli_query($this->connection, $sql);
/*fetch values*/
while($result = mysqli_fetch_array($stmt)){
echo "<br /><br />";
$sql2 = "SELECT itm_details.id,itm_details.itm_make,itm_details.itm_model,itm_details.itm_desc,itm_pic_detail.itm_pic_name, itm_value.itm_sale_price
FROM
itm_details, itm_pic_detail, itm_value
WHERE
itm_details.id = {$result['id']} AND itm_pic_detail.id = {$result['id']}
AND itm_value.id = {$result['id']} ORDER BY id ASC";
$stmt2 = mysqli_query($this->connection, $sql2);
while($result2 = $stmt2->fetch_array()){
echo ($result2['id']); echo"<br />";
echo ($result2['itm_make']); echo"<br />";
echo ($result2['itm_model']); echo"<br />";
echo ($result2['itm_desc']); echo"<br />";
echo ($result2['itm_sale_price']); echo"<br />";
echo "<img src='$this->dir"."{$result2['itm_pic_name']}'><br />";
echo"<br />";
echo $value;
}
}
}
First of all.. Your code is pretty bad. I made it little bit more clear. For example: echo ($result2['id']); echo"<br />"; TO-> echo $result2['id'] . '<br />'; (for future references.)
They way your code is at the moment. You wont add anything to an array, since you echo them right away. I made my version, that would first create the array and then later you can use that array..in any way you want :)
And doesn't the ORDER BY use by default as ASC?
I also changed the first query more dynamic.
NOTE: I'm not very good with mysql table joining, somebody must validate the second query. But in my eyes, its absolutely incorrect. However, I'm posting this in the reference for creating an array with a function and then displaying it.
NOTE2: I hope your question was about PHP o.0
# This should be in some class ?!
function getAllReelImages ($category) {
$sql = "SELECT * FROM `" . $this->table3 . "` WHERE `itm_cat` = '" . $category . "' ORDER BY `id` ASC";
$stmt = mysqli_query($this->connection, $sql);
while($result = mysqli_fetch_array($stmt)) {
$sql2 = "SELECT `itm_details.id`, `itm_details.itm_make`, `itm_details.itm_model`, `itm_details.itm_desc`, `itm_pic_detail.itm_pic_name`, `itm_value.itm_sale_price` FROM `itm_details`, `itm_pic_detail`, `itm_value` WHERE `itm_details.id` = '" . $result['id'] . "' AND `itm_pic_detail.id` = '" . $result['id'] . "' AND `itm_value.id` = '" . $result['id'] . "' ORDER BY `id` ASC";
$stmt2 = mysqli_query($this->connection, $sql2);
while($result2 = $stmt2->fetch_array()){
$returns[$result2['id']]['id'] = $result2['id'];
$returns[$result2['id']]['itm_make'] = $result2['itm_make'];
$returns[$result2['id']]['itm_model'] = $result2['itm_model'];
$returns[$result2['id']]['itm_desc'] = $result2['itm_desc'];
$returns[$result2['id']]['itm_sale_price'] = $result2['itm_sale_price'];
$returns[$result2['id']]['itm_pic_name'] = $this->dir . $result2['itm_pic_name'];
}
}
return $returns;
}
# Display the results:
echo '<br /><br />';
foreach (getAllReelImages(2) as $img_id => $img_value) {
echo $img_id . '<br>';
echo $img_value['itm_make'] . '<br>';
echo $img_value['itm_model'] . '<br>';
echo $img_value['itm_desc'] . '<br>';
echo $img_value['itm_sale_price'] . '<br>';
echo $img_value['itm_pic_name'] . '<br>';
echo '<hr>';
}
echo '<br />';