I’m not sure if the title makes sense, but here is what I’m trying to do.
Is it possible if, for example a column in my database is called “point”. Inside, I would enter numbers from 1-5. Is it possible for PHP or the html to display instead of numbers 1-5, each number would display a different picture?
I’m not sure if that made sense, if it doesn’t please reply and I will tell more in detail. I can’t search in Google because of my lack of capability to word them correctly and find my answer.
sure it’s possible!
What you would do is save your numbers in the database as normal.
Then, in your PHP script, you would:[LIST]
[]connect to your database
[]build a query along the lines of SELECT point FROM yourTable
[]store the result of the query in a variable
[]iterate over the result set using (for example) while and mysql_fetch_array
[]in your while loop, you could then do something like echo ("<img src="/path/to/images/folder/$row['point'].jpg />");
[]this would output an image tag, whose src attribute points to a jpg with the same name as the value stored in your database (e.g. <img src=“/images/5.jpg” />).
[/LIST]
Does that make sense?
You would essentially be using the values stored in point as file names.
If you need help implementing any of this, just let me know (but try and have a go yourself, first).
As much as how I’m tempted to do this, I simply do not get it I don’t get the “while” and things, sorry
I apologise if I’m a MySQL and PHP noob but I just started. However I appreciate your help and it seems really cool and I can’t wait for it to work… Can you help me give an example in a code?
Yup, no problem.
Before we start:
Do you have a database which you can connect to and a PHP environment to test in?
Also, does your table with the column “points” exist?
Yes, I’ve recently purchased hosting from hostgator. I have a database to test it. Even not, I have MAMP on my computer where I can locally test it.
And sorry, I don’t know what’s points?
Cool.
Now we need to select everything in the column “point” in your database table and store the result of this query in a variable.
We do this, like this:
$result = mysql_query("SELECT point FROM yourTable");
This returns a results set which we can loop over using while and mysql_fetch_array
<?php
$con = mysql_connect("localhost","yourName","yourPassword");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("yourDatabase", $con);
$result = mysql_query("SELECT point FROM yourTable");
while($row = mysql_fetch_array($result)) {
echo "$row['point']<br />";
}
mysql_close($con);
?>
I’ve got to duck out now for a bit, but this should give you something to experiment with.
Let me know how you get on.
[I]Hi, I’ve updated the php document - but it shows an error:
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/eugechin/public_html/mysqltest.php on line 18
On line 18, it is echo “$row[‘food’]”; where food is my column.[/I]
Nevermind, there is no space between w and [ thanks
Please do. If PHP is less troublesome, I’d love to learn about this and put it in my journal.
Right now, because of trouble putting information in, I dropped the table and tried to create one using PHP. However, when I visit the link it returns me with a message like this:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ’ env INT), service INT), hygiene INT), price INT), address VARCHAR(30), p’ at line 4
Hi. When I run the php, it displays the successful message.
However, when I go back to phpmyadmin, it doesn’t display that table.
Perhaps there may be some confusion between the database. Im hosting with Hostgator and I create a database from their cPanel. Therefore, after I make a db, it will be called username_dbname. I tried entering
mysql_select_db(“username_dbname”, $con);
and also
mysql_select_db(“dbname”, $con);
and run the php, but it doesnt appear.