Jump to content

[SOLVED] mysql_fetch_array() not working


PlagueInfected

Recommended Posts

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

Link to comment
Share on other sites

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);
?>

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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);
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.