Page 1 of 1

SQL Syntax Error

Posted: Sun Oct 31, 2004 10:47 pm
by jabbaonthedais
I'm trying this code feyd helped me with a few weeks ago. I'm not sure if this is a syntax error or if my mysql version is too old.

Code: Select all

<?php
$db = '';
$user = '';
$pw = '';
$mysql_access = mysql_connect("localhost", $user, $pw);
mysql_select_db($db, $mysql_access);
$date = date("Ymj");

$rows = array();
$query = mysql_query('SELECT id,url,text FROM zlinks WHERE cat IS BETWEEN 1 AND 36') or die(mysql_error());

while($rows[] = mysql_fetch_assoc($query));
array_pop($rows);
$inserted = array();
$size = sizeof($rows);
while(sizeof($inserted) != $size)
{
 while(!in_array($inserted,$which = mt_rand(1,36)) && isset($rows[$which]));
 $sql = 'INSERT ' . 'INTO' . ' zlink1 (oid, date, url, text) VALUES(''' . $rows[$which]['id'] . ''', ''' . $date . ''', ''' . $rows[$which]['url'] . ''', ''' . $rows[$which]['text'] . ''')';

 if(!mysql_query($sql)) break;
 $inserted[] = $which;
}
?>
When I run the code just like that, it comes back with "You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'BETWEEN 1 AND 36' at line 1"

My server's php version is 4.3.9
and my mysql version is 4.0.22-standard-log

Posted: Mon Nov 01, 2004 4:00 am
by kettle_drum

Code: Select all

SELECT id,url,text FROM zlinks WHERE cat BETWEEN '1' AND '36'

Posted: Mon Nov 01, 2004 10:30 am
by jabbaonthedais
That brings back a loop of "Warning: in_array(): Wrong datatype for second argument in /home2/nnhaven/public_html/0gal.php on line 19"

Posted: Mon Nov 01, 2004 10:44 am
by swdev
Read the php manual for [php_man]in_array[/php_man]

this says
bool in_array ( mixed needle, array haystack [, bool strict])

Searches haystack for needle and returns TRUE if it is found in the array, FALSE otherwise.
so you need to swap your $inserted and $which variables on line 19

Posted: Mon Nov 01, 2004 1:14 pm
by jabbaonthedais
When I swap them, I get a loop of:

Warning: in_array(): Wrong datatype for second argument in /home2/nnhaven/public_html/0gal.php on line 20

Warning: Cannot use a scalar value as an array in /home2/nnhaven/public_html/0gal.php on line 24

I'm reading the manual, but I'm still pretty new to php.