Thank you for reading my question.
I am trying to make a site where information from a database is displayed onto a webpage. The end result will look like this, but for a different game.
Here is a plain HTML page of what I want it to look like.
So far I know that my connection to the database works. When I run:
mysql_select_db("DATABASE", $con);
$result = mysql_query("SELECT * FROM DATABASE");
while($row = mysql_fetch_array($result)) {
echo $row['Title'] . " " . $row['Type'];
echo "<br />";
}
It returns the Title and Type.
What I want to do is run an If/Else statement that runs a different that block of code depending on the card type.
while($row = mysql_fetch_array($result)) {
if ($row['Title'] == 'Hero') {
echo "<div>";
}
}
I tried this based on the tutorials at w3schools.com but it doesn't work.
Do any of you have any ideas for what I should do?
EDIT:
Here is what I tried running:
while($row = mysql_fetch_assoc($result)) {
if ($row['Title'] == 'Hero') {
echo $row['Title'] . " Hero.<br>";
} else {
echo $row['Title'] . " Who cares.<br>";
}
}
Here is the output (Gimli should show up as a Hero):
For Gondor! Who cares.<br>
Bilbo Baggins Who cares.<br>
Ungoliant's Spwan Who cares.<br>
Gimli Who cares.
EDIT 2: Thank you Phil for spotting the error, I now get the result I wanted using Mikushi's method. Thank you all so much.
The fetching of your mysql result seems wrong, should be like this:
while($row = mysql_fetch_assoc($result)) {
if ($row['Title'] == 'Hero') {
echo ""; }
}
mysql_fetch_array fetch the result as an indexed array (1=> data, 2=> thing) , which explains why $row['Title'] doesn't work.
The difference:
http://ca2.php.net/mysql_fetch_array
http://ca2.php.net/mysql_fetch_assoc
Please, always refer to the documentation, it's very well done and a better source than w3cschools.
Maybe it's one of those all too obvious things but...
Shouldn't it be
if ($row['Type'] == 'Hero') // "Type", not "Title"
Related
I am updating all my code to Mysqli, before I did I had this code and it worked:
while($row = mysql_fetch_assoc($WildHorses)){
$InWild[] = $row['id'];
}
$RandomWild = array_rand($InWild);
$RandomHorse = $InWild[$RandomWild];
This is my SELECT statement:
$sql = "SELECT Horse.id, Horse.Name, Horse.Age, Horse.Image_name, Horse.Owner, Horse.Barn, Images.Image_path, Images.Image_name FROM Horse, Images WHERE Horse.Owner = '$colname_WildHorseList' AND Images.Image_name = Horse.Image_name";
$result = mysqli_query($con,$sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "id: " . $row["id"]. " Name: " . $row["Name"]. " ImageName: " . $row["Image_name"]. "<br>";
}
} else {
echo "0 results";
}
The SELECT statement ends up echoing all of the correct information, but I want to make an array of only the Id's so that I can pick one at random each time a button is clicked.
I have tried multiple different copies and pastes of code to try and get what I want, but nothing seems to come out right.
Can someone point me in the right direction or explain what I'm doing wrong?
In your while loop you can simply do this :-
$i=0;
$animals=array();
$animals[$i]=$row["id"]; //puts id in array
And then you can create a random number by "rand()" between the length of 0-$i
and can get the job done.
I'm trying to get rows from my database between two dates. I've used other questions here to get to this point, but I don't know how to move forward from here.
Here is the code, and underneath are the screenshots of the output and a photo running the query in SQL.
Thanks for the help in advance!
--
This first section of code outputs the ID, name, etc.
<?php
$sql = "SELECT * FROM songs ";
$result = $link->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["ID"] . " - Name: " . $row["name"] . " " . $row["released"]. "<br>";
}
} else {
echo "0 results";
}
/* close connection */
$link->close();
?>
This section of code outputs "0 Results"
<?php
$from_date = '2015-01-01';
$to_date = '2017-04-04';
$sql = "SELECT * FROM songs WHERE released BETWEEN '" . $from_date . "' AND '" . $to_date . "' ";
$result = $link->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["ID"] . " - Name: " . $row["name"] . " " . $row["released"]. "<br>";
}
} else {
echo "0 results";
}
/* close connection */
$link->close();
?>
This is what the page looks like with the above code.
I've successfully selected the rows in SQL.
EDIT: I made a mistake when writing the code into the question for the IF ELSE statement on the 2nd block of code. I just updated it to what I actually have on the site. I didn't change anything based on the solutions, and am still not getting the rows to print.
You done everything right, except using while/else ;
Your code (2-nd) section should look like this:
if ($result->num_rows > 0) {
while () {
//print your rows
}
} else {
//Say that there's 0 results
}
Your results are listed - as you can see from your screenshot. Problem: While along with else does not make sence.
your last example is missing a } so it says: while { } else { echo "0 results"}
But the echo "0 results" should be related to the num_rows-check, not to the while.
Ah man, I just needed to had to comment out this line from the first block:
$link->close();
I apologize to everyone who answered if it wasn't clear that the blocks of code followed one another. Anyways, for anyone else with the same problem, don't close the connection -_-
Here's what is output when I comment out the line from the first block
JSFiddle here, with unfunctionaing PHP obviously, but on the local host it pulls through the information just fine.
I am trying to append data in a loop into a specific set of tags, inside a di of a certain class.
For example, for every row in the the table, add a div with the class "card" and append "Title" into a H2, "description" into a P tag etc.
Anyone know how I could go about doing this? I'm googling but finding it hard to phrase my question right.
Div "Card" markup:
<div class="card">
<h2>Health and Wellbeing</h2>
<p>Sometimes you just did too much sitting not enough jumping go go go.</p>
</div>
PHP pull:
$sql = "SELECT title, description, count FROM relevant_topics";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<b>Title: </b>" . $row["title"]. " - Description: " . $row["description"]. " " . $row["count"]. "<br>";
}
} else {
echo "0 results";
}
You can echo the div markup into the PHP code like this:
while($row = $result->fetch_assoc()) {
echo'<div class="card">
<h2>'.$row["title"].'</h2>
<p>'.$row["description"].'
</div>';
}
Not sure where you want the Row['count'] though.
Also you might wanna use PDO instead of mysql, Since mysql is outdated: How can I prevent SQL injection in PHP?
check this
if ($result->num_rows > 0) {
// output data of each row
while($row = $result-> mysql_fetch_array()) {//if not works change mysql_fetch_array() to fetch_assoc()
echo'<div class="card"><h2>';
echo '.$row["title"].';
echo '</h2><p>';
echo '.$row["description"].';
echo '</P></div>';
}
I'm trying to make a simple search engine using php & mysql . I know how get mysql result using php. Its Work.
ex : - Mysql database stored this (database table filed "meta")
I am having some difficulty with my PHP/MySQL search. It worked great
a year ago, but for some reason it has just stopped working. I have
tried searching and cannot figure it out. I have it echo my query but
it never adds in the WHERE clause. Any help is much appreciated. Form
Code: PHP Code:
Search word is searching
I Need to dispay search result like this
I have tried searching and cannot figure it out. I have it echo my
query but it never adds in the WHERE clause. Any help is much
appreciated. Form Code: PHP Code:
$query= mysql_query("SELECT meta FROM data WHERE LIKE '%searching%' ");
$count = mysqli_num_rows($query);
if($count >0){
while($row = mysqli_fetch_array($query)) {
echo "$row[id]'>$row[name]</option>";
$count =1;
}
else{
echo "<option value='' selected='selected'></option>";
}
mysqli_close($con);
From the code above here, there's a couple of issues that stand out:
The echo for the option doesn't have an open for , so it's not going to show.
When you're echoing information from the $row variable, you're referencing id and name, but the query is only outputting meta.
The query isn't searching on a field for the where clause, and is therefore failing
The code, from what I can see, should be changed to the following to work (assuming no other surrounding issues):
$query = mysql_query("SELECT meta, id, name FROM data WHERE meta LIKE '%searching%'");
$count = mysqli_num_rows($query);
if ($count > 0)
{
while ($row = $mysqli_fetch_array($query))
{
echo "<option value='" . $row['id'] . "'>" . $row['name'] . "</option>";
$count = 1; // not sure why this needs to be here, but it's in your code so I'm keeping it there
}
}
else
{
echo "<option value='' selected='selected'></option>";
}
mysqli_close($con);
I'm trying to determine the right syntax from what I'm trying to do with MySQL. I'm basically saying that if a value in a certain column of a row of a table is equal to some session variables, I want to echo out info.
I have a table with subject, description and user. User is set by taking the current user's first name and last name and inserting it into the table under user. This is done by the following code:
$sql="INSERT INTO tbl_name (subject, description, user)
VALUES
('$_POST[subject]','$_POST[description]','$_SESSION[firstname] $_SESSION[lastname]')";
Then once I'm calling this data back out, I want to basically allow the user to delete content that they submitted themselves. The first step for me is to be able to display it in the table. I believe this is just syntax error, but I've confused myself now with how things are set up:
$sql = "SELECT * FROM tbl_name ORDER BY subject, description
LIMIT {$startpoint},{$limit}";
$result = $mysqli->query($sql);
$num_rows = mysqli_num_rows($result);
if($num_rows>0){
$field_num = $mysqli->field_count;
echo "<h1>HERE ARE SOME EXAMPLES:</h1>";
echo "<table border='0'><tr>";
for($i=0; $i<$fields_num; $i++)
{
$field = mysql_fetch_field($result);
echo "<td>{$field->subject}</td>";
echo "<td>{$field->description}</td>";
echo "<td>{$field->user}</td>";
if('$field->user' == '$_SESSION[firstname] $_SESSION[lastname]'){
echo '<td>You can delete this</td>';
}
}
I figured the $field->user would equal $_SESSION[firstname] $_SESSION[lastname] because that's how it was initially submitted to the table (without the '.' for concatenation).
Any help would be appreciated. Thanks!
EDIT
Here's the result of my table output code. The results are actually being display with $cell instead of from within the for loop I believe. I've added the if statement in after the but it doesn't seem to recognize echo "<td>".$field->user."</td>"; which makes me think that that is where the problem lies. What I would like to do is be able to add the if statement in a immediately after `echo {$field->user}"; to keep the code clean. I think I've confused myself thoroughly:
if($num_rows>0){
$field_num = $mysqli->field_count;
echo "<h1>HERE ARE SOME JOBS:</h1>";
echo "<table border='0'><tr>";
for($i=0; $i<$fields_num; $i++)
{
$field = mysql_fetch_field($result);
echo "<td>{$field->subject}</td>";
echo "<td>{$field->description}</td>";
echo "<td>{$field->user}</td>";
if($field->user == $_SESSION[firstname]." ".$_SESSION[lastname]){
echo '<td>You can delete this</td>';
}
else{
echo "<td>".$field->user."</td>";
echo "<td>".$_SESSION[firstname]." ".$_SESSION[lastname]."</td>";
}
}
echo "</tr>\n";
while($row = mysqli_fetch_row($result))
{
echo"<tr>";
foreach($row as $cell)
echo "<td>$cell</td>";
echo "</tr>\n";
}
mysqli_free_result($result);
}
else{
echo 'There are no jobs!';
}
I re-wrote the code in a way that was a little bit easier for me to understand (though maybe not the shortest way to do it):
while($row = mysqli_fetch_array($result))
{
echo"<tr>";
echo"<td>".$row['subject']."</td>";
echo"<td>".$row['description']."</td>";
echo"<td>".$row['user']."</td>";
if($row['user'] == $_SESSION['firstname']." ".$_SESSION['lastname']){
echo"<td>You can delete this</td>";
}
else{
echo"<td>Code didn't work</td>";
}
echo "</tr>\n";
}
It ended up working this way. If there's way to do this shorter then feel free to post it here otherwise thanks for the help!