<?php
require('db.php');
$_SESSION['sloggedIn']="yes";
$data1['first_name'] = $_SESSION['sfirstname'];
$data1['email'] = $_SESSION['semail'];
$sqlQuery = "SELECT first_name, email FROM otium where id = :id";
file_put_contents('log/DBErrors.txt', 'Connection: '.'rivi8'.$sqlQuery."\n", FILE_APPEND);
$kysely1 = $DBH->prepare($sqlQuery);
$kysely1->execute($data1);
// Loop the recordset $rs
// Each row will be made into an array ($row) using mysqli_fetch_array
//while($row = mysqli_fetch_array($rs)) {
// Write the value of the column FirstName (which is now in the array $row)
echo $sqlQuery['first_name'] . "<br />";
echo $sqlQuery['email'] . "<br />";
//}
?>
First time doing this php and i would like to print out user's logged in data, i have this code but is not working! please help
$sqlQuery is just string variable containing query, you can't expect it to be a array.
I guess you already got firstName and email from session then there is no need of executing sql query, just printing the session will make your job done.
echo $_SESSION['first_name'] . "<br />";
echo $_SESSION['email'] . "<br />";
I am just running a SQL query to fetch a particular data from the database and would like to display in horizontal way inspite of the vertical format, Can we do something to display the data as required.
For Getting the clear understanding of the question i have attached the Output which i am getting and the output which i required after the SQL query is executed.
Thanks in Advance.
Query:
<?php
include 'connect-db.php';
$query = "SELECT distinct work_component FROM daily_progress_demo WHERE package_no='$package_no'";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
echo "<center>".$row['work_component']."</center>";
echo "<br />";
}
?>
you can do like this
echo "<table>"
echo "<tr>"
while($row = mysql_fetch_array($result)){
echo "<td>".$row['work_component']."</td>";
}
echo "</tr>";
echo "</table>";
echo $row['work_component']."<br>";
I'm really stuck on this guys, and I know where the fault is but I can't seem to fix it.
The while loop itself works it's the if that is causing all the trouble.
Whenever there are 3 groups in the database it only displays 2 the strange part it will only display the items in the while loop. But the first item is somehow tied to the if statement.
public function get_group($user_id){
$sql3="SELECT * FROM groups WHERE user =
$user_id";
$results = mysqli_query($this->db, $sql3);
$user_data = mysqli_fetch_array($results);
if ($results->num_rows > 0) {
// output data of each row
while($row = $results->fetch_assoc()){
echo "hello";
echo "group :" . $row["group"] . "<br>";
echo "groupdesc:" . $row["groupdesc"] . "<br>";
echo "<a class='btn btn-primary' href='project.php?groupid=" . $row["groupid"] . "'>></a>";
}
}
else {
echo "0 results";
}
}
It seems it can't output the first one the first one just keeps hidden.
You fetch your first row in this line:
$user_data = mysqli_fetch_array($results);
And you do nothing with it.
And please do not mix procedural and object-oriented usage of mysqli.
I cannot figure this out, it's not an error as such, but when i try to echo out the results of a query:
echo "SELECT * FROM `active_customers` WHERE `cus_email`='".$k."' AND `cus_product`='".$_GET['product']."'" . "<br />";
$q = mysql_query("SELECT * FROM `active_customers` WHERE `cus_email`='".$k."' AND `cus_product`='".$_GET['product']."'");
$a = mysql_fetch_array($r);
$n = mysql_num_rows($q);
echo "Number of rows:" . $n;
echo $a['cus_id'];
echo $a['cus_email'];
The number of posts is returning the correct number which is 1, these values $a['cus_id']; and $a['cus_email']; are not coming through at all, i even did a mysql query in phpmyadmin and it works there.
can anyone see what i have done wrong?
correct code can be
echo "SELECT * FROM `active_customers` WHERE `cus_email`='".$k."' AND `cus_product`='".$_GET['product']."'" . "<br />";
$q = mysql_query("SELECT * FROM `active_customers` WHERE `cus_email`='".$k."' AND `cus_product`='".$_GET['product']."'");
$n = mysql_num_rows($q);
echo "Number of rows:" . $n;
while($row = mysql_fetch_array($q, MYSQL_ASSOC)){
echo $row['cus_id'];
echo $row['cus_email'];
}
Although i would recommend using mysqli or PDO for sql databases
My solution to this is at the bottom
My issue is: I am trying to display foreign key data, but because there is more than one foreign key, I am getting a 'duplicate' query for each of the foreign keys.
http://i.imgur.com/Gfqx497.png
As you can see, I can query the correct data, but I don't know how to attach the other foreign key data to the same 'one line output'.
I've been lurking stackoverflow for a while to find an answer to my problem and I'm at a wits end. I have found quite a number of threads, such as the two links below, where I believe people are asking the same thing, however I can't seem to wrap my head around getting the solution to work in my case. From my understanding, I need to be using aliases for the tables, however I've tried multiple different interpretations of the solutions and can't recreate the solution.
How do I merge two or more rows based on their foreign key
mysql query 2 foreign keys
--
I've got two tables ('Minions and Ability'), one of which has four foreign keys linking to the other.
http://i.imgur.com/ctpFHur.png
This is the php code that I'm using for the query, which is mostly taken from PHP and MySQL Web Development 4th Edition (Welling, Thomson) which I purchased to get me started with php and mysql.
$query = "SELECT minions.name, minions.summon, minions.attack,
minions.health, minions.race, minions.rarity,
minions.ability1, minions.ability2, minions.ability3,
minions.ability4, minions.imagebig,
ability.ability
AS ability FROM minions
INNER JOIN ability on
minions.ability1 = ability.abilityid
OR minions.ability2=ability.abilityid";
//Only trying for 2 foreign keys to try get it to work
$result = $db->query($query);
$num_results = $result->num_rows;
echo "<p>Number of items found: ".$num_results."</p>";
for ($i=0; $i <$num_results; $i++){
$row = $result->fetch_assoc();
//echo "<p><strong>".($i+1).". Name: ";
echo "<p><strong>";
echo htmlspecialchars(stripslashes($row['name']));
echo "</strong><br />Summoning cost: ";
echo stripslashes($row['summon']);
echo "<br />Attack: ";
echo stripslashes($row['attack']);
echo "<br />Health: ";
echo stripslashes($row['health']);
echo "<br />Race: ";
echo stripslashes($row['race']);
echo "<br />Rarity: ";
echo stripslashes($row['rarity']);
//if (stripslashes($row['ability'] != NULL)){
echo "<br />Abilty: ";
echo stripslashes($row['ability']);
//}
echo "<br />";
$imageMinion = stripslashes($row['imagebig']);
// $iwidth = 25;
// $iheight = 100;
// echo '<img src="img/'.$imageMinion.'.png" style="width:'.$iwidth.'px;height:'.$iheight.'px;">';
//echo "<br />";
echo '<img src="img/'.$imageMinion.'.png">';
echo "</p>";
Could someone please guide me to getting this to display correctly? I've tried to follow the other solutions and just can't seem to get the alias naming correct, if I'm correct in thinking that is the solution.
========EDIT REGARDING ANSWER FROM verbumSapienti===========
I am embarrassingly unable to get your Answer to work. This is how the code looks.
$query = "SELECT minions.name, minions.summon, minions.attack, minions.health,
minions.race, minions.rarity, minions.ability1, minions.ability2,
minions.ability3, minions.ability4, minions.imagebig,
ability.ability
AS ability
FROM minions
INNER JOIN ability
ON minions.ability1 = ability.abilityid
OR minions.ability2 = ability.abilityid
OR minions.ability3 = ability.abilityid
OR minions.ability4 = ability.abilityid";
$result = $db->query($query);
$num_results = $result->num_rows;
echo "<p>Number of items found: ".$num_results."</p>";
for ($i=0; $i <$num_results; $i++){
$row = $result->fetch_assoc();
$abilities = array('ability1', 'ability2', 'ability3', 'ability4');
foreach($abilities as $ability)
{
$q = "SELECT $ability FROM minions WHERE name={$row['name']}";
$result = $db->query($q);
$row2 = $result->fetch_assoc();
$abilitiesArr[] = $row2[$ability];
}
echo "<p><strong>";
echo htmlspecialchars(stripslashes($row['name']));
echo "</strong><br />Summoning cost: ";
echo stripslashes($row['summon']);
echo "<br />Attack: ";
echo stripslashes($row['attack']);
echo "<br />Health: ";
echo stripslashes($row['health']);
echo "<br />Race: ";
echo stripslashes($row['race']);
echo "<br />Rarity: ";
echo stripslashes($row['rarity']);
foreach($abilitiesArr as $ability)
{
$q = "SELECT $ability FROM ability";
$result = $db->query($q);
$row = $result->fetch_assoc();
echo "<br />Ability: $row";
}
/*if (stripslashes($row['ability'] != NULL)){
echo "<br />Abilty: ";
echo stripslashes($row['ability']);
}*/
echo "<br />";
$imageMinion = stripslashes($row['imagebig']);
echo '<img src="img/'.$imageMinion.'.png">';
echo "</p>";
}
I've tried changing around a few things and haven't had any success. As is, I get the following error:
Fatal error: Call to a member function fetch_assoc() on a non-object in D:\Xampp\htdocs\ocduels\results.php on line 87
Which is:
$row2 = $result->fetch_assoc();
In:
$abilities = array('ability1', 'ability2', 'ability3', 'ability4');
foreach($abilities as $ability)
{
$q = "SELECT $ability FROM minions WHERE name={$row['name']}";
$result = $db->query($q);
$row2 = $result->fetch_assoc();
$abilitiesArr[] = $row2[$ability];
}
::MY SOLUTION TO THIS::
This seems to work. I don't think its efficient, but its enough to allow me to continue learning. Thank you for all the responses. This allows me to find a 'Minion' and only have one instance of the 'Minion' when there is more than 1 Foreign Key with data.
$query = "SELECT
m.name as m_name,
m.summon as m_summon,
m.attack as m_attack,
m.health as m_health,
m.race as m_race,
m.rarity as m_rarity,
m.ability1 as m_ability1,
m.ability2 as m_ability2,
aa.ability as a_ability,
ab.ability as b_ability,
m.imagebig as m_imagebig
FROM minions m
LEFT JOIN ability aa
ON m.ability1 = aa.abilityid
LEFT JOIN ability ab
ON m.ability2 = ab.abilityid";
$result = $db->query($query);
$num_results = $result->num_rows;
echo "<p>Number of items found: ".$num_results."</p>";
for ($i=0; $i <$num_results; $i++){
$row = $result->fetch_assoc();
echo "<p><strong>";
echo htmlspecialchars(stripslashes($row['m_name']));
echo "</strong><br />Summoning cost: ";
echo stripslashes($row['m_summon']);
echo "<br />Attack: ";
echo stripslashes($row['m_attack']);
echo "<br />Health: ";
echo stripslashes($row['m_health']);
echo "<br />Race: ";
echo stripslashes($row['m_race']);
echo "<br />Rarity: ";
echo stripslashes($row['m_rarity']);
if (stripslashes($row['a_ability'] != NULL)){
echo "<br />Ability 1: ";
echo stripslashes($row['a_ability']);
}
if (stripslashes($row['b_ability'] != NULL)){
echo "<br />Ability 2: ";
echo stripslashes($row['b_ability']);
}
echo "<br />";
$imageMinion = stripslashes($row['m_imagebig']);
echo '<img src="img/'.$imageMinion.'.png">';
echo "</p>";
}
Try DISTINCT keyword to restrict duplicate values.
SELECT DISTINCT minions.name, minions.summon, minions.attack,
minions.health, minions.race, minions.rarity,
minions.ability1, minions.ability2, minions.ability3,
minions.ability4, minions.imagebig,
ability.ability
AS ability FROM minions
INNER JOIN ability on
minions.ability1 = ability.abilityid
OR minions.ability2=ability.abilityid";
you could try a subquery that prints only the ability text for each ability ID contained in each minion's attributes, maybe something along the lines of:
$abilities = array('ability1', 'ability2', 'ability3', 'ability4');
foreach($abilities as $ability)
{
$q = "SELECT $ability FROM minions WHERE name={$row['name']}";
$result = $db->query($q);
$row2 = $result->fetch_assoc()
$abilitiesArr[] = $row2[$ability];
}
then replace
echo "<br />Abilty: ";
echo stripslashes($row['ability']);
with
foreach($abilitiesArr as $ability)
{
$q = "SELECT $ability FROM ability";
$result = $db->query($q);
$row = $result->fetch_assoc()
echo "<br />Ability: $row";
}