PlagueInfected Posted August 29, 2009 Share Posted August 29, 2009 so im trying to select and display data from my database im currently coding like this... <?php $connect = mysql_connect("localhost", "username", "password"); if (!$connect) { die('Could not connect: ' . mysql_error()); } mysql_select_db("myblog", $connect); while($row = mysql_fetch_array($result)) { echo 'posted on: ' .date("l, F d, Y"); echo '<div class="name">' . $row['name'] . '</td>'; echo '<div class="subject">' . $row['subject'] . '</td>'; echo '<div class="mainsubject">' . $row['textarea'] . '</div>'; } mysql_close($connect); ?> and i get this error: mysql_fetch_array(): supplied argument is not a valid MySQL result resource Quote Link to comment https://forums.phpfreaks.com/topic/172349-solved-mysql_fetch_array-not-working/ Share on other sites More sharing options...
mellis95 Posted August 29, 2009 Share Posted August 29, 2009 It looks like there is no query. Quote Link to comment https://forums.phpfreaks.com/topic/172349-solved-mysql_fetch_array-not-working/#findComment-908695 Share on other sites More sharing options...
PFMaBiSmAd Posted August 29, 2009 Share Posted August 29, 2009 Your code has no SQL query statement in it and it has no mysql_query function call to execute that query. Quote Link to comment https://forums.phpfreaks.com/topic/172349-solved-mysql_fetch_array-not-working/#findComment-908698 Share on other sites More sharing options...
PlagueInfected Posted August 29, 2009 Author Share Posted August 29, 2009 ok added the query(how could i forget thr query) and i still got the same error <?php $connect = mysql_connect("localhost", "myusername", "mypasss"); if (!$connect) { die('Could not connect: ' . mysql_error()); } mysql_select_db("mydb", $connect); $result = mysql_query("SELECT * FROM blogs"); while($row = mysql_fetch_array($result)) { echo 'posted on: ' .date("l, F d, Y"); echo '<div class="name">' . $row['name'] . '</td>'; echo '<div class="subject">' . $row['subject'] . '</td>'; echo '<div class="mainsubject">' . $row['textarea'] . '</div>'; } mysql_close($connect); ?> Quote Link to comment https://forums.phpfreaks.com/topic/172349-solved-mysql_fetch_array-not-working/#findComment-908701 Share on other sites More sharing options...
darkfreaks Posted August 29, 2009 Share Posted August 29, 2009 [code<?php $result = mysql_query("SELECT*FROM blogs") or die(mysql_error()); ?> Quote Link to comment https://forums.phpfreaks.com/topic/172349-solved-mysql_fetch_array-not-working/#findComment-908703 Share on other sites More sharing options...
mellis95 Posted August 29, 2009 Share Posted August 29, 2009 I don't know that this will help any, but you are better off asking for each column by name instead of using SELECT * $result = mysql_query("SELECT name, subject, textarea FROM blogs"); Looking at that query, do you really have a column named textarea? Quote Link to comment https://forums.phpfreaks.com/topic/172349-solved-mysql_fetch_array-not-working/#findComment-908705 Share on other sites More sharing options...
PlagueInfected Posted August 29, 2009 Author Share Posted August 29, 2009 to be honest, this is my first time using MySQL here is the code I use to store into the DB <?php error_reporting(E_ALL); ini_set('display_errors', 1); $time = date("l, F d, Y"); $name = $_POST['name']; $subject = $_POST['subject']; $textarea = $_POST['textarea']; $connect = mysql_connect("localhost", "myusername", "mypass") or die ('Error:' .mysql_error()); if (!$connect) { die('Could not connect: ' . mysql_error()); } mysql_select_db("myblogs", $connect); mysql_query("INSERT INTO blogs (name, subject, textarea) VALUES ('$name', '$subject', '$textarea')"); echo 'updated DB with: ' .$name."<br />"; echo 'updated DB with: ' .$subject."<br />"; echo 'updated DB with: ' .$textarea."<br /><br />"; echo '<a href="javascript:history.back(-1);">Click to go back!</a>'; mysql_close($connect); ?> it works but i dont know how to write tables into my DB, completely new to it. I do have SQLDeveloper if that helps at all Quote Link to comment https://forums.phpfreaks.com/topic/172349-solved-mysql_fetch_array-not-working/#findComment-908711 Share on other sites More sharing options...
mellis95 Posted August 29, 2009 Share Posted August 29, 2009 It looks like you stored the data in "blogs" (database) and are calling it back from "mydb" (database). mysql_select_db("mydb", $connect); $result = mysql_query("SELECT * FROM blogs"); while($row = mysql_fetch_array($result)) { echo 'posted on: ' .date("l, F d, Y"); echo '<div class="name">' . $row['name'] . '</td>'; echo '<div class="subject">' . $row['subject'] . '</td>'; echo '<div class="mainsubject">' . $row['textarea'] . '</div>'; } mysql_close($connect); ?> Quote Link to comment https://forums.phpfreaks.com/topic/172349-solved-mysql_fetch_array-not-working/#findComment-908718 Share on other sites More sharing options...
PlagueInfected Posted August 29, 2009 Author Share Posted August 29, 2009 i just changed that to hide my stuff lol when i should just hide the user and pass when i have it even at my default db "jpmarine_blogs" it still gives me the error which has to do wit hthe mysql_fetch_array() Quote Link to comment https://forums.phpfreaks.com/topic/172349-solved-mysql_fetch_array-not-working/#findComment-908723 Share on other sites More sharing options...
PFMaBiSmAd Posted August 29, 2009 Share Posted August 29, 2009 For debugging, echo mysql_error(); immediately after the mysql_query() line of code to find out why the query is failing (no guessing is required.) Quote Link to comment https://forums.phpfreaks.com/topic/172349-solved-mysql_fetch_array-not-working/#findComment-908725 Share on other sites More sharing options...
PlagueInfected Posted August 29, 2009 Author Share Posted August 29, 2009 Table 'jpmarine_blogs.blogs' doesn't exist how do i do this =[ totally new to mysql. idk how to write a table into the mysql db Quote Link to comment https://forums.phpfreaks.com/topic/172349-solved-mysql_fetch_array-not-working/#findComment-908727 Share on other sites More sharing options...
PFMaBiSmAd Posted August 29, 2009 Share Posted August 29, 2009 If your database does not contain a table named blogs and you have never created a table at all, it will be to your advantage to read a basic php/mysql book or at least go through a basic mysql tutorial - http://w3schools.com/php/php_mysql_intro.asp Quote Link to comment https://forums.phpfreaks.com/topic/172349-solved-mysql_fetch_array-not-working/#findComment-908730 Share on other sites More sharing options...
PlagueInfected Posted August 29, 2009 Author Share Posted August 29, 2009 already reading it lol but it doesnt say where i code the table at like php, or sql dev idk Quote Link to comment https://forums.phpfreaks.com/topic/172349-solved-mysql_fetch_array-not-working/#findComment-908731 Share on other sites More sharing options...
mellis95 Posted August 29, 2009 Share Posted August 29, 2009 If it is a hosted site, you may have phpmyadmin installed, which is basically a GUI for managing MySQL. However, I think it is important to learn the commands. I highly recommend "Web Database Applications with PHP and MySQL" from O'Reilly. It is very thorough for getting started. Quote Link to comment https://forums.phpfreaks.com/topic/172349-solved-mysql_fetch_array-not-working/#findComment-908736 Share on other sites More sharing options...
PlagueInfected Posted August 29, 2009 Author Share Posted August 29, 2009 next paycheck i'll have a look into that book, i have his "programming PHP" book right now, I'll check out my hosts panel and see how it is, thanks dude =] Quote Link to comment https://forums.phpfreaks.com/topic/172349-solved-mysql_fetch_array-not-working/#findComment-908939 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.