m00ch0 Posted August 8, 2007 Share Posted August 8, 2007 I'm very new at this and all I need is to display the mysql data I have three fields to display name color and county its just a test program. I can enter the information into mysql database but cant display it on a page I get the error below Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/zendurl/public_html/m/m00ch00/display.php on line 18 I've search for it on these forums and cant find the problem anyone point me in a direction to help me or maybe point out my mistake -------------------------- Input.php <?php $db_host = "localhost"; $db_user = "USER"; $db_pwd = "PW"; $db_name = "databasename"; mysql_connect($db_host, $db_user, $db_pwd); mysql_select_db($db_name); ?> <html> <head> <title>MySQL submission form</title> </head> <body> <?php if (!isset($_POST['submit'])) { ?> <form action="" method="post"> Name: <input type="text" name="name"><br> Favorite Color: <input type="text" name="color"><br> What County are you from: <input type="text" name="county"><br> <input type="submit" name="submit" value="Submit!"> </form> <?php } else { $name = $_POST['name']; $color = $_POST['color']; $county = $_POST['county']; mysql_query("INSERT INTO `colours` (name, favoriteColor, county) VALUES ('$name', '$color', '$county')"); echo "Success! Your favourite colour has been added!"; } ?> </body> </html> ----------------------- Display.php <html> <head> <title>Displaying MySQL Data</title> </head> <body> <?php $db_host = "localhost"; $db_user = "USER"; $db_pwd = "PW"; $db_name = "databasename"; mysql_connect($db_host, $db_user, $db_pwd); mysql_select_db($db_name); ?> <table> <?php $sql = "SELECT * FROM colours"; $query = mysql_query($sql); if($row = mysql_fetch_array($query)) { echo "<tr>"; echo "<td>".$row['name']."</td>"; echo "<td>".$row['favoritecolor']."</td>"; echo "<td>".$row['county']."</td>"; echo "</tr>"; } ?> </table> </body> </html> ??? Quote Link to comment https://forums.phpfreaks.com/topic/63878-solved-simple-displaying-mysql-data-through-php/ Share on other sites More sharing options...
jitesh Posted August 8, 2007 Share Posted August 8, 2007 check table name `colours` and run this query directly in mysql (phpmyadmin or other) and check working ok or not. SELECT * FROM colours Quote Link to comment https://forums.phpfreaks.com/topic/63878-solved-simple-displaying-mysql-data-through-php/#findComment-318387 Share on other sites More sharing options...
m00ch0 Posted August 8, 2007 Author Share Posted August 8, 2007 It brought up all my information correctly when I ran the query? Quote Link to comment https://forums.phpfreaks.com/topic/63878-solved-simple-displaying-mysql-data-through-php/#findComment-318397 Share on other sites More sharing options...
jitesh Posted August 8, 2007 Share Posted August 8, 2007 while($row = mysql_fetch_array($query)) { echo "<tr>"; echo "<td>".$row['name']."</td>"; echo "<td>".$row['favoritecolor']."</td>"; echo "<td>".$row['county']."</td>"; echo "</tr>"; } Quote Link to comment https://forums.phpfreaks.com/topic/63878-solved-simple-displaying-mysql-data-through-php/#findComment-318400 Share on other sites More sharing options...
pranav_kavi Posted August 8, 2007 Share Posted August 8, 2007 mysql_query("INSERT INTO `colours` (name, favoriteColor, county) VALUES ('$name', '$color', '$county')"); Why is ther a single quote surrounding ur table name in the query,is ur table name 'colours' or colours? Quote Link to comment https://forums.phpfreaks.com/topic/63878-solved-simple-displaying-mysql-data-through-php/#findComment-318408 Share on other sites More sharing options...
m00ch0 Posted August 8, 2007 Author Share Posted August 8, 2007 Forgive my slow replys my host is free and the users keep maxing out I got the same error swapping the if for a while My table is named colours without the single quotes? will the single quotes effect the display.php page because the input.php works fine? I'll take them off and see how it goes Quote Link to comment https://forums.phpfreaks.com/topic/63878-solved-simple-displaying-mysql-data-through-php/#findComment-318414 Share on other sites More sharing options...
m00ch0 Posted August 8, 2007 Author Share Posted August 8, 2007 Same error occurred Quote Link to comment https://forums.phpfreaks.com/topic/63878-solved-simple-displaying-mysql-data-through-php/#findComment-318418 Share on other sites More sharing options...
jitesh Posted August 8, 2007 Share Posted August 8, 2007 while ($row = mysql_fetch_array($query, MYSQL_BOTH)) { Quote Link to comment https://forums.phpfreaks.com/topic/63878-solved-simple-displaying-mysql-data-through-php/#findComment-318420 Share on other sites More sharing options...
jitesh Posted August 8, 2007 Share Posted August 8, 2007 while($row = mysql_fetch_assoc($query)) { Quote Link to comment https://forums.phpfreaks.com/topic/63878-solved-simple-displaying-mysql-data-through-php/#findComment-318422 Share on other sites More sharing options...
pranav_kavi Posted August 8, 2007 Share Posted August 8, 2007 dont know whether tis cud b the problem...worth a try... in ur insert query,the column name is favoriteColor but in ur select query it is favoritecolor in echo "<td>".$row['favoritecolor']."</td>"; Quote Link to comment https://forums.phpfreaks.com/topic/63878-solved-simple-displaying-mysql-data-through-php/#findComment-318423 Share on other sites More sharing options...
m00ch0 Posted August 8, 2007 Author Share Posted August 8, 2007 Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/zendurl/public_html/m/m00ch00/display.php on line 18 could it be to do with my database or is that not possible basically my database is build up as name Text null favoritecolor Text null count Text null same problem changing the c in favoritecolor Quote Link to comment https://forums.phpfreaks.com/topic/63878-solved-simple-displaying-mysql-data-through-php/#findComment-318433 Share on other sites More sharing options...
pranav_kavi Posted August 8, 2007 Share Posted August 8, 2007 i cant figure out y it is not workin 4 u... but ur code s workin for me well..xcept that the value in 'favoritecolor' is not being displayed as the column names do not match. ve u chkd frm the backend...r d records being inserted properly??? Quote Link to comment https://forums.phpfreaks.com/topic/63878-solved-simple-displaying-mysql-data-through-php/#findComment-318439 Share on other sites More sharing options...
m00ch0 Posted August 8, 2007 Author Share Posted August 8, 2007 Could you write how you set up ur sql table because I think that maybe the problem as its the first time trying to get my playin with mysql with php Quote Link to comment https://forums.phpfreaks.com/topic/63878-solved-simple-displaying-mysql-data-through-php/#findComment-318446 Share on other sites More sharing options...
pranav_kavi Posted August 8, 2007 Share Posted August 8, 2007 query to create the table is as follows frm backend, create table `test`.`colours` ( `name` text NULL , `favoriteColor` text NULL , `county` text NULL ) Quote Link to comment https://forums.phpfreaks.com/topic/63878-solved-simple-displaying-mysql-data-through-php/#findComment-318452 Share on other sites More sharing options...
m00ch0 Posted August 8, 2007 Author Share Posted August 8, 2007 just to make sure are u using the exact script as it is in my first post? coz if so I can just play around with mysql till I find the problem Quote Link to comment https://forums.phpfreaks.com/topic/63878-solved-simple-displaying-mysql-data-through-php/#findComment-318472 Share on other sites More sharing options...
m00ch0 Posted August 8, 2007 Author Share Posted August 8, 2007 Turns out it was just my database name was wrong missed off a 0, took me all day to find that out Thanks for your help though as without it I would still be thinking its that line Thanks jitesh & pranav_kavi yourve both bit very helpful Quote Link to comment https://forums.phpfreaks.com/topic/63878-solved-simple-displaying-mysql-data-through-php/#findComment-318481 Share on other sites More sharing options...
m00ch0 Posted August 10, 2007 Author Share Posted August 10, 2007 I now have another problem it was working correctly then I added in the code echo "<tr class=\"data\">"; just above where if outputs my data echo "<tr>"; <-- added here echo "<td>".$row['name']."</td>"; echo "<td>".$row['favoritecolor']."</td>"; echo "<td>".$row['county']."</td>"; echo "</tr>"; now it only prints one line of my database information I then took the line out and replaced it with only <tr> which worked before and it still insists on only displayin one line of information out of mysql database I'm puzzled?!?!? Quote Link to comment https://forums.phpfreaks.com/topic/63878-solved-simple-displaying-mysql-data-through-php/#findComment-320275 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.