Can some one tell me a very basic way to set up a sql database with phpMyAdmin. Say an example with one row and give me the conect code to show this row on a php page. The way to set up the database for the following bit of code would be useful! As am having problems. I can't seem to get my head round how you call a table from a sql database. I can set them up fine. I seen enough tutorials and used access and excell enough to know how to set up databases like this quite easy. I just dont get how to call it and display it using php. Any help would be greatly appreciated. Thanks. <?php $c =mysql_connect(localhost,root, ""); mysql_select_db(user.$c); for($i=1; $i<=50;$i++) { $sql = "SELECT * FROM `users` WHERE `username` = $i"; $res = mysql_query($sql) or die(mysql_error()); $row = mysql_fetch_assoc($res); echo "Row $row[id] has the username of $row[username]<br>\n"; } ?> Code (markup):
<?php $c =mysql_connect(localhost,root, ""); mysql_select_db(user,$c); for($i=1; $i<=50;$i++) { $sql = "SELECT * FROM users WHERE username = '$i'"; $res = mysql_query($sql); $myrow = mysql_fetch_array($res); echo "Row $myrow['id'] has the username of $myrow['username']<br>\n"; } ?> Code (markup):
<?php $c = mysql_connect("localhost", "username", "password"); mysql_select_db("user", $c); $res = mysql_query("SELECT * FROM users WHERE username = '$i' LIMIT 0, 50"); while ($row = mysql_fetch_assoc($res)) { echo "Row ".$row['id']." has the username of ".$row['username']."<br>"; } ?>
I am using apache server on my computer, the server is "localhost", username is "root", there is no password so its "", I created a database called "test" and a table called "poo". How would I connect to this table and be able to add data to it. I have added. three rows. ID, name, surname. So you could make a basic form input name and surname and get it to post it to the table. I have no problem writing the code for this but everytime I try to I seem to have problem connecting to the database as it goes NO Database found or brings up an error. Any help would be great! Thanks!
Not entirely sure what you're asking. Ned points out that your mysql_select_db had an error. grikis points out that you're better of selecting all the rows and outputing them in a loop though his code won't work. I think however you're asking how to setup the database table in the first place.
<?php $c = mysql_connect("localhost", "root", ""); mysql_select_db("test", $c); $res = mysql_query("SELECT * FROM poo"); while ($row = mysql_fetch_assoc($res)) { echo "Id - ".$row['id']." Name - ".$row['name']." Surname - ".$row['surname']."<br>"; } ?> This code will display all records
Thanks a lot for anyone who posted a reply! I realise now what I was doing wrong. I did not understand that I was opening the database at the start. Then I was trying to open it again later. It was this bit that confused me ("SELECT * FROM poo"); I was trying to ("SELECT * FROM test"); and it was not working! Basically I did not understand properly how to select a row out of a table and call it. Thanks for any help I understand it now. Cheers all!