Forum Moderators: coopster

Message Too Old, No Replies

Error

Warning: mysql_fetch_arry(): supplied argument is not a valid MySQL

         

CIAimages

8:15 am on Jun 14, 2004 (gmt 0)

10+ Year Member


<?php
if ($page) {
$page_sql = "SELECT * FROM SPI_html WHERE page_name=$page";
$page_result = mysql_query($page_sql);
$page_myrow = mysql_fetch_array($page_result);
$body = $page_myrow["body"];
echo $body;
}
?>

I am getting an error and I cannot figure out why?

johnerazo

8:59 am on Jun 14, 2004 (gmt 0)

10+ Year Member



$page_result = mysql_query($page_sql);

Check this out for the right syntax of mysql_query:
[php.net ]

Timotheos

3:21 pm on Jun 14, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Usually if there's something wrong with your sql syntax it will tell you. Another possibility is that you haven't connected with your database.

CIAimages

4:39 pm on Jun 14, 2004 (gmt 0)

10+ Year Member



The error I get is:

Warning: mysql_fetch_field(): supplied argument is not a valid MySQL result resource in /usr/local/www/test.php on line 58

I tried

$page_myrow = mysql_fetch_array($page_result);
$page_myrow = mysql_fetch_row($page_result);
$page_myrow = mysql_fetch_field($page_result);

54 <?php
55 if ($page) {
56 $page_sql = "SELECT * FROM SPI_html WHERE page_name=$page";
57 $page_result = mysql_query($page_sql);
58 $page_myrow = mysql_fetch_array($page_result);
59 $body = $page_myrow["body"];
60 echo $body;
61 }
62 ?>

I get the same error all the time.

[edited by: CIAimages at 4:43 pm (utc) on June 14, 2004]

Philosopher

4:41 pm on Jun 14, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



and what code do you have on line 58?

Maybe give use the line before and the line after as well.

jatar_k

4:43 pm on Jun 14, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



try using this for your query

$page_result = mysql_query($page_sql) or die(mysql_error());

what error message does that give you?

CIAimages

4:47 pm on Jun 14, 2004 (gmt 0)

10+ Year Member



Now I'm getting this error:

Unknown column 'home' in 'where clause'

jatar_k

4:58 pm on Jun 14, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



looks like your query is not quite right then, checking for a column that doesn't exist

try this now

$page_sql = "SELECT * FROM SPI_html WHERE page_name=$page";
echo $page_sql;

then take a look at the constructed query and see if there are any errors in it. I often take the output query and connect to sql, in my case with putty, and then test the query there.

I usually get the same error but then i can look at the table itself and check all the columns and the data to make sure I am doing what I thought I was doing and it makes it easier to fix the query.

Some people obviously do this with phpmyadmin but I am a command line mysql'er. ;)

CIAimages

5:06 pm on Jun 14, 2004 (gmt 0)

10+ Year Member



SELECT * FROM SPI_html WHERE page_name=home

I see no error in the sql statment
I looked at my db via phpMyAdmin and I see no error in my table eather. But I am new at php and mysql so here is how my table look like

Field Type Null
page_name text No
title text No
description text No
keywords text No
body text No

Could the error come from the fact that I use the same connection for 2 different table processing?

// Connect to the mySQl server
// ***************************
$db = mysql_connect($host, $username, $password) or die("Could not connect to host.");
mysql_select_db($database, $db) or die("Could not find database.");

// Get all the Info from the mySQl DB
// **********************************
$sql = "SELECT * FROM SPI_html";
$result = mysql_query($sql);

as well for this script

$page_sql = "SELECT * FROM SPI_html WHERE page_name=$page";

Timotheos

5:58 pm on Jun 14, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try it with single quotes in the WHERE statement...
SELECT * FROM SPI_html WHERE page_name='home'

If you do a select in phpMyAdmin it will display the SQL statement. Often that gives me clues of what I'm doing wrong.

m_shroom

2:06 am on Jun 15, 2004 (gmt 0)

10+ Year Member



1. I think it is beter to use mysql_pconect.
2. A db conection must be done on each page of your script that it is used.

3. create a function to sinplify it (functions a held in memory)

function condb()
{$db = mysql_pconnect($host, $username, $password) or die("Could not connect to host.");
mysql_select_db($database, $db) or die("Could not find database.");}

then every page you need to connect to the db simply type

condb();