I will be using data from a MySQL database and to receive that data I will be using this code:
SELECT link, notes FROM links WHERE useri_id=XXX;
I am now stuck on how to display this nicely in a list where it will be a link and then on next line the notes for that link and then a spacer line and then it will display the next link and notes and so on. How would I code this?
You can use something like this:
$rs = mysql_query("SELECT link, notes FROM links WHERE useri_id=XXX") or die(mysql_error());
echo "<ul>";
while( false !== ($row = mysql_fetch_assoc($rs)))
{
echo "<li>";
echo "<a href='" . $row['link'] . "'>" . $row['link'] . "</a><br />";
echo $row['notes'] . "<hr />";
echo "</li>";
}
echo "</ul>";
Use this:
$query = "SELECT link, notes FROM links WHERE useri_id=XXX";
$result = mysqli_query(db_conn_link_var_name, $query);
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
echo "<ul>";
echo "<li>{$row['link']}</li>";
echo "<li>{$row['notes']}</li>";
echo "</ul>";
}
Coding HTML right in PHP code is not a recommended way.
You should use some templating engine like PHP Smarty or whatever rocks nowadays.
Check e.g. http://devzone.zend.com/article/1238
However, for learning purposes, the other anwsers are just ok.
Ondra's answer is the most reliable one In my view , addition to his answer I suggest you to use PHP frameworks which will take care of many things for you and you can focus on design and code in an organized way.
If you can see all the codes given above are to hard to maintain because of spaghetti nature of PHP coding .
CakePHP and Symfony are really good, Zend needs more time to learn .
But still If you need this code just for a minor database connection use the codes given above.
Related
I am new to PHP and programming, but am attempting to print out the results from each row of one of my MySQL database tables.
Using a set of while loops I achieved this:
while($placeHolder = $query->fetch_assoc()){ // use assoc
echo "<div class='placeHolder'><img src=" . $placeHolder['photo']. " /></div>";
echo "<br />";
}
while($placeHolder2 = $query2->fetch_assoc()){
echo "<div class='placeHolder2'>" . $placeHolder2['name'] . "</div>";
echo "<br />";
}
//etc, etc...
Besides being inefficient, I'm assuming this may also be a security risk.
Is there a better way to do this, possibly using a foreach statement?
Here is my SQL code I forgot to include:
$query = mysqli_query($conx, $sql);
$query2 = mysqli_query($conx, $sql);
$sql = ('SELECT id,name,picture FROM table ORDER BY name DESC LIMIT 50');
I dont think using a foreach statement to print out the results would make it any less safe/unsafe. It really depends on your query. You should be able to print all of them using a foreach doing something like:
$result = $query->fetch_all(MYSQLI_ASSOC);
foreach($result as $row) {
echo $row['photo'];
}
Note** to use fetch_all you would need mysqlnd installed
edit: looking at your query nothing can be injected there. No outside variables being used in the query, so its safe. I would just use the while loop that you originally posted.
edit 2: link to fetch_all documentation: http://php.net/manual/en/mysqli-result.fetch-all.php
i am developing multi-level (comments and repelis) comment system using php.
all comments and repelis are in same database table.
in the function this system have to run 'sql' query for each comment box to generate them in correct order.
so, will this slow down strongly my system while handling thousands of comments.
here below is my function,
function getComments($row) {
echo "<li class='comment'>";
echo "<div class='aut'>".$row['author']."</div>";
echo "<div class='comment-body'>".$row['comment']."</div>";
echo "<div class='timestamp'>".$row['created_at']."</div>";
echo "<a href='#comment_form' class='reply' id='".$row['id']."'>Reply</a>";
$q = "SELECT * FROM threaded_comments WHERE parent_id = ".$row['id']."";
$r = mysql_query($q);
if(mysql_num_rows($r)>0)
{
echo "<ul>";
while($row = mysql_fetch_assoc($r)) {
getComments($row);
}
echo "</ul>";
}
echo "</li>";
}
or i have another idea to put comments and repelis in tow tables. while comments displaying,
get all repelis data to Array and push them in to related comment box using for loop for that array.
which method will be best for this work. the best performance ?
I'm looking to create a formatted product list from an SQL database. My aim is to have a store on my website with a series of small boxes containing some shorthand information about each product, that when clicked will open a pop-up containing detailed information. (I have a working Javascript/JQuery code to create the pop-ups.)
Here is the PHP code so far, simply to get the information from the database and display it on a webpage...
(I've been using XAMPP to provide an environment for me to test the code in)
<?php
mysql_connect("localhost", "root", "") or die (mysql_error ());
mysql_select_db("Database1") or die(mysql_error());
$strSQL = "SELECT * FROM Products";
$rs = mysql_query($strSQL);
while($row = mysql_fetch_array($rs)) {
echo $row['Brand'] . " " . $row['ProductName'] . " " . $row['Image'] . "<br />";
}
mysql_close();
?>
I want the echoed line to be displayed in a divider, with a divider generated for each record in the SQL database (say I have 10 products available, there would be ten dividers, and 10 different boxes on the webpage). The divider's class is "ProductBox".
echo "<div class=\"ProductBox\">"; $row['Brand'] . " " . $row['ProductName'] . " " . $row['Image'] . "</div>";
This was the closest I have come to a solution, which was simply managing to write a code with no syntax errors - alas, nothing actually displays on the webpage.
If I'm going about this entirely the wrong way please tell me - I'm fairly sure I need to use a SQL database to dynamically update stock on a live website, but if I need to implement a different programming language or whatever then just tell me what you think would work and help me with a solution.
You have an extra semicolon in your code
echo "<div class=\"ProductBox\">"; $row['Brand'] . " " . $row['ProductName'] . " " . $row['Image'] . "</div>";
Replace with
echo "<div class=\"ProductBox\">". $row['Brand'] . " " . $row['ProductName'] . " " . $row['Image'] . "</div>";
mysql_fetch_array needs to be used like this (see PHP Doc):
while($row = mysql_fetch_array($rs, MYSQL_ASSOC)) {
}
or you could just use "mysql_fetch_assoc" instead.
HOWEVER, if you're new to PHP, I HIGHLY RECOMMEND that you get started on the right foot. mysql_query functions are soon to be deprecated. DON'T USE THEM. Most recommend using "PDO" for querying your database. Here's a great tutorial to teach you: http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers
Also, as mentioned, you have an extra semi-colon.
Dont forget these basics markups :
`<HTML>
<HEAD>
</HEAD>
<BODY> put in here your divs
</BODY>
</HTML>`
I realize this isn't the typical question, and I will delete it tonight as to not take up space with a non-code error question, but I really need some help. I am trying to pull information from the question database and create a list of questions organized by the question title. When the user click on the question title, it brings them to the next page according to the question_id.
I posted the code for how the list of questions are currently being displayed, but I can't figure out how I would style them to make it actually look appealing. Would I use say 10 div tags with 10 different id's that all specify the background, and distance from the top? or would I use a table? or is there something else I could use?
I'm new with php and html, and I really don't know many ways to display information that is being pulled from the database....
Current_questions.php
<?php
$i = 0;
$str = "";
$sql = "SELECT * FROM questions";
$result = mysql_query ($sql, $conn) or die(mysql_error());
if (mysql_num_rows($result) >= 0)
{
while ($row = mysql_fetch_array($result, MYSQL_ASSOC) and $i<10 )
{
$i++;
$str .= $i . ". ";
$str .= "<a href='show_question2.php?question_id=" . $row["question_id"] . "'>"
. $row["title"] . "</a> <br> ";
}
print $str;
}
?>
This code currently just prints a list of the title's, and that is not ideal. Thanks!
im not looking for how to code it, just suggestion on best way. Simple answer of a couple of words is all i am looking for –
It really helps to learn some CSS
$toggle = false;
while ($row = mysql_fetch_array($result, MYSQL_ASSOC) and $i<10 )
{
$i++;
$str .= $i . ". ";
$toggle = !$toggle;
if($toggle)
$style = "background: Grey;";
else
$style = "background: White;";
$str .= "<a style='padding: 10px;$style' href='show_question2.php?question_id=" . $row["question_id"] . "'>"
. $row["title"] . "</a> <br> ";
}
Start with the HTML entity which best describes what you are doing. To me that sounds like a list. Now decide if it is an ordered or unordered list. If unsure go with u-ordered
You'll probably want your html to end up something like:
<ul>
<li><a href='show_question2.php?question_id=1'>Question 1 Title</a></li>
<li><a href='show_question2.php?question_id=2'>Question 2 Title</a></li>
<li><a href='show_question2.php?question_id=3'>Question 3 Title</a></li>
</ul>
Now look around the internet for one of the many articles on styling a list.
Here are 3 to start you off:
http://www.alistapart.com/articles/taminglists/ -- I'm a big fan of these guys
http://www.webreference.com/programming/css_style2/index.html
http://www.marcofolio.net/css/8_different_ways_to_beautifully_style_your_lists.html
A real quick and dirty examples with lists
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"