Rianna
09-22-2006, 12:37 PM
Hi, Anyone know of a way to add web site url's to my pages using My sql on the server?
Thank you very much.
Thank you very much.
|
Click to See Complete Forum and Search --> : Any way to use my sql on the server to add links to my pages? Rianna 09-22-2006, 12:37 PM Hi, Anyone know of a way to add web site url's to my pages using My sql on the server? Thank you very much. PineSolPirate 09-22-2006, 12:56 PM Do you mean to store a bunch of links in the database then display them on a page? Rianna 09-22-2006, 12:57 PM Yep, thats it. PineSolPirate 09-22-2006, 01:03 PM Well, you just need to pick a server-side language. To do this in PHP let's assume you have a database table called "mylinks" in database "mydatabase" with three columns, "ID", "URL" and "linkText". You'd just add this to your PHP page: <?php $link = mysql_connect('localhost', 'mysql_user', 'mysql_password'); if (!$link) { die('Not connected : ' . mysql_error()); } $db_selected = mysql_select_db('mydatabase', $link); if (!$db_selected) { die ('Can\'t use mydatabase : ' . mysql_error()); } $query = "SELECT * FROM mylinks LIMIT 0, 5"; // Note, here you are pulling back only 5 links. To get them all just remove the "LIMIT 0,5" Part $result = mysql_query($query); while ($row = mysql_fetch_array($result) { print "<a href='$row[URL]'>$row[linkText]</a><br/>"; }?> Hope this helps. Rianna 09-22-2006, 01:10 PM OK thanks pirate I'll look it over well, php is great! Any other ideas, please let me know. Rianna 09-22-2006, 01:35 PM OK, I like this idea, hope I can use it, I'm a bit fuzzy on how I add these fields to the database so it will in turn put them in the .php page? Please let me know, thank you very much. PineSolPirate 09-22-2006, 01:43 PM M'k, on your hosting control panel do you have anything that says "Administer MySQL" or "phpMyAdmin" Thats usually the easiest way to insert a new table into your database. As for placing it in the page, just make sure the extension is .php and just drop that code in whereever you want: <html> <head><title>Some Website</title></head> <body> <p>Integer rhoncus tortor eu diam. Sed feugiat viverra enim. Phasellus facilisis. Sed eu odio. Vivamus dolor magna, placerat non, commodo nec, volutpat ac, massa. Vivamus eget mi. Nulla condimentum dolor scelerisque ipsum. Ut diam est, nonummy eu, porttitor vitae, vehicula quis, sapien. Sed ut pede non arcu faucibus placerat. Nulla enim. Donec et magna ut turpis tristique mollis. Nullam venenatis. Donec dolor dui, semper et, rhoncus id, pellentesque quis, lectus.</p> <?php $link = mysql_connect('localhost', 'mysql_user', 'mysql_password'); if (!$link) { die('Not connected : ' . mysql_error()); } $db_selected = mysql_select_db('mydatabase', $link); if (!$db_selected) { die ('Can\'t use mydatabase : ' . mysql_error()); } $query = "SELECT * FROM mylinks LIMIT 0, 5"; // Note, here you are pulling back only 5 links. To get them all just remove the "LIMIT 0,5" Part $result = mysql_query($query); while ($row = mysql_fetch_array($result) { print "<a href='$row[URL]'>$row[linkText]</a><br/>"; } ?> <p>Integer rhoncus tortor eu diam. Sed feugiat viverra enim. Phasellus facilisis. Sed eu odio. Vivamus dolor magna, placerat non, commodo nec, volutpat ac, massa. Vivamus eget mi. Nulla condimentum dolor scelerisque ipsum. Ut diam est, nonummy eu, porttitor vitae, vehicula quis, sapien. Sed ut pede non arcu faucibus placerat. Nulla enim. Donec et magna ut turpis tristique mollis. Nullam venenatis. Donec dolor dui, semper et, rhoncus id, pellentesque quis, lectus.</p> </body> </html> That would print the first paragraph of gibberish, the links, then another paragraph of gibberish. Rianna 09-23-2006, 01:09 AM Wow, took me all day to set up phpadmin, didn't know I opened up a whole new world. Pretty amazing stuff. If I put several fields in my sql table but only call on a few of them in the above type .php will the above php still work? Seems like it would but I am not sure. Reason I am wondering is I was hoping I could not only use the database as the link script, but also as a database to store other information I need to store. That way I don't have to set up many different databases. Thank you! PineSolPirate 09-23-2006, 02:09 AM Yes you can do that, though it's typically better to keep seperate information on seperate tables in the database. If you do end up just using one, tweak the query's so they only pull back the info you want, that'll save some execution time and hassle. You'd also want to add a kind of "type" column, to differentiate between links and whatever else you put in there. Honestly, I reccomend just using one table for one thing :) Here's the code for if you added a "type" column, and links were type "L". <?php $link = mysql_connect('localhost', 'mysql_user', 'mysql_password'); if (!$link) { die('Not connected : ' . mysql_error()); } $db_selected = mysql_select_db('mydatabase', $link); if (!$db_selected) { die ('Can\'t use mydatabase : ' . mysql_error()); } $query = "SELECT URL, linkText FROM mytable WHERE type='L' LIMIT 0, 5"; // Note, here you are pulling back only 5 links. To get them all just remove the "LIMIT 0,5" Part $result = mysql_query($query); while ($row = mysql_fetch_array($result) { print "<a href='$row[URL]'>$row[linkText]</a><br/>"; } ?> Rianna 09-23-2006, 08:16 AM OK thank you very much. Rianna 09-23-2006, 03:11 PM The good news is it liked everything until the very last line. I am using the last one you posted. It said it didn't like the bracket "{" which I don't understand because I have seen while with print commands with brackets before. So I removed them both. Then it said it didn't like the print command. Which told me nothing. As a sidenote my database has a name, my table has a name, then there are the column names in it that the links go in. So I have no idea if your code needs both a database name and table name as well as the fields. But I found this online all by itself, I hope it will give you some ideas, hope it doesn't lead in the wrong direction instead. <ul> <?php $result = mysql($conn, "SELECT class, address, name FROM tutorials"); /* Lets presume that the used database table has fields id, class, address name and description, which in this example has been fetched information from three fields. */ while (list($class, $address, $name) = mysql_fetch_row($result)) { print("<li><a class=\"$class\" href=\"$address\" target=\"new\">$name</a></li>"); } ?> </ul> PineSolPirate 09-23-2006, 06:01 PM Oops, there's a missing paren. ) while ($row = mysql_fetch_array($result)) { print "<a href='$row[URL]'>$row[linkText]</a><br/>"; } My bad. Rianna 09-23-2006, 07:38 PM Hey Pirate, very good! Right now I'm getting this. mysql_fetch_array(): argument not valid MySQL result resource Wondering about my fields. Earlier I was messing around with SERVER queries to see if my table would show with php, and it said Error performing query: Unknown column 'url' in 'field list' With your code it might say if there is a table problem, but thought I should show you this anyway. Unknown column makes no sense because my database has fields and rows, and I was told fields and columns are the same thing. I didn't get a database errors so it is finding the database. If your fields work with this, but you might not need it, then something must be wrong with my table,--but I sure don't know what. In SELECT "url" is what one of my fields is named. I tried putting the name of the table in there too and I got the same error. This is what I used. <?php $link = mysql_connect('mysql.......host', 'mblinks', 'password'); mysql_select_db('mblinks', $link); if (!mysql_select_db('mblinks')) { exit('<p>Unable to locate ' . 'database at this time.</p>'); } $result = @mysql_query('SELECT url'); if (!$result) { exit('<p>Error performing query: ' . mysql_error() . '</p>'); } ?> PineSolPirate 09-23-2006, 07:43 PM The "argument not valid MySQL result resource" might be because there are no entries in the database, maybe you can hand-edit some in with phpmyadmin and try again. If that doesn't work can you could post the whole chunk of php that's in your file, sans passwords and such of course? Also, try out the bbcode php tags, they make it easier to read code, just do [ php ] your code, then [ /php ] (Remove the spaces from those tags so they'll work. :) Rianna 09-23-2006, 07:54 PM You lost me on the php tags. I guess you mean there's some new fangled php type codes like the above that you want me to put the code in. Never seen them before, what do they do and where can you use them, out of curiosity. They work the same as other php tags? Which tag spaces are you talking about, in these? Rianna 09-23-2006, 07:57 PM I just noticed something in mysql am I supposed to add indexes? Or just fields and rows only for now. Thanks PineSolPirate 09-23-2006, 08:06 PM Oh, sorry, I meant in the forums :) They colorize and clean up the code. This is code out of the tags, <?php $world = "World"; echo "Hello".$world; ?> And this the same code inside those tags: <?php $world = "World"; echo "Hello".$world; ?> Try posting and clicking on the little icon that looks like a page with "php" on it -> http://www.webdeveloper.com/forum/images/editor/php.gif PineSolPirate 09-23-2006, 08:08 PM Just fields and rows are fine. Make sure it's all the exactly the same in the query and in the table, capitalization matters. For instance "SELECT url" from a table that only has a field called "URL" won't return right. I'm going to jump onto AIM, if you want to just talk through that my screen name is pinesolpirate Rianna 09-23-2006, 09:14 PM Ah gotcha, you're just full of surprises, don't know what AIM is either. :) Thought maybe php came out with some new tags, might not be a bad idea, their other ones are kind of whats the word.... Rianna 09-23-2006, 10:27 PM Finally remembered what AIM is. Can't find the little php gadget, I'll try the quotes but don't think thats it. I deleted the table and made a new one, phpadmin says I have a table with two rows just as I made it. Am I supposed to have certain funtions for the table and rows specified? I just used "text." Here's what I have, getting same error message, maybe you can see something wrong. <?php $link = mysql_connect('mysqlhostetc.', 'mblinks', 'password'); if (!$link) { die('Not connected : ' . mysql_error()); } $db_selected = mysql_select_db('mblinks', $link); if (!$db_selected) { die ('Can\'t use mblinks : ' . mysql_error()); } $query = "SELECT url, title FROM mytable WHERE type='L' LIMIT 0, 5"; // Note, here you are pulling back only 5 links. To get them all just remove the "LIMIT 0,5" Part $result = mysql_query($query); while ($row = mysql_fetch_array($result)) { print "<a href='$row[url]'>$row[title]</a><br/>"; } ?> mblinks is name of database, url is one column and title is another. No capitals anywhere. Still don't need an entry anywhere for name of table? I won't be back for several hours, sandmans got me, thank you Pirate! PineSolPirate 09-24-2006, 11:02 PM Make sure in the query that "mytable" is the name of the table you created, whatever you called it. Also, since I was wrong about how you were going to put in different types of information in one table you don't need the "type" column. It's not in your database anyway. So change this $query = "SELECT url, title FROM mytable WHERE [b]type='L'[/b[ LIMIT 0, 5"; To this, making sure "mytable" is the name of your table. $query = "SELECT url, title FROM mytable LIMIT 0, 5"; PineSolPirate 09-24-2006, 11:34 PM I went ahead and built this script on my system. I took some screenshots of the process. You can look at it all here: http://static.velvetcache.org/temp/phpexample/ Rianna 09-25-2006, 03:18 PM Thanks! webdeveloper.com
Copyright Internet.com Inc., All Rights Reserved. |