Help accessing mysql database using php?
-
Hi guys, I am a web designer trying to broaden my horizons by learning PHP & mySQL. I have been learning mySQL for a few months now by following tutorials etc and have got quite a good understanding of the format now. I have got onto adding a mySQL database to my website using PHP (I am still at the starting stages of learning PHP). Basically - I have a small pretend database on mySQL called "phpdb" containing one table called "usertable". I have connected to the database & tried to ask my first simple statement using: <?php mysql_connect ("localhost", "myusername", "mypassword"); mysql_connect_db ("phpdb") $result = mysql_query ("select * from usertable"); print "$result" --------------------------------------… When I update my server I get a message that say "QUERY WAS EMPTY". If I was to enter that simple "select * from usertable" on the mysql prompt I get the desired result - a table with 5 id's, first & last names & age. What is it that I am doing wrong? I am building up to doing guestbooks and message boards but need to get over this little stumbling block! Any help would be greatly appreciated but especially any help that explains rather than directing me to tutorials that I have often already exhausted! Another question I have for anyone man enough ....! In the same tutorial I was told to add a php code saying: $numrows = mysql_num_rows ($result), whenever i try to use this code I get an ugly syntax error! any idea's why?! Ta in advance :)
-
Answer:
My first use of mysql and php was also my first attempt at building a web page. Within one month I had a full production database working for a manufacturing company. If you are taking this long you must be using a wrong path to learn. You have one very obvious error in your script. Your second statement should be mysql_select_db("phpdb"); $result is only going to return the query ID. You should also always set a connection ID when connecting. This makes it possible to have connections to different databases at the same time. Try : <?php $conn = mysql_connect("localhost","myusername","… mysql_select_db("phpdb",$conn); $result = mysql_query("select * from usertable",$conn); if(mysql_num_rows($result) > 0){ while ($record = mysql_fetch_array($result)){ echo "$record[id], $record[firstname], $record[lastname], $record[age] "; } } else{ echo "Empty Table"; } exit; ?>
Yiaggi at Yahoo! Answers Visit the source
Related Q & A:
- How To Build Business Directory Using Php Mysql?Best solution by Stack Overflow
- where is the index physically located in MySQL database?Best solution by Database Administrators
- How to store an image in database using MySQL?Best solution by stackoverflow.com
- How to manage MySQL database in Azure?Best solution by stackoverflow.com
- How to do a local mysql database replication on an online server?Best solution by howtoforge.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.