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
https://forums.phpfreaks.com/topic/172349-solved-mysql_fetch_array-not-working/
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);
?>

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?

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

 

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

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

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.