SQL Syntax Error

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
jabbaonthedais
Forum Contributor
Posts: 127
Joined: Wed Aug 18, 2004 12:08 pm

SQL Syntax Error

Post 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
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

Code: Select all

SELECT id,url,text FROM zlinks WHERE cat BETWEEN '1' AND '36'
jabbaonthedais
Forum Contributor
Posts: 127
Joined: Wed Aug 18, 2004 12:08 pm

Post 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"
swdev
Forum Commoner
Posts: 59
Joined: Mon Oct 25, 2004 8:04 am

Post 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
jabbaonthedais
Forum Contributor
Posts: 127
Joined: Wed Aug 18, 2004 12:08 pm

Post 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.
Post Reply