Solved

mysql_fetch_array(): problem

Posted on 2007-03-26
7
428 Views
Last Modified: 2013-12-13
here is my PHP:
<?
$link = mysql_connect("", "", "") OR DIE("Unable to connect to the database");
mysql_select_db("");
$result = mysql_query("(SELECT * FROM wp1_posts ORDER BY postdate DESC LIMIT 1) UNION ALL
(SELECT * FROM wp2_posts ORDER BY post_date DESC LIMIT 1) UNION ALL
(SELECT * FROM wp3_posts ORDER BY post_date DESC LIMIT 4) UNION ALL
(SELECT * FROM wp4_posts ORDER BY post_date DESC LIMIT 4) UNION ALL
(SELECT * FROM wp5_posts ORDER BY post_date DESC LIMIT 4) UNION ALL
(SELECT * FROM wp6_posts ORDER BY post_date DESC LIMIT 4) UNION ALL
(SELECT * FROM wp7_posts ORDER BY post_date DESC LIMIT 4) UNION ALL
(SELECT * FROM wp8_posts ORDER BY post_date DESC LIMIT 4) UNION ALL
(SELECT * FROM wp9_posts ORDER BY post_date DESC LIMIT 4) UNION ALL
(SELECT * FROM wp10_posts ORDER BY post_date DESC LIMIT 4) UNION ALL
(SELECT * FROM wp11_posts ORDER BY post_date DESC LIMIT 4) UNION ALL
(SELECT * FROM wp12_posts ORDER BY post_date DESC LIMIT 4) UNION ALL
(SELECT * FROM wp13_posts ORDER BY post_date DESC LIMIT 4) UNION ALL
(SELECT * FROM wp14_posts ORDER BY post_date DESC LIMIT 4) ORDER BY post_date DESC LIMIT 4;");
//$r = mysql_query($query);
//echo mysql_error();
if( $r=mysql_fetch_array( $result ) )
    $posttitle_1 = $result[ "post_title" ];
if( $r=mysql_fetch_array( $result ) )
    $posttitle_2 = $result[ "post_title" ];
if( $r=mysql_fetch_array( $result ) )
    $posttitle_3 = $result[ "post_title" ];
if( $r=mysql_fetch_array( $result ) )
    $posttitle_4 = $result[ "post_title" ];
mysql_close($result);
?>

I keep getting this error:


Warning: mysql_close(): supplied argument is not a valid MySQL-Link resource in /home/14612/domains/.com/html/blog_refer.php on line 25

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/14612/domains/.com/html/blog_refer.php on line 28

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/14612/domains/.com/html/blog_refer.php on line 30

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/14612/domains/.com/html/blog_refer.php on line 32

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/14612/domains/.com/html/blog_refer.php on line 34



is there something wrong with my statement?
0
Comment
Question by:lvollmer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 10

Expert Comment

by:ClickCentric
ID: 18796804
Yes. For starters, it looks like you have qualifier criteria but you've bound all of your select statements which puts them outside of the select statements which means they're invalid.   I'd strongly suggest running your SQL from the command line or something like phpmyadmin.  The part outside of the last select statement shouldn't be there.  Beyond that, there may be more wrong dependent on the structure of the database being queried.  
0
 

Author Comment

by:lvollmer
ID: 18796811
how should the statement look?
0
 

Author Comment

by:lvollmer
ID: 18796887
I got the SQL to work. i had to make a slight modification.

The problem I am having now is that the variables posttitle_1 -4 do not echo. Only the breaks echo. Any idea why that is?
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 10

Expert Comment

by:ClickCentric
ID: 18798931
Can you post the current version of the code? Do a print_r($result) right after query.  It should print out an object resource identifier.  If it prints out something else, the sql is still failing. Have you confirmed that the sql is returning something on the command line or some other means?  
0
 

Author Comment

by:lvollmer
ID: 18800637
teh sql is returning data. I think my array syntax is wrong i will paste it in
0
 

Author Comment

by:lvollmer
ID: 18800738
<?

$link = mysql_connect("", "", "") OR DIE("Unable to connect to the database");
mysql_select_db("");
$result = mysql_query("(SELECT * FROM wp_1_posts ORDER BY post_date DESC LIMIT 1) UNION ALL
(SELECT * FROM wp_2_posts ORDER BY post_date DESC LIMIT 1) UNION ALL
(SELECT * FROM wp_3_posts ORDER BY post_date DESC LIMIT 4) UNION ALL
(SELECT * FROM wp_5_posts ORDER BY post_date DESC LIMIT 4) UNION ALL
(SELECT * FROM wp_6_posts ORDER BY post_date DESC LIMIT 4) UNION ALL
(SELECT * FROM wp_7_posts ORDER BY post_date DESC LIMIT 4) UNION ALL
(SELECT * FROM wp_8_posts ORDER BY post_date DESC LIMIT 4) UNION ALL
(SELECT * FROM wp_9_posts ORDER BY post_date DESC LIMIT 4) UNION ALL
(SELECT * FROM wp_10_posts ORDER BY post_date DESC LIMIT 4) UNION ALL
(SELECT * FROM wp_11_posts ORDER BY post_date DESC LIMIT 4) UNION ALL
(SELECT * FROM wp_12_posts ORDER BY post_date DESC LIMIT 4) UNION ALL
(SELECT * FROM wp_13_posts ORDER BY post_date DESC LIMIT 4) UNION ALL
(SELECT * FROM wp_35_posts ORDER BY post_date DESC LIMIT 4) UNION ALL
(SELECT * FROM wp_43_posts ORDER BY post_date DESC LIMIT 4) UNION ALL
(SELECT * FROM wp_55_posts ORDER BY post_date DESC LIMIT 4) UNION ALL
(SELECT * FROM wp_65_posts ORDER BY post_date DESC LIMIT 4) UNION ALL
(SELECT * FROM wp_75_posts ORDER BY post_date DESC LIMIT 4) UNION ALL
(SELECT * FROM wp_77_posts ORDER BY post_date DESC LIMIT 4) UNION ALL
(SELECT * FROM wp_78_posts ORDER BY post_date DESC LIMIT 4) UNION ALL
(SELECT * FROM wp_79_posts ORDER BY post_date DESC LIMIT 4) UNION ALL
(SELECT * FROM wp_80_posts ORDER BY post_date DESC LIMIT 4) ORDER BY post_date DESC LIMIT 4;");
//$r = mysql_query($query);
//echo mysql_error();
if( $r=mysql_fetch_array( $result ) )
    $posttitle_1 = $result[ "post_title" ];
if( $r=mysql_fetch_array( $result ) )
    $posttitle_2 = $result[ "post_title" ];
if( $r=mysql_fetch_array( $result ) )
    $posttitle_3 = $result[ "post_title" ];
if( $r=mysql_fetch_array( $result ) )
    $posttitle_4 = $result[ "post_title" ];

?>
<body>
<? echo $posttitle_1;?>
<br /><hr />
<? echo $posttitle_2;?>
<br /><hr />
<? echo $posttitle_3;?>
<br /><hr />
<? echo $posttitle_4;?>
<br /><hr />
<? mysql_close($link); ?>
0
 
LVL 10

Accepted Solution

by:
ClickCentric earned 500 total points
ID: 18813389
Change:
if( $r=mysql_fetch_array( $result ) )
    $posttitle_1 = $result[ "post_title" ];
if( $r=mysql_fetch_array( $result ) )
    $posttitle_2 = $result[ "post_title" ];
if( $r=mysql_fetch_array( $result ) )
    $posttitle_3 = $result[ "post_title" ];
if( $r=mysql_fetch_array( $result ) )
    $posttitle_4 = $result[ "post_title" ];


To:
if( $r=mysql_fetch_array( $result ) )
    $posttitle_1 = $r[ "post_title" ];
if( $r=mysql_fetch_array( $result ) )
    $posttitle_2 = $r[ "post_title" ];
if( $r=mysql_fetch_array( $result ) )
    $posttitle_3 = $r[ "post_title" ];
if( $r=mysql_fetch_array( $result ) )
    $posttitle_4 = $r[ "post_title" ];
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article discusses four methods for overlaying images in a container on a web page
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question